-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
246 lines (217 loc) · 5.4 KB
/
main.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
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
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#define INF 1000000000
#define sz(a) (int)a.size()
#define pb(a) push_back(a)
#define mset(a,val) memset(a,val,sizeof(a))
#define REP(i,n) for(__typeof(n) i=0;i<(n);++i)
#define FORE(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define pii pair<int,int>
#define mp(a,b) make_pair(a,b)
#define fi first
#define se second
typedef long long ll;
using namespace std;
struct node//segtree node
{
int val;
};
node MND(int a)
{
node t={a};
return t;
}
node getNode(node A,node B)
{
A.val=max(A.val,B.val);
return A;
}
// ---------- LOWEST COMMON ANCESTOR + HEAVY LIGHT DECOMPOSITION-----------
#define MAXN 10005
#define LOGN 16
int N,PATHC;
vector<pii> G[MAXN];//GRAPH
int T[MAXN];//PARENT
int D[MAXN];//DIST TO PARENT
int L[MAXN];//LEVEL
int ANC[MAXN][LOGN];//MAXN,log2(MAXN) ANC[i][j] = 2^j-th ancestor of i
int getPATH[MAXN];//what heavy path i-th node belongs to
int getPPOS[MAXN];//position of the i-th node in its heavy path
vector<int> PATHS[MAXN];//nodes of i-th heavy path
vector<node> SEGTREE[MAXN];//segment tree for i-th path
pii EDGE[MAXN];
void initStuff()
{
REP(i,N)G[i].clear(),PATHS[i].clear(),SEGTREE[i].clear();
}
int makeT(int i,int par,int lev)
{
int maxi=-1,ind=0,cur=0,size=1;
T[i]=par;
L[i]=lev;
//find the biggest subtree
FORE(it,G[i])
if (it->first!=par)
{
cur=makeT(it->first,i,lev+1);
D[it->first]=it->second;
size+=cur;
if (cur>maxi) maxi=cur,ind=it->first;
}
//extend the right path
if(maxi==-1)
{//we are in the leaf
getPATH[i]=PATHC++;
}else
getPATH[i]=getPATH[ind];
getPPOS[i]=sz(PATHS[getPATH[i]]);
PATHS[getPATH[i]].pb(i);
return size;
}
void initLCA()
{
mset(ANC,-1);
REP(i,N)
ANC[i][0]=T[i];
for(int j=1;(1<<j)<N;j++)
REP(i,N)
if (ANC[i][j-1]!=-1)
ANC[i][j]=ANC[ANC[i][j-1]][j-1];
}
inline int getAncestor(int ind,int k)//gets k-th ancestor of node ind
{
for(int i=LOGN-1;ind!=-1&&i>=0;i--)
if (k>=(1<<i))
{
k-=(1<<i);
ind=ANC[ind][i];
}
return ind;
}
inline int getLCA(int a,int b)//returns lowest common ancestor of a and b
{
if (L[a]<L[b]) swap(a,b);
if (L[a]!=L[b])
a=getAncestor(a,L[a]-L[b]);
if (a==b) return a;
for(int i=LOGN-1;i>=0;i--)
if (ANC[a][i]!=-1&&ANC[a][i]!=ANC[b][i])
a=ANC[a][i],
b=ANC[b][i];
return T[a];
}
//---------------------------------------------
// ------------------ SEGMENT TREE ------------------------------
void makeSEGTREE(int lo,int hi,int indT,int indP)//lo,hi - interval, indT - tree index, indP - path index
{
assert(indT<sz(SEGTREE[indP]));
if (lo+1==hi)
SEGTREE[indP][indT]=MND(D[PATHS[indP][lo]]);
else
{
int mid=(lo+hi)/2;
makeSEGTREE(lo,mid,indT*2,indP);
makeSEGTREE(mid,hi,indT*2+1,indP);
SEGTREE[indP][indT]=getNode(SEGTREE[indP][indT*2],SEGTREE[indP][indT*2+1]);
}
}
void updateSEGTREE(int lo,int hi,int indT,int indP,int updatePos)//lo,hi - interval, indT - tree index, indP - path index
{
assert(indT<sz(SEGTREE[indP]));
if (lo+1==hi)
SEGTREE[indP][indT]=MND(D[PATHS[indP][lo]]);
else
{
int mid=(lo+hi)/2;
if (updatePos<mid)
updateSEGTREE(lo,mid,indT*2,indP,updatePos);
else
updateSEGTREE(mid,hi,indT*2+1,indP,updatePos);
SEGTREE[indP][indT]=getNode(SEGTREE[indP][indT*2],SEGTREE[indP][indT*2+1]);
}
}
node querySEGTREE(int lo,int hi,int indT,int indP,int qX,int qY)
{
assert(indT<sz(SEGTREE[indP]));
if (qX<=lo&&hi<=qY) return SEGTREE[indP][indT];
if (lo+1==hi) return SEGTREE[indP][indT];
int mid=(lo+hi)/2;
node L=MND(-INF),R=MND(-INF);
if (max(lo,qX)<min(qY,mid))
L=querySEGTREE(lo,mid,indT*2,indP,qX,qY);
if (max(mid,qX)<min(qY,hi))
R=querySEGTREE(mid,hi,indT*2+1,indP,qX,qY);
if (L.val==-INF) return R;
if (R.val==-INF) return L;
return getNode(L,R);
}
// ---------------------------------------------------------------
int queryTree(int ind,int tar)//computes the maximum edge on the path ind->tar where L[ind]>=L[tar]
{
if (ind==tar) return 0;
if (getPATH[ind]==getPATH[T[ind]])
{//we can jump forward - HEAVY EDGE
int cPath=getPATH[ind];
int end=PATHS[cPath].back();
end=L[end]>=L[tar]?end:tar;
int val1=queryTree(end,tar);
int val2=querySEGTREE(0,sz(PATHS[cPath])-1,1,cPath,getPPOS[ind],getPPOS[end]).val;
return max(val1,val2);
}else
{//we have to move - LIGHT EDGE
return max(queryTree(T[ind],tar),D[ind]);
}
}
int cases,a,b,c;
char buf[16];
int main( )
{
scanf("%d",&cases);
REP(ii,cases)
{
scanf("%d",&N);
initStuff();
REP(i,N-1)
{
scanf("%d %d %d",&a,&b,&c),a--,b--;
EDGE[i]=mp(a,b);
G[a].pb(mp(b,c));
G[b].pb(mp(a,c));
}
PATHC=0;
makeT(0,-1,0);
initLCA();
REP(i,PATHC)
if (sz(PATHS[i])>1)
{//init segment tree
SEGTREE[i].resize(sz(PATHS[i])*5);
makeSEGTREE(0,sz(PATHS[i])-1,1,i);
}
while(scanf("%s",buf),buf[0]!='D')
{
scanf("%d %d",&a,&b);
if (buf[0]=='C')
{//CHANGE i ti : change the cost of the i-th edge to ti
a--;
int A=EDGE[a].fi;
int B=EDGE[a].se;
if (L[A]<L[B])swap(A,B);//A is child
D[A]=b;
int curPath=getPATH[A];
if (PATHS[curPath].back()!=A)
updateSEGTREE(0,sz(PATHS[curPath])-1,1,curPath,getPPOS[A]);
}else
{//QUERY a b : ask for the maximum edge cost on the path from node a to node b
a--,b--;
int LCA=getLCA(a,b);
int res=max(queryTree(a,LCA),queryTree(b,LCA));
printf("%d\n",res);
}
}
if (ii<cases-1)printf("\n");
}
return 0;
}