POJ3468

来源:互联网 发布:mac隐藏文件夹 编辑:程序博客网 时间:2024/05/29 06:45
#include<iostream>//思路要详解 ,不能废题 #include<cstring>//A了 #define ll __int64#define maxn  100010using namespace std;struct NOde{ll l,r,ad,value;}tree[maxn*4];void pushup(ll root){tree[root].value=tree[root<<1].value+tree[root<<1|1].value;//cout<<tree[root].value<<endl;} ll a[maxn];void build(ll left,ll right,ll root)//value用于存和 {tree[root].l=left;tree[root].r=right;tree[root].ad=0;if(left==right){tree[root].value=a[left];//cout<<tree[root].value<<endl; return ;}ll mid=(left+right)>>1;build(left,mid,root<<1);build(mid+1,right,root<<1|1);//是这样么????     pushup(root); }void update(ll left,ll right,ll root,ll lc,ll rc,ll data)//维护每个叶子节点所要加的值和value {//cout<<lc<<" "<<rc<<" "<<data<<endl;if(left>=lc&&right<=rc){tree[root].ad+=data;return;}tree[root].value+=(rc-lc+1)*data; //直接更新了哟木有 ll mid=(left+right)/2;if(rc<=mid) {update(left,mid,root*2,lc,rc,data); return;}//记得这里加return; if(lc>=mid+1){ update(mid+1,right,root*2+1,lc,rc,data); return;}else{//cout<<"miao"<<endl;update(left,mid,root*2,lc,mid,data);update(mid+1,right,root*2+1,mid+1,rc,data);return;}}ll query(ll left,ll right,ll root,ll lc,ll rc)//标记往下传后再更新 {//cout<<left<<" "<<right<<endl;     //cout<<lc<<" "<<rc<<endl;if(left==lc&&right==rc){//cout<<tree[root].l<<" "<<tree[root].r<<endl;//cout<<tree[root].value<<" "<<tree[root].ad<<endl;//cout<<tree[root].value+tree[root].ad*(rc-lc+1)<<endl;return tree[root].value+tree[root].ad*(rc-lc+1); }    if(tree[root].ad!=0)    {    tree[root*2].ad+=tree[root].ad;    tree[root*2+1].ad+=tree[root].ad;//value一开始就更过了,不用更了     tree[root].value+=(tree[root].r-tree[root].l+1)*tree[root].ad;     tree[root].ad=0;//update中刚好在范围内的没有更新value值         }ll mid=(left+right)/2;if(rc<=mid) return query(left,mid,root*2,lc,rc);if(lc>=mid+1) return query(mid+1,right,root*2+1,lc,rc);else{return query(left,mid,root*2,lc,mid)+query(mid+1,right,root*2+1,mid+1,rc);}}int main(){ll N,M; scanf("%I64d%I64d",&N,&M);for(ll i=1;i<=N;i++)scanf("%I64d",&a[i]);build(1,N,1);for(ll i=1;i<=M;i++){char judge;cin>>judge;if(judge=='C')    {    int a,b,c;    cin>>a>>b>>c;    update(1,N,1,a,b,c);    }if(judge=='Q'){int a,b;cin>>a>>b;ll result=query(1,N,1,a,b);printf("%I64d\n",result);} } return 0;}

0 0
原创粉丝点击