poj 3468 A Simple Problem with Integers(区间查询区间更新)
来源:互联网 发布:网络剧申请流程 编辑:程序博客网 时间:2024/06/15 04:03
题目:
You have N integers, A1, A2, … , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b” means querying the sum of Aa, Aa+1, … , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
The sums may exceed the range of 32-bit integers.
题意:
给你一个序列,要你进行区间修改以及区间查询.
ps:
模板题,lazy标记的使用,没啥说的,注意lazy是累加,还有sum更新的方法是加上区间长乘以更新值,还有一个地方坑了我好久找bug,写在代码注释里了。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define ls (rt<<1)#define rs (rt<<1|1)#define lson L,mid,ls#define rson mid+1,R,rs#define def_mid int mid=(L+R)>>1#define ll long long#define INF 0x3f3f3f3fusing namespace std;const int maxn = 123456;ll sum[maxn<<2],lazy[maxn<<2];ll a[maxn];void pushup(int rt){ sum[rt]=sum[ls]+sum[rs];}void pushlazy(int lz,int len,int rt){ sum[rt]+=(ll)lz*len;//被这里坑了两个int类型相乘会爆int所以在这里要强转为ll lazy[rt]+=lz;}void pushdown(int L,int R,int rt){ if(lazy[rt]!=0) { def_mid; pushlazy(lazy[rt],mid-L+1,ls); pushlazy(lazy[rt],R-mid,rs); lazy[rt]=0; } return;}void build(int L,int R,int rt){ lazy[rt]=0; if(L==R) { sum[rt]=a[L]; return; } def_mid; build(lson); build(rson); pushup(rt);}void update(int l,int r,int L,int R,int rt,int k){ if(l<=L&&r>=R) { sum[rt]+=k*(R-L+1); lazy[rt]+=k; return; } pushdown(L,R,rt); def_mid; if(l<=mid) update(l,r,lson,k); if(r>mid) update(l,r,rson,k); pushup(rt);}ll querysum(int l,int r,int L,int R,int rt){ if(l<=L&&r>=R) return sum[rt]; pushdown(L,R,rt); def_mid; ll ret=0; if(l<=mid) ret+=querysum(l,r,lson); if(r>mid) ret+=querysum(l,r,rson); return ret;}int main(){ int n,q,k; while(scanf("%d%d",&n,&q)!=EOF) { memset(lazy,0,sizeof(lazy)); memset(sum,0,sizeof(sum)); memset(a,0,sizeof(a)); char op[3]; int l,r; for(int i=1;i<=n;i++) scanf("%lld",&a[i]); build(1,n,1); while(q--) { scanf("%s%d%d",op,&l,&r); if(op[0]=='Q') printf("%lld\n",querysum(l,r,1,n,1)); else { scanf("%d",&k); update(l,r,1,n,1,k); } } } return 0;}
- poj 3468 A Simple Problem with Integers经典区间更新区间查询
- POJ 3468 A Simple Problem with Integers (树状数组解法 树状数组区间更新 区间查询)
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
- POJ 3468 A Simple Problem with Integers 树状数组(区间更新,区间查询)
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
- poj 3468 A Simple Problem with Integers(区间查询区间更新)
- 线段树 (更新区间查询区间)poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers(线段树+树状数组区间更新区间查询)
- POJ 3468 A Simple Problem with Integers(区间更新)
- POJ 3468 A Simple Problem with Integers(区间更新)
- POJ 3468 A Simple Problem with Integers(区间更新)
- POJ 3468 A Simple Problem with Integers(区间更新)
- poj 3468 A Simple Problem with Integers(线段树|成段更新,区间查询)
- POJ 3468 A Simple Problem with Integers 线段树成段更新+区间查询
- POJ 3468 A Simple Problem with Integers 线段树(区间更新,查询,lazy数组)
- POJ - 3468 A Simple Problem with Integers(线段树成段更新,查询区间和)
- POJ A Simple Problem with Integers (线段树区间更新区间查询模板)
- poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)
- python中文显示不正常解决方案
- 深入理解Apache Flink核心技术
- 数论基础知识
- javascript中的ajax
- RE:JAVA学习-深入继承+访问控制
- poj 3468 A Simple Problem with Integers(区间查询区间更新)
- 巨头都在争抢无人驾驶 这家智慧停车公司却先上了路 科技事务 百家号 08-14 15:55 今年来,互联网巨头在智慧交通领域动作频频,4月初,百度提出雄心勃勃的“阿波罗计划”,宣布开放自动驾驶平台以
- linux查看软件的安装目录
- codeMirror支持sql输入提示
- 安装mysql connector odbc 后在 控制面板 数据源下没有找到mysql的驱动
- 【公益译文】网络钓鱼风险(攻击带来的损失)
- Word Break问题及解法
- Github搭建详细解说!
- Android护眼模式功能小记