<span style="font-family: 'Times New Roman'; font-size: 12px;">Coder</span>
9 add 1 add 2 add 3 add 4 add 5 sum add 6 del 3 sum 6 add 1 add 3 add 5 add 7 add 9 sum
3 4 5HintC++ maybe run faster than G++ in this problem.这题线段树有点分治的感觉,下面举个例子就明白了,看图:#include <iostream> #include <cstdio> #include <map> #include <vector> 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<int , int> mp; vector<OP> 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<int , int>::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; }
原文地址:http://blog.csdn.net/u011836218/article/details/39031181