题解 P5314 [Ynoi2012]D2T3

题面
%%%lxl
看到题面 怎么做啊???
考虑一种暴力:
每次暴力修改链上的每个点,并每个点维护一颗平衡树
修改时改一下父亲的平衡树

然后考虑优化
把改n个点降低
想到了树剖
平衡树中只维护轻儿子的权值
重儿子暴力查
单次$log^2$

再考虑优化
首先是链加
用树上的差分(口胡的)把链加,单点查转成单点加,子树查
然后搞出dfs序树状数组维护即可
$log^2->log$
查询是一个log的,考虑搞多一点
把每个点的重儿子个数搞多一点,变成x个
这样修改复杂度$->log_2n*log_xn$
询问复杂度$->xlog_2n$
x差不多为7,8的样子

于是理论上能过
但我subtest5全T,指不定是打错了
希望路过大佬帮忙康康
以下仅供参考

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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <bits/stdc++.h>
#define re register
using namespace std;
const int sonsize = 1, inf = 2000000000;

namespace IO
{
#define gc() (iS==iT?(iT=(iS=ibuff)+fread(ibuff,1,SIZ,stdin),(iS==iT?EOF:*iS++)):*iS++)
const int SIZ=1<<21|1;
char *iS,*iT,ibuff[SIZ],obuff[SIZ],*oS=obuff,*oT=oS+SIZ-1,fu[110],c;int fr;
inline void out()
{
fwrite(obuff,1,oS-obuff,stdout);
oS=obuff;
}
template <class T>
inline void read(T &x)
{
x=0;T y=1;
for(c=gc();(c>'9'||c<'0')&&c!='-';c=gc());
c=='-'?y=-1:x=(c&15);
for(c=gc();c>='0'&&c<='9';c=gc()) x=x*10+(c&15);
x*=y;
}
template <class T>
inline void print(T x,char text='\n')
{
if(x<0) *oS++='-',x*=-1;
if(x==0) *oS++='0';
while(x) fu[++fr]=x%10+'0',x/=10;
while(fr) *oS++=fu[fr--];
*oS++=text;out();
}
}
using IO::read;
using IO::print;

pair<int, int> ed[2000010];
int buf[2000010];
int *son[1000010], cnt[1000010];
int n, m;

inline void build_tree() {
int len = (n - 1) << 1;
sort(ed, ed + len);
for(int i = 0; i < len; i++) buf[i] = ed[i].second;
for(re int l = 0, r = 0; l < len; l = r = r + 1) {
for(; r < len && ed[r].first == ed[r + 1].first; r++);
son[ed[l].first] = buf + l;
cnt[ed[l].first] = r - l + 1;
}
}
int dfn[1000010], sze[1000010], lfa[1000010], fa[1000010];
int w[1000010];
int _w[1000010];
inline int cmp(int a, int b) {
return sze[a] > sze[b];
// return rand() % 2;
}
void dfs1(int now, int f) {
fa[now] = f;
sze[now] = 1;
for(re int i = 0; i < cnt[now]; i++) {
if(son[now][i] == f) swap(son[now][i], son[now][cnt[now] - 1]), cnt[now]--;
if(i >= cnt[now]) break;
w[now] -= w[son[now][i]];
dfs1(son[now][i], now);
sze[now] += sze[son[now][i]];
}
sort(son[now], son[now] + cnt[now], cmp);
}
void dfs2(int now, int lf) {
static int dfsnow = 0;
dfn[now] = ++dfsnow;
lfa[now] = lf;
for(re int i = 0; i < sonsize && i < cnt[now]; i++) dfs2(son[now][i], lf);
for(re int i = sonsize; i < cnt[now]; i++) dfs2(son[now][i], son[now][i]);
}
namespace LCA {
int st[2000010][24];
int lg2[2000010], dfn[2000010], dfsnow = 0;
void dfs(int now) {
st[dfn[now] = ++dfsnow][0] = now;
for(re int i = 0; i < cnt[now]; i++) {
dfs(son[now][i]);
st[++dfsnow][0] = now;
}
}
inline int _max(re int a, re int b) {
return dfn[a] < dfn[b] ? a : b;
}
inline void build() {
for(int i = dfsnow; i > 0; i--)
for(int j = 1; i + (1 << j) - 1 <= dfsnow; j++)
st[i][j] = _max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
lg2[0] = -1;
for(int i = 1; i <= dfsnow; i++) lg2[i] = lg2[i >> 1] + 1;
}
inline int get_lca(re int a, re int b) {
a = dfn[a], b = dfn[b];
if(a > b) swap(a, b);
int k = lg2[b - a + 1];
return _max(st[a][k], st[b - (1 << k) + 1][k]);
}
};


