poj 3468 线段树成段更新
2012-10-11 16:27
417 查看
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<vector> #include<sstream> #include<string> #include<climits> #include<stack> #include<set> #include<bitset> #include<cmath> #include<deque> #include<map> #include<queue> #define iinf 2000000000 #define linf 1000000000000000000LL #define dinf 1e200 #define eps 1e-5 #define all(v) (v).begin(),(v).end() #define sz(x) x.size() #define pb push_back #define mp make_pair #define lng long long #define sqr(a) ((a)*(a)) #define pii pair<int,int> #define pll pair<lng,lng> #define pss pair<string,string> #define pdd pair<double,double> #define X first #define Y second #define pi 3.14159265359 #define ff(i,xi,n) for(int i=xi;i<=(int)(n);++i) #define ffd(i,xi,n) for(int i=xi;i>=(int)(n);--i) #define ffl(i,r) for(int i=head[r];i!=-1;i=edge[i].next) #define cc(i,j) memset(i,j,sizeof(i)) #define two(x) ((lng)1<<(x)) #define N 100002 #define M 1000000 #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1 | 1 #define Mod n #define Pmod(x) (x%Mod+Mod)%Mod using namespace std; typedef vector<int> vi; typedef vector<string> vs; typedef unsigned int uint; typedef unsigned lng ulng; template<class T> inline void checkmax(T &x,T y){if(x<y) x=y;} template<class T> inline void checkmin(T &x,T y){if(x>y) x=y;} template<class T> inline T Min(T x,T y){return (x>y?y:x);} template<class T> inline T Max(T x,T y){return (x<y?y:x);} template<class T> T gcd(T a,T b){return (a%b)==0?b:gcd(b,a%b);} template<class T> T lcm(T a,T b){return a*b/gcd(a,b);} template<class T> T Abs(T a){return a>0?a:(-a);} template<class T> inline T lowbit(T n){return (n^(n-1))&n;} template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));} template<class T> inline bool isPrimeNumber(T n) {if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;} int n,q; lng sum[N<<2],add[N<<2]; inline void pushup(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } inline void pushdown(int rt,lng len) { if(add[rt]) { add[rt<<1]+=add[rt],add[rt<<1|1]+=add[rt]; sum[rt<<1]+=(len-(len>>1))*add[rt]; sum[rt<<1|1]+=(len>>1)*add[rt]; add[rt]=0; } } void update(int L,int R,lng ad,int l,int r,int rt) { if(L<=l&&r<=R) { sum[rt]+=(lng)(r-l+1)*ad; add[rt]+=ad; return ; } pushdown(rt,r-l+1); int mid=(l+r)>>1; if(L<=mid) update(L,R,ad,lson); if(R>mid) update(L,R,ad,rson); pushup(rt); } lng query(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R) { return sum[rt]; } lng res=0; pushdown(rt,r-l+1); int mid=(l+r)>>1; if(L<=mid) res+=query(L,R,lson); if(R>mid) res+=query(L,R,rson); return res; } void build(int l,int r,int rt) { if(l==r) { scanf("%lld",&sum[rt]); return ; } int mid=(l+r)>>1; build(lson); build(rson); pushup(rt); } int main() { while(scanf("%d%d",&n,&q)==2) { char opt; build(1,n,1); ff(i,1,q) { getchar(); scanf("%c",&opt); if(opt=='C') { lng a,b,c; scanf("%lld%lld%lld",&a,&b,&c); update(a,b,c,1,n,1); } else { int a,b; scanf("%d%d",&a,&b); printf("%lld\n",query(a,b,1,n,1)); } } } return 0; }
相关文章推荐
- poj 3468线段树 成段更新
- 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 【线段树 成段更新 求区间和】.cpp
- 线段树(成段更新) 之 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为例)
- POJ 3468:A Simple Problem with Integers(线段树[成段更新])
- POJ 3468 A Simple Problem with Integers(段更新的区间求和&Lazy思想&线段树)