-
Notifications
You must be signed in to change notification settings - Fork 169
/
Path Queries II.cpp
164 lines (142 loc) · 3.06 KB
/
Path Queries II.cpp
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
#include <bits/stdc++.h>
/**
* Link Cut Tree
*/
using namespace std;
const int maxN = 2e5+1;
int N, Q, k, x, y, root, idcounter;
struct Lazy {
int a = 1, b = 0;
bool empty(){
return (a == 1 && b == 0);
}
};
struct Node {
Lazy tag;
bool rev = false;
Node *c[2] = {nullptr, nullptr}, *p = nullptr;
int sz, val, sum, mn, mx, id = ++idcounter;
Node(int v){
sz = 1;
val = sum = mn = mx = v;
}
void apply(Lazy other){
mn = mn*other.a + other.b;
mx = mx*other.a + other.b;
val = val*other.a + other.b;
sum = sum*other.a + sz*other.b;
tag = {tag.a*other.a, tag.b*other.a+other.b};
}
void push(){
if(rev){
swap(c[0], c[1]);
if(c[0]) c[0]->rev ^= true;
if(c[1]) c[1]->rev ^= true;
rev = false;
}
if(!tag.empty()){
if(c[0]) c[0]->apply(tag);
if(c[1]) c[1]->apply(tag);
tag = Lazy();
}
}
void pull(){
sum = mn = mx = val;
sz = 1;
for(int i = 0; i < 2; i++){
if(c[i]){
mn = min(mn, c[i]->mn);
mx = max(mx, c[i]->mx);
sum += c[i]->sum;
sz += c[i]->sz;
}
}
}
} *LCT[maxN];
bool notRoot(Node *t){
return t->p && (t->p->c[0] == t || t->p->c[1] == t);
}
void rotate(Node *t){
Node *p = t->p;
bool b = (p->c[0] == t);
if((t->p = p->p) && notRoot(p)) t->p->c[(t->p->c[1] == p)] = t;
if((p->c[!b]=t->c[b])) p->c[!b]->p = p;
t->c[b] = p;
p->p = t;
p->pull();
}
void splay(Node *t){
while(notRoot(t)){
Node *p = t->p;
p->push();
t->push();
rotate(t);
}
t->push();
t->pull();
}
Node* access(Node *t){
Node *last = nullptr;
for(Node *u = t; u; u = u->p){
splay(u);
u->c[1] = last;
last = u;
}
splay(t);
return last;
}
void evert(Node *t){
access(t);
t->rev = true;
}
void link(Node *u, Node *v){
evert(u);
u->p = v;
}
void cut(Node *u, Node *v){
evert(u);
access(v);
if(v->c[0]) v->c[0]->p = 0;
v->c[0] = 0;
v->pull();
}
Node* path(Node *u, Node *v){
evert(u);
access(v);
return v;
}
Node* LCA(Node *u, Node *v){
evert(LCT[root]);
access(u);
return access(v);
}
bool connected(Node *u, Node *v){
path(u, v);
while(v->c[0])
v = v->c[0];
return u == v;
}
int main(){
scanf("%d %d", &N, &Q);
for(int i = 1; i <= N; i++){
scanf("%d", &x);
LCT[i] = new Node(x);
}
for(int i = 0; i < N-1; i++){
scanf("%d %d", &x, &y);
link(LCT[x], LCT[y]);
}
root = 1;
for(int i = 0; i < Q; i++){
scanf("%d", &k);
if(k == 1){
scanf("%d %d", &x, &y);
Node *p = path(LCT[x], LCT[x]);
p->apply({0, y});
} else if(k == 2){
scanf("%d %d", &x, &y);
Node *p = path(LCT[x], LCT[y]);
printf("%d ", p->mx);
}
}
}