struct data{
int l, r, size, val, rd;
} d[1000010];
int stk[1000010], tot, _top;
inline int newdata(re int v){
int ans;
if(_top) ans = stk[--_top];
else ans = ++tot;
d[ans].val = v;
d[ans].l = d[ans].r = 0;
d[ans].size = 1;
d[ans].rd = rand();
return ans;
}
inline void deldata(re int x) {
stk[_top++] = x;
}
class Tree {
public:
#define updata(p) d[p].size = d[d[p].l].size + d[d[p].r].size + 1
inline void lro(int& p) {
int q = p;
p = d[p].r;
d[q].r = d[p].l;
d[p].l = q;
updata(p), updata(q);
}
inline void rro(int& p) {
int q = p;
p = d[p].l;
d[q].l = d[p].r;
d[p].r = q;
updata(p), updata(q);
}
inline Tree() {root = 0;}
void insert(re int val, int &p) {
if(p == 0)
p = newdata(val);
else if(val < d[p].val){
insert(val, d[p].l);
if(d[d[p].l].rd < d[p].rd) rro(p);
}
else{
insert(val, d[p].r);
if(d[d[p].r].rd < d[p].rd) lro(p);
}
updata(p);
}
void del(re int val, int &p) {
if(p == 0) return;
if(d[p].val == val){
if(d[p].l != 0 && d[p].r != 0) {
if(d[d[p].l].rd < d[d[p].r].rd) rro(p), del(val, d[p].r);
else lro(p), del(val, d[p].l);
updata(p);
}
else {
deldata(p);
p = d[p].l + d[p].r;
}
}
else {
if(d[p].val > val) del(val, d[p].l);
else del(val, d[p].r);
updata(p);
}
}
inline int kth(re int k){
re int p = root;
while(1){
if(p == 0) return EOF;
if(k > d[d[p].l].size + 1) k -= d[d[p].l].size + 1, p = d[p].r;
else if(k <= d[d[p].l].size) p = d[p].l;
else return d[p].val;
}
}
inline int get_rank(re int val) {
re int ans = 1;
re int p = root;
while(1) {
if(p == 0) return ans;
if(val <= d[p].val) p = d[p].l;
else ans += d[d[p].l].size + 1, p = d[p].r;
}
}
inline void insert(re int val) {
insert(val, root);
}
inline void remove(re int val) {
del(val, root);
}
private:
int root;
};

#define lowbit(x) ((x) & (-(x)))
int c[1000010];
inline int get(re int x) {
int ans = 0;
for(; x; x -= lowbit(x)) ans += c[x];
return ans;
}
inline void modify(re int x, re int k) {
for(; x <= n; x += lowbit(x)) c[x] += k;
}
inline int getw(re int x) {
return get(dfn[x] + sze[x] - 1) - get(dfn[x] - 1);
}
Tree tree[1000010];
inline void modify(re int x, re int y, re int k) {
int lca = LCA :: get_lca(x, y);
modify(dfn[x], k);
modify(dfn[y], k);
modify(dfn[lca], -k);
if(fa[lca]) modify(dfn[fa[lca]], -k);
while(lfa[x] != lfa[y]) {
if(dfn[lfa[x]] < dfn[lfa[y]]) swap(x, y);
x = lfa[x];
tree[fa[x]].remove(_w[x]);
_w[x] += k;
tree[fa[x]].insert(_w[x]);
x = fa[x];
}
if(dfn[x] > dfn[y]) swap(x, y);
if(x == lfa[x] && fa[x]) {
tree[fa[x]].remove(_w[x]);
_w[x] += k;
tree[fa[x]].insert(_w[x]);
}
}
int _tmp[15], _tot;
inline int solve(re int x, re int k) {
_tot = 0;
for(re int i = 0; i < cnt[x] && i < sonsize; i++)
_tmp[_tot++] = getw(son[x][i]);
_tmp[_tot++] = getw(x);
if(fa[x]) _tmp[_tot++] = getw(fa[x]);
sort(_tmp, _tmp + _tot);
// printf("tot = %d\n", _tot);
for(re int i = 0; i < _tot; i++) {
int rnk = tree[x].get_rank(_tmp[i]);
// printf("%d\n", rnk);
if(rnk == k) return _tmp[i];
else if(rnk < k) k--;
else break;
}
return tree[x].kth(k);
}
int main() {
srand(20041013);
read(n);
read(m);
for(re int i = 1; i <= n; i++) read(w[i]), _w[i] = w[i];
for(re int i = 0; i < n - 1; i++) {
read(ed[i << 1].second);
read(ed[i << 1].first);
ed[i << 1 | 1].first = ed[i << 1].second;
ed[i << 1 | 1].second = ed[i << 1].first;
}
build_tree();
dfs1(1, 0);
dfs2(1, 1);
// for(int i = 1; i <= n; i++) {
// for(int j = 0; j < cnt[i]; j++) printf("%d ", son[i][j]);
// puts("");
// }
// for(int i = 1; i <= n; i++) printf("%d ", dfn[i]);
// puts("");
for(re int i = 1; i <= n; i++) modify(dfn[i], w[i]);
// for(int i = 1; i <= n; i++) printf("%d ", getw(i));
// puts("");
LCA :: dfs(1);
LCA :: build();
// for(int i = 1; i <= n; i++)
// for(int j = 1; j <= n; j++)
// printf("lca(%d, %d) = %d\n", i, j, LCA :: get_lca(i, j));
for(re int i = 1; i <= n; i++)
if(i == lfa[i] && fa[i])
tree[fa[i]].insert(_w[i]);
for(re int i = 1; i <= m; i++) {
int opt, x, y, k;
read(opt);
if(opt == 1) {
read(x), read(y), read(k);
modify(x, y, k);
}
else {
read(x), read(k);
print(solve(x, k));
}
}
return 0;
}