当前位置:网站首页>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;
}
边栏推荐
- MySQL的10大经典错误
- Codeforces Round #797 (Div. 3) A—E
- BGP中的状态机
- State machine in BGP
- Generic classes and parameterized classes of SystemVerilog
- CUDA用户对象
- 穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
- CUDA中的Warp Shuffle
- Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
- Monitoring uplink of VRRP
猜你喜欢
Sumo tutorial Hello World
Spark overview
实习生跑路留了一个大坑,搞出2个线上问题,我被坑惨了
500. Keyboard line
官方零基础入门 Jetpack Compose 的中文课程来啦!
State machine in BGP
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
Network related knowledge (Hardware Engineer)
Linear DP (split)
数据科学【八】:SVD(一)
随机推荐
递归(迷宫问题、8皇后问题)
BGP报文详细解释
Hydration failed because the initial UI does not match what was rendered on the server.问题原因之一
When requesting resttemplate, set the request header, request parameters, and request body.
State machine in BGP
LeetCode 47. 全排列 II
Use of Arduino wire Library
稀疏数组(非线性结构)
Flutter hybrid development: develop a simple quick start framework | developers say · dtalk
Little bear sect manual query and ADC in-depth study
CUDA中内置的Vector类型和变量
【程序员的自我修养]—找工作反思篇二
【张三学C语言之】—深入理解数据存储
CUDA中的线程层次
Is there a really free applet?
实习生跑路留了一个大坑,搞出2个线上问题,我被坑惨了
Don't use the new WP collection. Don't use WordPress collection without update
Linear DP (split)
Let every developer use machine learning technology
CUDA与Direct3D 一致性