Coder
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3567 Accepted Submission(s): 1390
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 < ... < a
k
Can you complete this task (and be then fired)?
------------------------------------------------------------------------------
1 See http://uncyclopedia.wikia.com/wiki/Algorithm
Input
There’re several test cases.
In each test case, the first line contains one integer N ( 1 <= N <= 10
5 ), 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 <= 10
9.
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).
Output
For each operation “sum” please print one line containing exactly one integer denoting the digest sum of the current set. Print 0 if the set is empty.
Sample Input
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
Sample Output
3
4
5
有三种类型的操作,add x表示往集合里添加数x,del x表示将集合中数x删除,sum求出从小到大排列的集合中下标模5为3的数的和(下标从1开始)。集合中的数都是唯一的。
离线处理,val有10^9,离散化一下,按val顺序从1-N编号就只有10^5,插入线段树中对应编号的位置。用cnt[o]表示当前区间的数的个数,mod[o][i]表示当前区间的数从1开始编号mod5余i的和。那么当o不为叶子的时候有mod[o][i]=mod[o<<1][i]+mod[o<<1|1][(((i-cnt[o<<1])%5)+5)%5]。遇见sum就输出mod[1][3]。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<map>
using namespace std;
typedef long long LL;
const int MAXN=100010;
const int MAXNODE=4*MAXN;
const int LOGMAXN=50;
const int INF=0x3f3f3f3f;
int N;
map<int,int> mp;
struct OP{
char str[20];
int val,id;
}op[MAXN];
bool cmp1(const OP& a,const OP& b){
return a.val<b.val;
}
bool cmp2(const OP& a,const OP& b){
return a.id<b.id;
}
struct SegmentTree{
int cnt[MAXNODE];
LL mod[MAXNODE][5];
void clear(){
memset(cnt,0,sizeof(cnt));
memset(mod,0,sizeof(mod));
}
void maintain(int o){
cnt[o]=cnt[o<<1]+cnt[o<<1|1];
for(int i=0;i<5;i++) mod[o][i]=mod[o<<1][i]+mod[o<<1|1][(((i-cnt[o<<1])%5)+5)%5];
}
void update(int o,int L,int R,int pos,int val,int cover){
cnt[o]+=cover;
if(L>=R){
if(cnt[o]) mod[o][1]=val;
else mod[o][1]=0;
return;
}
int mid=L+(R-L)/2;
if(pos<=mid) update(o<<1,L,mid,pos,val,cover);
else update(o<<1|1,mid+1,R,pos,val,cover);
maintain(o);
}
}tree;
int main(){
freopen("in.txt","r",stdin);
while(scanf("%d",&N)!=EOF){
for(int i=0;i<N;i++){
scanf("%s",op[i].str);
if(op[i].str[0]!='s') scanf("%d",&op[i].val);
op[i].id=i;
}
sort(op,op+N,cmp1);
mp.clear();
int k=0;
for(int i=0;i<N;i++) if(op[i].str[0]=='a'){
mp[op[i].val]=++k;
}
sort(op,op+N,cmp2);
tree.clear();
for(int i=0;i<N;i++){
if(op[i].str[0]=='a') tree.update(1,1,k,mp[op[i].val],op[i].val,1);
else if(op[i].str[0]=='d') tree.update(1,1,k,mp[op[i].val],op[i].val,-1);
else printf("%I64d\n",tree.mod[1][3]);
}
}
return 0;
}