6 const int maxn
= 1e5
+ 10;
11 int val
, lx
, rx
, mx
, size
, sum
;
12 inline bool dir() { return this == p
->ch
[1]; }
13 inline bool isroot() { return !(this == p
->ch
[0] || this == p
->ch
[1]); }
14 inline void setv(int x
);
17 } pool
[maxn
], *nil
= pool
;
19 inline void node::setv(int x
) {
23 lx
= rx
= mx
= max(0, x
) * size
;
26 inline void node::relax() {
30 if (ch
[0] != nil
) ch
[0]->rev
^= 1;
31 if (ch
[1] != nil
) ch
[1]->rev
^= 1;
35 if (ch
[0] != nil
) ch
[0]->setv(val
);
36 if (ch
[1] != nil
) ch
[1]->setv(val
);
41 inline void node::update() {
42 if (ch
[0] != nil
) ch
[0]->relax();
43 if (ch
[1] != nil
) ch
[1]->relax();
44 size
= ch
[0]->size
+ 1 + ch
[1]->size
;
45 sum
= ch
[0]->sum
+ val
+ ch
[1]->sum
;
46 lx
= max(ch
[0]->lx
, ch
[0]->sum
+ val
+ ch
[1]->lx
);
47 rx
= max(ch
[1]->rx
, ch
[0]->rx
+ val
+ ch
[1]->sum
);
48 mx
= max(max(ch
[0]->mx
, ch
[1]->mx
), ch
[0]->rx
+ val
+ ch
[1]->lx
);
51 void rotate(node
*u
) {
53 v
->relax(), u
->relax();
56 if (!v
->isroot()) v
->p
->ch
[v
->dir()] = u
;
57 if (u
->ch
[d
^1] != nil
) u
->ch
[d
^1]->p
= v
;
58 v
->ch
[d
] = u
->ch
[d
^1];
65 void splay(node
*u
, node
*target
= nil
) {
67 while (!u
->isroot()) {
71 (u
->dir() == u
->p
->dir()) ? (rotate(u
->p
), rotate(u
)) : (rotate(u
), rotate(u
));
76 node
* expose(node
*u
) {
78 for (v
= nil
; u
!= nil
; u
= u
->p
) {
86 inline void evert(node
*u
) {
94 nil
->p
= nil
->ch
[0] = nil
->ch
[1] = nil
;
95 for (int i
= 1; i
<= n
; ++i
) {
96 scanf("%d", &pool
[i
].val
);
97 pool
[i
].ch
[0] = pool
[i
].ch
[1] = pool
[i
].p
= nil
;
100 for (int i
= 1; i
< n
; ++i
) {
101 scanf("%d%d", &a
, &b
);
103 pool
[a
].p
= pool
+ b
;
108 scanf("%d%d%d", &op
, &a
, &b
);
112 printf("%d\n", expose(pool
+ b
)->mx
);
116 expose(pool
+ b
)->setv(c
);