「BZOJ-2631」tree-Link-Cut Tree

一棵n个点的树,每个点的初始权值为1。对于这棵树有q个操作,每个操作为以下四种操作之一:

+ $u$ $v$ $c$:将u到v的路径上的点的权值都加上自然数c;

- $u_1$ $v_1$ $u_2$ $v_2$:将树中原有的边 $(u_1,v_1)$ 删除,加入一条新边 $(u_2,v_2)$,保证操作完之后仍然是一棵树;

* $u$ $v$ $c$:将 $u$ 到 $v$ 的路径上的点的权值都乘上自然数 $c$;

/ $u$ $v$:询问 $u$ 到 $v$ 的路径上的点的权值和,求出答案对于 $51061$ 的余数。

链接

BZOJ-2631

题解

模板题,还需要题解?
注意: 此题只需要 unsigned int 不需要 long long

论三目运算符与 ifelse 的常数差距……直接拿下了BZOJ rank1

经过三道题,看来我的 LCT 常数确实较小。

代码

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* created by xehoth on 21-03-2017
*/
#include <bits/stdc++.h>

typedef unsigned int uint;

const int MAXN = 100010;
const int MOD = 51061;

struct Node *null;

struct Node {
Node *c[2], *fa;
bool rev;
Node *top;
uint sum, add, mul, size, val;

Node() : mul(1), sum(1), val(1), size(1), fa(null) {
c[0] = c[1] = null;
}

inline void cover(int m, int a) {
val = (val * m + a) % MOD;
sum = (sum * m + a * size) % MOD;
add = (add * m + a) % MOD;
mul = (mul * m) % MOD;
}

inline void maintain() {
sum = (c[0]->sum + c[1]->sum + val) % MOD;
size = (c[0]->size + c[1]->size + 1) % MOD;
}
inline void reverse() {
rev ^= 1, std::swap(c[0], c[1]);
}

inline void pushDown() {
rev ? c[0]->reverse(), c[1]->reverse(), rev = false : 0;
mul != 1 || add != 0 ? c[0]->cover(mul, add),
c[1]->cover(mul, add), mul = 1, add = 0 : 0;
}


inline bool relation() {
return this == fa->c[1];
}

inline void rotate(bool f) {
Node *o = fa;
top = o->top;
o->pushDown(), pushDown();
(fa = o->fa)->c[o->relation()] = this;
(o->c[f] = c[!f])->fa = o;
(c[!f] = o)->fa = this;
o->maintain();
}
/*
我也不想这么写,
我也很绝望啊,
然而三目运算符常数小啊......
*/
inline void splay() {
Node *o = fa;
bool f;
for (pushDown(); o != null; o = fa) {
o->fa == null ? rotate(o->c[1] == this) :
((f = o->c[1] == this) == (o->fa->c[1] == o)
? (o->rotate(f), rotate(f)) : (rotate(f), rotate(!f)));
}
maintain();
}

inline void expose(Node *p = null) {
splay();
if (c[1] != null)
c[1]->top = this, c[1]->fa = null;
(c[1] = p)->fa = this;
maintain();
}

inline Node *access() {
Node *x = this;
for (x->expose(); x->top; x = x->top)
x->top->expose(x);
return x;
}

inline void evert() {
access(), splay(), reverse();
}

inline void link(Node *f) {
Node *x = access();
x->reverse(), x->top = f;
}

inline void cut(Node *y) {
Node *x = this;
x->expose(), y->expose();
if (x->top == y) x->top = NULL;
if (y->top == x) y->top = NULL;
}

inline Node *findRoot() {
Node *f = this;
f->access(), f->splay();
while (f->pushDown(), f->c[0] != null) f = f->c[0];
f->splay();
return f;
}

inline void split(Node *v) {
v->evert(), access(), splay();
}
} pool[MAXN];

inline void init() {
null = pool, null->fa = null;
null->sum = null->val = null->size = null->add = 0;
null->mul = 1;
}

inline char nextChar() {
static const int IN_LEN = 1000000;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}

inline int read() {
static int x = 0;
static char c;
for (x = 0, c = nextChar(); !isdigit(c); c = nextChar());
for (; isdigit(c); c = nextChar())
x = (x + (4 * x) << 1) + (c ^ '0');
return x;
}

const int OUT_LEN = 10000000;
char obuf[OUT_LEN], *oh = obuf;

template<class T>
inline void print(T x) {
static int buf[30], cnt;
if (x == 0) {
*oh++ = '0';
} else {
if (x < 0) *oh++ = '-', x = -x;
register int cnt = 0;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) *oh++ = buf[cnt--];
}
}

template<class T>
inline void println(T x) {
print(x), *oh++ = '\n';
}

inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}

int n, q;

int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
init();
n = read(), q = read();
for (register int i = 1; i <= n; i++) pool[i] = Node();
char s;
register int x, y, z;
for (register int i = 1; i < n; i++) (pool + read())->link(pool + read());
for (register int i = 1; i <= q; i++) {
s = nextChar(), x = read(), y = read();
switch (s) {
case '+':
z = read(), (pool + x)->split(pool + y);
(pool + x)->cover(1, z);
break;
case '-':
(pool + x)->cut(pool + y),
x = read(), y = read(),
(pool + x)->link(pool + y);
break;
case '*':
z = read(), (pool + x)->split(pool + y);
(pool + x)->cover(z, 0);
break;
case '/':
(pool + x)->split(pool + y), println((pool + x)->sum);
break;
}
}
flush();
return 0;
}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×