hdu 4288 Coder(树形结构-线段树)

2015-07-20 17:45:39 · 作者: · 浏览: 4

Coder

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3187 Accepted Submission(s): 1258


Problem Description   In mathematics and computer science, an algorithm describes a set of procedures or instructions that define a procedure. The term has become increasing popular since the advent of cheap and reliable computers. Many companies now employ a single coder to write an algorithm that will replace many other employees. An added benefit to the employer is that the coder will also become redundant once their work is done. 1
  You are now the signle coder, and have been assigned a new task writing code, since your boss would like to replace many other employees (and you when you become redundant once your task is complete).
Your code should be able to complete a task to replace these employees who do nothing all day but eating: make the digest sum.
  By saying “digest sum” we study some properties of data. For the sake of simplicity, our data is a set of integers. Your code should give response to following operations:
  1. add x ? add the element x to the set;
  2. del x ? remove the element x from the set;
  3. sum ? find the digest sum of the set. The digest sum should be understood by
\

  where the set S is written as {a 1, a 2, ... , a k} satisfying a 1 < a 2 < a 3<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vc3ViPiA8IC4uLiA8IGE8c3ViPms8L3N1Yj4gPGJyPgqhoaGhQ2FuIHlvdSBjb21wbGV0ZSB0aGlzIHRhc2sgKGFuZCBiZSB0aGVuIGZpcmVkKT88YnI+Ci0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTxicj4KPHN1cD4xPC9zdXA+IFNlZSBodHRwOi8vdW5jeWNsb3BlZGlhLndpa2lhLmNvbS93aWtpL0FsZ29yaXRobQoKIAo8YnI+CgpJbnB1dAoKoaGhoVRoZXJloa9yZSBzZXZlcmFsIHRlc3QgY2FzZXMuPGJyPgqhoaGhSW4gZWFjaCB0ZXN0IGNhc2UsIHRoZSBmaXJzdCBsaW5lIGNvbnRhaW5zIG9uZSBpbnRlZ2VyIE4gKCAxIDw9IE4gPD0gMTA8c3VwPjU8L3N1cD4gKSwgdGhl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than G++ in this problem. 
这题线段树有点分治的感觉,先离散化,从小到大作为线段树叶子节点。下面举个例子就明白了,看图:\



#include 
    
     
#include 
     
       #include 
       #include 
       
         using namespace std; #define ll long long const int maxn = 100010; struct tree{ int l , r , sum; ll mod[5]; }a[4*maxn]; struct OP{ char op; int x; OP(char o = 'a' , int a = 0){ op = o , x = a; } }; int N , cnt , value[maxn]; map
        
          mp; vector
         
           operate; void pushup(int k){ a[k].sum = a[2*k].sum+a[2*k+1].sum; int x = a[2*k].sum%5; for(int i = 0; i < 5; i++){ a[k].mod[(x+i)%5] = a[2*k].mod[(x+i)%5]+a[2*k+1].mod[i]; } } void build(int l , int r , int k){ a[k].l = l; a[k].r = r; a[k].sum = 0; for(int i = 0; i < 5; i++) a[k].mod[i] = 0; if(l != r){ int mid = (l+r)/2; build(l ,mid , 2*k); build(mid+1 , r , 2*k+1); } } void update(int l , int r , int k , int c){ if(l <= a[k].l && a[k].r <= r){ if(c < 0) a[k].sum -= 1; else a[k].sum += 1; a[k].mod[1] += c; }else{ int mid = (a[k].l+a[k].r)/2; if(mid >= r) update(l , r , 2*k , c); else update(l , r , 2*k+1 , c); pushup(k); } } void initial(){ mp.clear(); cnt = 0; operate.clear(); } void readcase(){ char op[10]; int x; for(int i = 0; i < N; i++){ scanf("%s" , op); if(op[0] == 'a' || op[0] == 'd'){ scanf("%d" , &x); operate.push_back(OP(op[0] , x)); mp[x] = 0; }else operate.push_back(OP(op[0] , -1)); } for(map
          
           ::iterator it = mp.begin(); it != mp.end(); it++){ it->second = cnt; value[cnt++] = it->first; } } void computing(){ build(0 , cnt-1 , 1); for(int i = 0; i < N; i++){ char op = operate[i].op; int x = operate[i].x; if(op == 'a') update(mp[x] , mp[x] , 1 , x); else if(op == 'd') update(mp[x] , mp[x] , 1 , -1*x); else{ printf("%I64d\n" , a[1].mod[3]); } } } int main(){ while(~scanf("%d" , &N)){ initial(); readcase(); computing(); } return 0; }