当前位置:网站首页>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;
}边栏推荐
- CUDA中的存储空间修饰符
- LeetCode 47. Full arrangement II
- LeetCode 40. Combined sum II
- Golang--map扩容机制(含源码)
- LeetCode 283. 移动零
- 介绍两款代码自动生成器,帮助提升工作效率
- New version of dedecms collection and release plug-in tutorial tool
- CUDA中的线程层次
- Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
- LeetCode 77. combination
猜你喜欢

Google play academy team PK competition, official start!

实习生跑路留了一个大坑,搞出2个线上问题,我被坑惨了

Spark overview

Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music

VRRP之监视上行链路

ShardingSphere-JDBC篇
![Data science [viii]: SVD (I)](/img/cb/7bf066a656d49666985a865c3a1456.png)
Data science [viii]: SVD (I)

穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!

Classic literature reading -- deformable Detr

数据科学【八】:SVD(一)
随机推荐
Sudo right raising
500. Keyboard line
BGP中的状态机
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
The official zero foundation introduction jetpack compose Chinese course is coming!
Ros2 --- lifecycle node summary
In depth understanding of JUC concurrency (II) concurrency theory
I/o multiplexing & event driven yyds dry inventory
BGP routing optimization rules and notification principles
Does the assignment of Boolean types such as tag attribute disabled selected checked not take effect?
Learn about various joins in SQL and their differences
数据科学【九】:SVD(二)
链表(线性结构)
浅谈三点建议为所有已经毕业和终将毕业的同学
CUDA中的Warp Shuffle
MySQL的10大經典錯誤
阿里云MFA绑定Chrome浏览器
Network related knowledge (Hardware Engineer)
Community theory | kotlin flow's principle and design philosophy
分布式事务 :可靠消息最终一致性方案