当前位置:网站首页>ABC253F Operations on a Matrix
ABC253F Operations on a Matrix
2022-06-12 09:03:00 【andyc_03】
题意
给定一个n*m的方格,初始均为0,有以下三种操作:
1.给l-r行每个位置加x
2.给l列每个位置修改为x
3.询问x列y行的值
分析
我们考虑第q次询问的答案,是由上一次修改第y列的位置q',和q'-q次之间修改x行的和组成的
因为加x的操作可加减,所以这里不需要主席树,可以离线下来做一个用前缀差计算,维护树状数组即可
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int n,m,q;
int opt[maxn],a[maxn],b[maxn],c[maxn];
int pos[maxn],cnt;
vector <int> del[maxn];
ll val[maxn],ans[maxn];
int lowbit(int x)
{
return x&(-x);
}
void modify(int pos,ll x)
{
while(pos<=m+1)
{
val[pos]+=x;
pos+=lowbit(pos);
}
}
ll query(int pos)
{
ll res=0;
while(pos)
{
res+=val[pos];
pos-=lowbit(pos);
}
return res;
}
int id[maxn],rev[maxn];
int main()
{
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=q;i++)
{
scanf("%d",&opt[i]);
if(opt[i]==1)
{
scanf("%d%d%d",&a[i],&b[i],&c[i]);
b[i]++;
}
if(opt[i]==2)
{
scanf("%d%d",&a[i],&c[i]);
pos[a[i]]=i;
}
if(opt[i]==3)
{
scanf("%d%d",&a[i],&b[i]);
swap(a[i],b[i]);
int x=c[pos[b[i]]];
ans[++cnt]=x;
id[i]=cnt; rev[cnt]=i;
del[pos[b[i]]].push_back(cnt);
}
}
// for(int i=1;i<=cnt;i++) printf("%lld\n",ans[i]);
for(int i=1;i<=q;i++)
{
if(opt[i]==1)
{
modify(a[i],c[i]);
modify(b[i],-c[i]);
}
if(opt[i]==2)
for(auto v:del[i]) ans[v]-=query(a[rev[v]]);
if(opt[i]==3)
ans[id[i]]+=query(a[i]);
}
for(int i=1;i<=cnt;i++) printf("%lld\n",ans[i]);
return 0;
}边栏推荐
- Sword finger offer:[day 9 dynamic planning (medium)] --- > maximum sum of continuous subarrays
- [character set 9] will GBK be garbled when copied to unicode?
- Source code and scheme for target recognition, detection and 6D attitude estimation (the most advanced method and data set)
- Handling abnormal data
- consul 配置相关
- Application method of new version UI of idea + use method of non test qualification and related introduction
- 【字符集七】汉字的宽字符码和多字节码分别是多少
- Jenkins Pipeline 语法
- mySql学习记录——三、mysql查询语句
- The classic dog contract of smart contract (I)
猜你喜欢

Summary of common character sets

xshell启动遇到“由于找不到mfc110.dll,无法继续执行代码的解决方法”

Description of string

Priority issues

List < string > sort

Filters and listeners

Set up redis sentinel cluster (instance):

Introduction to applet cloud development -- questionnaire evaluation applet practice (7)

The classic dog contract of smart contract (I)

Machine learning notes - circular neural network memo list
随机推荐
Load the source code of 2D 3D virtual anchor in the web page (1: project introduction and source code)
(12) Interactive component selectable
第六章-包含多个段的程序
Regular verification user name
Introduction to applet cloud development -- questionnaire evaluation applet practice (7)
Description of string
第七章-更灵活定位内存地址
The classic dog contract of smart contract (I)
《MATLAB 神經網絡43個案例分析》:第7章 RBF網絡的回歸--非線性函數回歸的實現
Handling abnormal data
[character set 9] will GBK be garbled when copied to unicode?
Background attribute compound writing
Wechat applet image saving function
Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock
Flink传入自定义的参数或配置文件
Knowledge points of 2022 system integration project management engineer examination: project cost management
Application method of new version UI of idea + use method of non test qualification and related introduction
【无标题】Task3 多路召回
Chapter IV - first procedure
【字符集六】宽字符串和多字节字符互转