当前位置:网站首页>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;
}边栏推荐
- Zhuanzhuanben - LAN construction - Notes
- Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
- LeetCode 40. 组合总和 II
- From design delivery to development, easy and efficient!
- Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
- Browser principle mind map
- 深入学习JVM底层(二):HotSpot虚拟机对象
- Sentinel 阿里开源流量防护组件
- 【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
- LeetCode 77. combination
猜你喜欢
随机推荐
Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
10 erreurs classiques de MySQL
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
介绍两款代码自动生成器,帮助提升工作效率
【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
The official zero foundation introduction jetpack compose Chinese course is coming!
Browser principle mind map
Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
锐捷EBGP 配置案例
深入了解JUC并发(一)什么是JUC
谷歌出海创业加速器报名倒计时 3 天,创业人闯关指南提前收藏!
Introduce two automatic code generators to help improve work efficiency
Lucene Basics
Sentinel 阿里开源流量防护组件
Step by step | help you easily submit Google play data security form
Use of Arduino wire Library
When requesting resttemplate, set the request header, request parameters, and request body.
Codeforces Round #797 (Div. 3) A—E
Frequently asked questions about jetpack compose and material you
Pbootcms collection and warehousing tutorial quick collection release


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




