标签:blog for nod null getchar [1] return build vector
#include <bits/stdc++.h> using namespace std; struct Node{ int l,r,n; Node *son[2]; }; int a[10005]; vector<Node*> root; void build(Node *&o,int l,int r){ int mid=(l+r)>>1; o=new Node; o->l=l;o->r=r; if(l==r){ o->son[0]=o->son[1]=NULL; o->n=a[l]; return; } build(o->son[0],l,mid); build(o->son[1],mid+1,r); o->n=o->son[0]->n+o->son[1]->n; } void update(Node *&o,int w,int n){ int mid=(o->l+o->r)>>1; Node *t=new Node; *t=*o; o=t; o->n+=n; if(o->l==o->r) return; if(w>mid) update(o->son[1],w,n); else update(o->son[0],w,n); } int query(Node *o,int l,int r){ if(o==NULL){ puts("WORNG"); getchar();getchar(); } int mid=(o->l+o->r)>>1; if(o->l==l&&o->r==r) return o->n; if(mid>=r) return query(o->son[0],l,r); else if(mid<l) return query(o->son[1],l,r); else return query(o->son[0],l,mid)+query(o->son[1],mid+1,r); } int main(){ Node *now=NULL; int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; build(now,1,n); while(1){ int a; cin>>a; if(a==1){ int w,n; cin>>w>>n; root.push_back(now); update(now,w,n); } if(a==2){ int l,r; cin>>l>>r; cout<<query(now,l,r)<<endl; } if(a==3){ int n; cin>>n; now=root[n]; } } return 0; }
标签:blog for nod null getchar [1] return build vector
原文地址:http://www.cnblogs.com/HC-LittleJian/p/7691933.html