poj 3468 A Simple Problem with Integers
2013-08-02 15:25
288 查看
链接:点击打开链接
有N个数,A1~An,有两种操作,C A B C代表A到B区间所有的数都加C,Q A B是求A到B区间的和,是一道简单的成段更新,懒惰记忆。记得用long long就行
有N个数,A1~An,有两种操作,C A B C代表A到B区间所有的数都加C,Q A B是求A到B区间的和,是一道简单的成段更新,懒惰记忆。记得用long long就行
#include<iostream> #include<cstring> #include<cstdio> using namespace std; #define N 100010 struct node{ int l; int r; long long sum; long long num; }anode[4*N]; int data ; void bulid(int l,int r,int n){ int mid; mid=(l+r)>>1; anode .num=0; if(l==r){ anode .l=l; anode .r=r; anode .sum=data[l]; } else{ anode .l=l; anode .r=r; bulid(l,mid,2*n); bulid(mid+1,r,2*n+1); anode .sum=anode[2*n].sum+anode[2*n+1].sum; } } long long query(int l,int r,int n,long long m){ int mid; mid=(anode .l+anode .r)>>1; if(anode .l==l&&anode .r==r){ return anode .sum+(r-l+1)*m; } if(r<=mid) return query(l,r,2*n,m+anode .num); else if(l>mid) return query(l,r,2*n+1,m+anode .num); else return query(l,mid,2*n,m+anode .num)+query(mid+1,r,2*n+1,m+anode .num); } void update(int l,int r,int n,int m){ int mid; mid=(anode .l+anode .r)>>1; anode .sum+=(r-l+1)*m; if(anode .l==l&&anode .r==r){ anode .num+=m; return; } if(r<=mid) update(l,r,2*n,m); else if(l>mid) update(l,r,2*n+1,m); else{ update(l,mid,2*n,m); update(mid+1,r,2*n+1,m); } } int main(){ int n,q,a,b,c,i; char str; while(~scanf("%d %d",&n,&q)){ for(i=1;i<=n;i++) scanf("%d",&data[i]); bulid(1,n,1); //getchar(); while(q--){ getchar(); scanf("%c",&str); if(str=='Q'){ scanf("%d %d",&a,&b); printf("%lld\n",query(a,b,1,0)); } else if(str=='C'){ scanf("%d %d %d",&a,&b,&c); update(a,b,1,c); } } } 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(区间更新,求和)-splay或线段树
- 【bzoj 3212】【POJ 3468】A Simple Problem with Integers(线段树)
- POJ 3468 A Simple Problem with Integers(线段树)
- 线段树 poj 3468 A Simple Problem with Integers 区间add更新
- 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 基础线段树