当前位置:网站首页>AtCoder Beginner Contest 253 F - Operations on a Matrix // 树状数组
AtCoder Beginner Contest 253 F - Operations on a Matrix // 树状数组
2022-07-02 06:18:00 【Jakon_】
题目传送门:F - Operations on a Matrix (atcoder.jp)
题意:
给一个N*M大小的零矩阵,以及Q次操作。操作1(l,r,x):对于 [l,r] 区间内的每列都加上x;操作2(i,x):对于第 i 行,替换为x;操作3(i,j):查询矩阵第 i 行,第 j 列元素的值。
N、M、Q大小均为2E5.
思路:树状数组
首先考虑没有操作2的情况,那么很容易地就可以用树状数组实现对列的区间加及单点查询。
当有操作2时,对于操作3的查询:将该行最后一次操作2的行修改值记作x,从最后一次操作2到该次操作所有的1操作列增加值记作sum2,那么输出的答案就为x + sum2。将从开始到该次操作的所有1操作列增加值记作sum,从开始到最后一次操作2的列增加值记作sum1,那么有sum2 = sum - sum1,答案就为:x + sum - sum1。离线查询即可。
代码参考:
#include <bits/stdc++.h>
#define LL long long
#define lowbit(x) (x & -x)
using namespace std;
const int N = 200010;
int n, m, Q, last[N];
LL tr[N], ans[N];
vector<int> v[N];
struct query {
int op, a, b, c;
} q[N];
void add(int x, int c)
{
for(int i = x; i <= m; i += lowbit(i)) tr[i] += c;
}
LL sum(int x)
{
LL res = 0;
for(int i = x; i; i -= lowbit(i)) res += tr[i];
return res;
}
int main()
{
cin >> n >> m >> Q;
for(int i = 1; i <= Q; i++) {
scanf("%d%d%d", &q[i].op, &q[i].a, &q[i].b);
if(q[i].op == 1) scanf("%d", &q[i].c);
else if(q[i].op == 2) last[q[i].a] = i;
else v[last[q[i].a]].push_back(i);
}
for(int i = 1; i <= Q; i++) {
if(q[i].op == 1) add(q[i].a, q[i].c), add(q[i].b + 1, -q[i].c);
else if(q[i].op == 2) for(auto item : v[i]) ans[item] = q[i].b - sum(q[item].b);
else printf("%lld\n", ans[i] + sum(q[i].b));
}
return 0;
}
边栏推荐
- Data science [9]: SVD (2)
- Summary of WLAN related knowledge points
- ROS create workspace
- 分布式事务 :可靠消息最终一致性方案
- 日志(常用的日志框架)
- BGP 路由優選規則和通告原則
- Current situation analysis of Devops and noops
- 锐捷EBGP 配置案例
- Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
- CUDA中的Warp matrix functions
猜你喜欢
找到页面当前元素z-index最高的数值
深入了解JUC并发(一)什么是JUC
TensorRT的数据格式定义详解
Amazon AWS data Lake Work Pit 1
Sentinel 阿里开源流量防护组件
Pbootcms collection and warehousing tutorial quick collection release
Current situation analysis of Devops and noops
State machine in BGP
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
来自读者们的 I/O 观后感|有奖征集获奖名单
随机推荐
实习生跑路留了一个大坑,搞出2个线上问题,我被坑惨了
深入了解JUC并发(一)什么是JUC
一起学习SQL中各种join以及它们的区别
It is said that Kwai will pay for the Tiktok super fast version of the video? How can you miss this opportunity to collect wool?
最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
递归(迷宫问题、8皇后问题)
LeetCode 40. 组合总和 II
程序员的自我修养—找工作反思篇
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
Step by step | help you easily submit Google play data security form
ShardingSphere-JDBC篇
注解和反射详解以及运用
CUDA中的线程层次
New version of dedecms collection and release plug-in tutorial tool
The official zero foundation introduction jetpack compose Chinese course is coming!
阿里云MFA绑定Chrome浏览器
深入了解JUC并发(二)并发理论
10 erreurs classiques de MySQL
广告业务Bug复盘总结
VLAN experiment of switching technology