题意:
一个序列,Add 操作加入一个数,保持有序
Del 操作删除一个数,保持有序
Sum操作求 i % 5 = 3 的和
维护线段树,move数组代表偏移了几位。PushUp那里是重点
/*
* =====================================================================================
*
* Author: KissBuaa.DS(AC)
* Company: BUAA-ACMICPC-Group
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <stdbool.h>
#include <math.h>
#define LL __int64
#define CLR(x) memset(x,0,sizeof(x))
#define typec double
#define sqr(x) ((x)*(x))
#define abs(x) ((x)<0?(-(x)):(x))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-(x)))
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define inf 100000000
//For C++
#include <cctype>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <list>
#include <iostream>
using namespace std;
const double eps=1e-10;
int dblcmp(typec d) {
if (fabs(d)<eps)
return 0;
return (d>0)?1:-1;
}
int n;
const int N = 111111;
LL tree[N << 2][5] ;
LL move[N << 2];
char str[10];
struct OP{
LL op ;
LL x;
void input(){
scanf("%s",str);
if (str[0]=='s') {op=0,x=0;}
else if (str[0]=='a'){
op = 1;
scanf("%I64d",&x);
}
else if (str[0] == 'd'){
op = -1;
scanf("%I64d",&x);
}
}
}io[N];
LL num[N];
int tot;
void PushUp(int rt){
for (int i = 0 ; i < 5 ; ++ i){
tree[rt][i] = tree[rt << 1][i] + tree[rt << 1 | 1][(i - move[rt << 1] + 15)%5];
}
}
void build(int l ,int r ,int rt){
move[rt]=0;
if (l == r){
for (int i = 0 ; i < 5 ; ++i)
tree[rt][i] = 0;
return;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int p , LL c , int l , int r, int rt){
move[rt] = (move[rt] + c + 10) % 5;
if (l == r){
if (c == -1) tree[rt][0] = 0;
else tree[rt][0] = num[p - 1];
return ;
}
int m = (l + r) >> 1;
if (p <= m) update(p , c , lson);
else update(p , c , rson);
PushUp(rt);
}
void solve(){
tot = 0;
for (int i = 0 ; i < n ; ++i){
io[i].input();
if (io[i].op){
num[tot ++] = io[i].x;
}
}
sort(num , num + tot);
tot = unique(num , num + tot ) - num;
build(1 , tot , 1);
for (int i = 0 ; i < n ; ++i){
if (io[i].op == 0) printf("%I64d\n",tree[1][2]);
else{
int pos = lower_bound(num , num + tot , io[i].x) - num + 1 ;
//printf("POS: %d\n",pos);
update(pos , io[i].op , 1 , tot , 1);
}
}
}
int main(){
while (~scanf("%d",&n)) solve();
}