洛谷 4743 Energy Center

被题意杀了,也许是语文不好……

平衡树维护序列,每个结点上记录每一种属性的值和子树和就好惹。

代码:

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <cstdio>
#include <cstdlib>
#define ls(p) tree[p].lson
#define rs(p) tree[p].rson
using namespace std;
const int N = 1e4;
const int Q = 1e4;
const int M = 200;
int n,m,q;
int p;
struct node
{
int val[M + 5],sum[M + 5];
int rnd,sz;
int lson,rson;
} tree[N + Q + 10];
inline void up(int p)
{
tree[p].sz = tree[ls(p)].sz + 1 + tree[rs(p)].sz;
for(register int i = 0;i < m;++i)
tree[p].sum[i] = tree[ls(p)].sum[i] + tree[p].val[i] + tree[rs(p)].sum[i];
}
void split(int p,int k,int &x,int &y)
{
if(!p)
{
x = y = 0;
return ;
}
if(tree[ls(p)].sz < k)
x = p,split(rs(p),k - tree[ls(p)].sz - 1,rs(p),y);
else
y = p,split(ls(p),k,x,ls(p));
up(p);
}
int merge(int x,int y)
{
if(!x || !y)
return x | y;
if(tree[x].rnd < tree[y].rnd)
{
rs(x) = merge(rs(x),y);
up(x);
return x;
}
else
{
ls(y) = merge(x,ls(y));
up(y);
return y;
}
}
int main()
{
srand(19260817);
scanf("%d%d",&n,&m);
for(register int i = 1;i <= n;++i)
{
int k,x,y;
scanf("%d",&k);
for(register int j = 1;j <= k;++j)
scanf("%d%d",&x,&y),tree[i].val[x] = tree[i].sum[x] = y;
tree[i].rnd = rand(),tree[i].sz = 1;
p = merge(p,i);
}
scanf("%d",&q);
char op[5];
int l,r;
int x,y,z;
while(q--)
{
scanf("%s",op);
if(op[0] == 'I')
{
int k;
scanf("%d%d",&l,&k);
++n;
for(register int i = 1;i <= k;++i)
scanf("%d%d",&x,&y),tree[n].val[x] = tree[n].sum[x] = y;
tree[n].rnd = rand(),tree[n].sz = 1;
split(p,l,x,y);
p = merge(merge(x,n),y);
}
else if(op[0] == 'D')
{
scanf("%d",&l);
split(p,l,x,z);
split(x,l - 1,x,y);
p = merge(x,z);
}
else if(op[1] == 'A')
printf("%d\n",tree[p].sz);
else
{
scanf("%d%d",&l,&r);
split(p,r,x,z);
split(x,l - 1,x,y);
for(register int i = 0;i < m;++i)
printf("%d ",tree[y].sum[i]);
puts("");
p = merge(merge(x,y),z);
}
}
puts("end");
}