hdu 4288 Coder

本文从WordPress迁移而来, 查看全部WordPress迁移文章

线段树

和 codeforces Sum of Medians 一样的题目 详细分析请在博客搜索

离线处理所有操作,离散化,利用模运算的性质来合并区间

单点更新,区间查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
#define N 100010
#define LL long long
#define cl(xx,yy) memset((xx),(yy),sizeof((xx)))
#define lson(i) ((i)<<1)
#define rson(i) ((i)<<1|1)

struct OP{
char op[10];
LL val;
}m[N];
struct SegTree{
int l,r,mid,cnt;
LL sum[5];
SegTree(){}
SegTree(int __l,int __r){
l = __l; r = __r; mid = (l+r)>>1;
cnt = 0; cl(sum,0);
}
}t[N<<2];
LL Hash[N];
map<LL,int>M;

void build(int rt,int a,int b){
t[rt] = SegTree(a,b);
if(a == b) return ;
int mid = t[rt].mid;
build(lson(rt),a,mid);
build(rson(rt),mid+1,b);
}

void PushUp(int rt){
cl(t[rt].sum,0);
t[rt].cnt = t[lson(rt)].cnt + t[rson(rt)].cnt;
int Lcnt = t[lson(rt)].cnt;
for(int i = 0; i < 5; i++){
t[rt].sum[i] += t[lson(rt)].sum[i];
t[rt].sum[(i+Lcnt)%5] += t[rson(rt)].sum[i];
}
}

void updata(int rt,int index,LL val){
if(t[rt].l == t[rt].r){
t[rt].sum[1] = val;
t[rt].cnt = (val == 0 ? 0 : 1);
return ;
}
int mid = t[rt].mid;
if(index <= mid)
updata(lson(rt),index,val);
else
updata(rson(rt),index,val);
PushUp(rt);
}

int main(){
int n,tot;
while(scanf("%d",&n)!=EOF){
M.clear(); tot = 0;
for(int i = 1; i <= n; i++){
scanf("%s",m[i].op);
if(m[i].op[0] != 's')
scanf("%I64d",&m[i].val);
if(m[i].op[0] == 'a')
Hash[++tot] = m[i].val;
}
sort(Hash+1,Hash+tot+1);
for(int i = 1; i <= tot; i++)
M[Hash[i]] = i;
build(1,1,tot);
for(int i = 1; i <= n; i++){
if(m[i].op[0] == 's'){
printf("%I64d\n",t[1].sum[3]);
}
else{
int index = M[m[i].val];
LL val = (m[i].op[0] == 'a' ? m[i].val : 0);
updata(1,index,val);
}
}
}
return 0;
}