poj 3468 A Simple Problem with Integer
来源:互联网 发布:上瘾网络剧15集未减版 编辑:程序博客网 时间:2024/05/17 08:14
线段树的区间更新与区间查询。
还是基础的线段树题…
多练练基础吧。。。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define mid ((l+r)>>1)#define lson (rt<<1)#define rson (rt<<1|1)typedef long long ll;const int MAXN=100010;ll sum[MAXN*4],col[MAXN*4];void PushUp(int rt){ sum[rt]=sum[lson]+sum[rson];}void PushDown(int rt,int m){ if(col[rt]){ col[lson]+=col[rt]; col[rson]+=col[rt]; sum[lson]+=(m-(m>>1))*col[rt]; sum[rson]+=(m>>1)*col[rt]; col[rt]=0; }}void build(int l,int r,int rt){ col[rt]=0; if(l==r){ scanf("%I64d",&sum[rt]); return ; } build(l,mid,lson); build(mid+1,r,rson); PushUp(rt);}void update(int L,int R,int c,int l,int r,int rt){ if(L<=l && r<=R){ col[rt]+=c; sum[rt]+=(ll)(r-l+1)*c; return ; } PushDown(rt,r-l+1); if(L<=mid) update(L,R,c,l,mid,lson); if(R>mid) update(L,R,c,mid+1,r,rson); PushUp(rt);}ll query(int L,int R,int l,int r,int rt){ if(L<=l && r<=R) return sum[rt]; PushDown(rt,r-l+1); ll ret=0; if(L<=mid) ret+=query(L,R,l,mid,lson); if(R>mid) ret+=query(L,R,mid+1,r,rson); return ret;}int main(){ int n,m; scanf("%d%d",&n,&m); build(1,n,1); char op[2]; int a,b,c; while(m--){ scanf("%s",op); if(op[0]=='C'){ scanf("%d%d%d",&a,&b,&c); update(a,b,c,1,n,1); } else{ scanf("%d%d",&a,&b); printf("%I64d\n",query(a,b,1,n,1)); } } return 0;}
- poj 3468 A Simple Problem with Integer
- 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
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- C#中unsafe的使用
- : The response of the WebApplicationException cannot be utilized as the response is already committe
- c# winform实现网页上用户自动登陆(破解)
- Oracle的在线重定义表功能
- 做手机应用,免费反而比收费更挣钱?
- poj 3468 A Simple Problem with Integer
- Mondrian
- Oracle DB2
- Intent 调用系统中经常用到的组件
- SQL Server 树型结构查询指定节点 SQL Server 树型结构查询的排序
- tools used by various tasks
- 从一组数中找到第二大的数/从一组数中找到不小于第二大数的数字的个数
- C++及C中的 string char指针及char数组
- HTML5究竟好在哪儿