【线段树区间求和裸题】poj 3468 A Simple Problem with Integers
来源:互联网 发布:unity3d lua 编辑:程序博客网 时间:2024/06/17 15:05
【线段树区间求和裸题】poj 3468 A Simple Problem with Integers
【poj链接】
【vj链接】
题目大意
第一行输入n和q
第二行n个位置的初始值
接下来q 行
首字母为‘Q’ ,跟两个数x,y 输出[x,y]区间数的和
首字母为‘C’ ,跟三个数x,y,z 把[x,y]区间里每个数加上z
Sample Input
10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
Sample Output
455915
解题思路
线段树求和,lazy标记
AC代码
#include <cstdio>#include <iostream>#include <algorithm>using namespace std;#define LL long longconst LL maxn=100000+100;struct p{ LL l,r,sum;}tree[maxn<<2];LL lazy[maxn<<2];LL cnt;LL a[maxn];void push_down(LL i){ lazy[i*2]+=lazy[i]; lazy[i*2+1]+=lazy[i]; tree[i].sum+=(tree[i].r-tree[i].l+1)*lazy[i]; lazy[i]=0;}void build(LL i,LL l,LL r){ tree[i].l=l; tree[i].r=r; tree[i].sum=0; if(l==r) { tree[i].sum=a[++cnt]; return; } LL mid=(l+r)>>1; build(i*2,l,mid); build(i*2+1,mid+1,r); tree[i].sum=tree[i*2].sum+tree[i*2+1].sum;}void update(LL i,LL l,LL r,LL v){ if(tree[i].l==l&&tree[i].r==r) { lazy[i]+=v; return; } tree[i].sum+=(r-l+1)*v; if(lazy[i]!=0) {//printf("YYYYYYYYYYYYYY"); push_down(i); } LL mid=(tree[i].l+tree[i].r)>>1; if(r<=mid) update(i*2,l,r,v); else if(l>mid) update(i*2+1,l,r,v); else { update(i*2,l,mid,v); update(i*2+1,mid+1,r,v); } //tree[i].sum=tree[i*2].sum+tree[i*2+1].sum;}LL q(LL i,LL l,LL r){ if(tree[i].l==l&&tree[i].r==r) { return tree[i].sum+(r-l+1)*lazy[i]; } if(lazy[i]!=0) { push_down(i); } LL mid=(tree[i].l+tree[i].r)>>1; if(r<=mid) {return q(i*2,l,r);} else if(l>mid) { return q(i*2+1,l,r);} else { return q(i*2,l,mid)+q(i*2+1,mid+1,r); }}int main(){ char s[5]; LL n,Q,i,x,y,z; scanf("%lld%lld",&n,&Q); for(i=1;i<=n;i++) scanf("%lld",&a[i]); cnt=0; build(1,1,n); while(Q--) { scanf("%s",s); if(s[0]=='C') { scanf("%lld%lld%lld",&x,&y,&z); update(1,x,y,z); } else if(s[0]=='Q') { scanf("%lld%lld",&x,&y); printf("%lld\n",q(1,x,y)); } } return 0;}
阅读全文
0 0
- 【线段树区间求和裸题】poj 3468 A Simple Problem with Integers
- Poj 3468 A Simple Problem with Integers (线段树 区间更新 区间求和)
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
- 20140719 「线段树 - 区间更新,区间求和」 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 3468A Simple Problem with Integers 线段树 区间修改_求和
- Linux多线程编程讲解之系列五
- GitHub 部分使用命令
- Java实现哈夫曼树及简易编码解码
- Fiddler抓包10-会话框添加查看get与post请求类型
- 对象的动态特性
- 【线段树区间求和裸题】poj 3468 A Simple Problem with Integers
- 1122: 小明的调查作业
- tomcat项目部署去掉项目名
- ftl基础语法
- MAVEN 私服推送本地jar包
- c++和python string 的find()方法的返回值不同
- Python easyui
- QQ oauth原理
- 声明式编程和命令式编程