2015
05-23

# Coder

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:
2. del x �C remove the element x from the set;
3. sum �C find the digest sum of the set. The digest sum should be understood by

where the set S is written as {a1, a2, … , ak} satisfying a1 < a2 < a3 < … < ak
Can you complete this task (and be then fired)?
——————————————————————————
1 See http://uncyclopedia.wikia.com/wiki/Algorithm

There’re several test cases.
In each test case, the first line contains one integer N ( 1 <= N <= 105 ), the number of operations to process.
Then following is n lines, each one containing one of three operations: “add x” or “del x” or “sum”.
You may assume that 1 <= x <= 109.
Please see the sample for detailed format.
For any “add x” it is guaranteed that x is not currently in the set just before this operation.
For any “del x” it is guaranteed that x must currently be in the set just before this operation.
Please process until EOF (End Of File).

There’re several test cases.
In each test case, the first line contains one integer N ( 1 <= N <= 105 ), the number of operations to process.
Then following is n lines, each one containing one of three operations: “add x” or “del x” or “sum”.
You may assume that 1 <= x <= 109.
Please see the sample for detailed format.
For any “add x” it is guaranteed that x is not currently in the set just before this operation.
For any “del x” it is guaranteed that x must currently be in the set just before this operation.
Please process until EOF (End Of File).

9
sum
del 3
sum
6
sum

3
4
5
HintC++ maybe run faster than G++ in this problem. 

#include<iostream>
#include<algorithm>
using namespace std;

#define MAXN 100010
#define lson u<<1
#define rson u<<1|1
typedef long long LL;

int dat[MAXN],tmp[MAXN];
char cmd[MAXN][5];

struct Node{
int lef,rig;
LL sum[5];
int cnt;
}T[MAXN<<2];

void Build(int u,int l,int r){
T[u].lef=l;
T[u].rig=r;
T[u].cnt=0;
for(int i=0;i<5;i++)T[u].sum[i]=0;
if(l==r)return;
int mid=(l+r)>>1;
Build(lson,l,mid);
Build(rson,mid+1,r);
}

void PushUp(int u){
for(int i=0;i<5;i++)T[u].sum[i]=T[lson].sum[i]+T[rson].sum[((i-T[lson].cnt)%5+5)%5];
}

void Update(int u,int pos,int val,int op){
T[u].cnt+=op;
if(T[u].lef==T[u].rig){
T[u].sum[0]+=val;return;
}
else {
if(pos<=T[lson].rig)Update(lson,pos,val,op);
else Update(rson,pos,val,op);
PushUp(u);
}
}

int main(){
int n;
while(scanf("%d",&n)==1){

int num=0;
for(int i=0;i<n;i++){
scanf("%s",cmd[i]);
if(cmd[i][0]!='s'){scanf("%d",&dat[i]);tmp[num++]=dat[i];}
}

sort(tmp,tmp+num);
num=unique(tmp,tmp+num)-tmp;

Build(1,1,num);
for(int i=0;i<n;i++){
int pos=lower_bound(tmp,tmp+num,dat[i])-tmp;
if(cmd[i][0]=='a')Update(1,pos,dat[i],1);
else if(cmd[i][0]=='d')Update(1,pos,-dat[i],-1);
else printf("%I64d\n",T[1].sum[2]);
}
}

}