#include<bits/stdc++.h>
using namespace std;
#define ii pair<int,int>
#define fi first
#define se second
const int MAXN=1e5+5;
const int sqr=320;
const long long INF=1e18;
const long long RNG=1e16;
vector<int> ds[MAXN];
vector<ii> init[MAXN];
int sub[MAXN],root[MAXN],L[MAXN],R[MAXN],dis[MAXN],dep[MAXN],sp[MAXN][20],tdfsus=0;
long long dp[MAXN],block[MAXN],P[MAXN];
bool ck[MAXN],dd[MAXN];
struct tree
{
vector<long long> seg,lazy;
vector<int> D;
vector<ii> vi,range;
int tdfs,n;
void build(int cnt) { n=cnt,tdfs=0,seg.resize(n*4+2),lazy.resize(n*4+2),range.resize(n+2),D.resize(n+2); }
void sortt() { sort(vi.begin(),vi.end()); }
int findpos(int val) { return vi[lower_bound(vi.begin(),vi.end(),(ii){val,0})-vi.begin()].se; }
void down(int pos)
{
long long val=lazy[pos];
seg[pos*2]+=val,seg[pos*2+1]+=val;
lazy[pos*2]+=val,lazy[pos*2+1]+=val;
lazy[pos]=0;
}
void update(int l,int r,int u,int v,long long val,int pos)
{
if(u<=l&&r<=v)
{
seg[pos]+=val,lazy[pos]+=val;
return ;
}
int mid=(l+r)/2;
down(pos);
if(v<=mid) update(l,mid,u,v,val,pos*2);
else if(mid+1<=u) update(mid+1,r,u,v,val,pos*2+1);
else
{
update(l,mid,u,v,val,pos*2);
update(mid+1,r,u,v,val,pos*2+1);
}
seg[pos]=min(seg[pos*2],seg[pos*2+1]);
}
void updnode(int i,long long val)
{
int pos=findpos(i);
update(1,n,range[pos].fi,range[pos].fi,val,1);
}
void updpath(int l,int r,long long val)
{
l=findpos(l),r=findpos(r);
if(D[l]>D[r]) swap(l,r);
update(1,n,range[r].fi,range[r].se,val,1);
}
void dfs(int i,int pre,int pp)
{
tdfs++,vi.push_back({i,tdfs});
int a=tdfs;
if(pp) D[a]=D[pp]+1;
range[a].fi=tdfs;
for(auto v:ds[i]) if(v!=pre&&!ck[v]) dfs(v,i,a);
range[a].se=tdfs;
}
};
tree T[MAXN];
int getlog(long long n) { return 63-__builtin_clzll(n); }
void precalc(int i,int pre)
{
for(auto v:init[i]) if(v.fi!=pre)
{
P[v.fi]=v.se,ds[v.fi].push_back(i),ds[i].push_back(v.fi);
precalc(v.fi,i);
}
}
void dfs(int i,int pre)
{
L[i]=++tdfsus,sp[i][0]=pre;
for(int j=1;j<=18;j++) sp[i][j]=sp[sp[i][j-1]][j-1];
for(auto v:ds[i]) if(v!=pre)
{
dis[v]=dis[i]+1;
dfs(v,i);
}
R[i]=tdfsus;
}
int lca(int a,int b)
{
int x=dis[a],y=dis[b],m=min(x,y);
for(int i=18;i+1;i--)
{
if((x-m)&(1<<i)) a=sp[a][i];
if((y-m)&(1<<i)) b=sp[b][i];
}
if(a==b) return a;
for(int i=18;i+1;i--) if(sp[a][i]!=sp[b][i]) a=sp[a][i],b=sp[b][i];
return sp[a][0];
}
void upddis(int i,long long val)
{
int l=L[i],r=R[i],bl=l/sqr,br=r/sqr;
if(bl==br) for(int j=l;j<=r;j++) dp[j]+=val;
else
{
for(int j=l;j<(bl+1)*sqr;j++) dp[j]+=val;
for(int j=br*sqr;j<=r;j++) dp[j]+=val;
for(int j=bl+1;j<br;j++) block[j]+=val;
}
}
long long dist(int a) { return dp[L[a]]+block[L[a]/sqr]; }
long long getdis(int a,int b) { return dist(a)+dist(b)-dist(lca(a,b))*2; }
void dfsus(int i,int pre)
{
sub[i]=1;
for(auto v:ds[i]) if(v!=pre&&!ck[v])
{
dfsus(v,i);
sub[i]+=sub[v];
}
}
int centroid(int i,int pre,int c)
{
for(auto v:ds[i]) if(v!=pre&&!ck[v]&&sub[v]*2>c) return centroid(v,i,c);
return i;
}
void CD(int i,int pre)
{
dfsus(i,i);
int pos=centroid(i,i,sub[i]);
ck[pos]=true,T[pos].build(sub[i]),T[pos].dfs(pos,pos,0),T[pos].sortt(),T[pos].seg[1]=T[pos].lazy[1]=INF;
if(pre) root[pos]=pre,dep[pos]=dep[pre]+1;
for(auto v:ds[pos]) if(!ck[v]) CD(v,pos);
}
long long query1(int node)
{
long long ans=INF,pos=node;
while(node) ans=min(ans,getdis(node,pos)+T[node].seg[1]),node=root[node];
return ans;
}
void query2(int node)
{
int pos=node;
if(!dd[node])
{
dd[node]=true;
while(node) T[node].updnode(pos,-INF),node=root[node];
}
else
{
dd[node]=false;
while(node) T[node].updnode(pos,INF),node=root[node];
}
}
void query3(int l,int r,long long val)
{
if(dep[l]<dep[r]) swap(l,r);
int node=r;
while(node) T[node].updpath(l,r,val),node=root[node];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,q,pre=1;
cin>>n>>q;
for(int i=1;i<n;i++)
{
int l,r,v;
cin>>l>>r>>v;
init[l].push_back({r,v}),init[r].push_back({l,v});
}
precalc(1,1);
dfs(1,1);
CD(1,0);
for(int i=2;i<=n;i++) query3(sp[i][0],i,P[i]),upddis(i,P[i]);
while(q--)
{
int t;
cin>>t;
if(t==1)
{
int p;
cin>>p;
long long ans=query1(p);
if(ans>=RNG) cout<<"-1\n";
else cout<<ans<<"\n";
}
else if(t==2)
{
int p;
cin>>p;
query2(p);
}
else
{
long long l,r,val;
cin>>l>>r>>val;
if(dis[l]<dis[r]) swap(l,r);
query3(r,l,val-P[l]);
upddis(l,val-P[l]);
P[l]=val;
}
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:169:13: warning: unused variable 'pre' [-Wunused-variable]
169 | int n,q,pre=1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24144 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
13 ms |
24144 KB |
Output is correct |
4 |
Correct |
13 ms |
24144 KB |
Output is correct |
5 |
Correct |
13 ms |
24144 KB |
Output is correct |
6 |
Correct |
17 ms |
24368 KB |
Output is correct |
7 |
Correct |
9 ms |
23120 KB |
Output is correct |
8 |
Correct |
7 ms |
23120 KB |
Output is correct |
9 |
Correct |
7 ms |
23120 KB |
Output is correct |
10 |
Correct |
13 ms |
24392 KB |
Output is correct |
11 |
Correct |
13 ms |
24568 KB |
Output is correct |
12 |
Correct |
12 ms |
24144 KB |
Output is correct |
13 |
Correct |
7 ms |
23120 KB |
Output is correct |
14 |
Correct |
7 ms |
23120 KB |
Output is correct |
15 |
Correct |
9 ms |
23156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1617 ms |
152688 KB |
Output is correct |
2 |
Correct |
1592 ms |
150180 KB |
Output is correct |
3 |
Correct |
1656 ms |
149836 KB |
Output is correct |
4 |
Correct |
1232 ms |
182724 KB |
Output is correct |
5 |
Correct |
1275 ms |
172200 KB |
Output is correct |
6 |
Correct |
1327 ms |
175388 KB |
Output is correct |
7 |
Correct |
158 ms |
70212 KB |
Output is correct |
8 |
Correct |
152 ms |
69440 KB |
Output is correct |
9 |
Correct |
166 ms |
69432 KB |
Output is correct |
10 |
Correct |
1268 ms |
181200 KB |
Output is correct |
11 |
Correct |
1152 ms |
178620 KB |
Output is correct |
12 |
Correct |
1305 ms |
182656 KB |
Output is correct |
13 |
Correct |
164 ms |
69436 KB |
Output is correct |
14 |
Correct |
152 ms |
69436 KB |
Output is correct |
15 |
Correct |
168 ms |
69952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1288 ms |
174012 KB |
Output is correct |
2 |
Correct |
1481 ms |
174956 KB |
Output is correct |
3 |
Correct |
1473 ms |
175248 KB |
Output is correct |
4 |
Correct |
1384 ms |
171364 KB |
Output is correct |
5 |
Correct |
1357 ms |
172440 KB |
Output is correct |
6 |
Correct |
1340 ms |
170692 KB |
Output is correct |
7 |
Correct |
1234 ms |
170272 KB |
Output is correct |
8 |
Correct |
1443 ms |
173500 KB |
Output is correct |
9 |
Correct |
1456 ms |
170920 KB |
Output is correct |
10 |
Correct |
1385 ms |
175132 KB |
Output is correct |
11 |
Correct |
1521 ms |
171668 KB |
Output is correct |
12 |
Correct |
1417 ms |
169412 KB |
Output is correct |
13 |
Correct |
1451 ms |
173560 KB |
Output is correct |
14 |
Correct |
1324 ms |
169196 KB |
Output is correct |
15 |
Correct |
1399 ms |
171620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1694 ms |
150984 KB |
Output is correct |
2 |
Correct |
1907 ms |
152596 KB |
Output is correct |
3 |
Correct |
1628 ms |
148144 KB |
Output is correct |
4 |
Correct |
1479 ms |
181636 KB |
Output is correct |
5 |
Correct |
1384 ms |
180156 KB |
Output is correct |
6 |
Correct |
1376 ms |
182332 KB |
Output is correct |
7 |
Correct |
153 ms |
69948 KB |
Output is correct |
8 |
Correct |
155 ms |
71008 KB |
Output is correct |
9 |
Correct |
173 ms |
69952 KB |
Output is correct |
10 |
Correct |
1242 ms |
179904 KB |
Output is correct |
11 |
Correct |
1398 ms |
174200 KB |
Output is correct |
12 |
Correct |
1451 ms |
180460 KB |
Output is correct |
13 |
Correct |
184 ms |
69692 KB |
Output is correct |
14 |
Correct |
191 ms |
70972 KB |
Output is correct |
15 |
Correct |
198 ms |
70204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2033 ms |
154684 KB |
Output is correct |
2 |
Correct |
1802 ms |
153280 KB |
Output is correct |
3 |
Correct |
1648 ms |
150436 KB |
Output is correct |
4 |
Correct |
1276 ms |
172992 KB |
Output is correct |
5 |
Correct |
1319 ms |
176316 KB |
Output is correct |
6 |
Correct |
1394 ms |
176632 KB |
Output is correct |
7 |
Correct |
177 ms |
69692 KB |
Output is correct |
8 |
Correct |
174 ms |
70208 KB |
Output is correct |
9 |
Correct |
181 ms |
70392 KB |
Output is correct |
10 |
Correct |
1401 ms |
170940 KB |
Output is correct |
11 |
Correct |
1394 ms |
176580 KB |
Output is correct |
12 |
Correct |
1362 ms |
177272 KB |
Output is correct |
13 |
Correct |
177 ms |
70264 KB |
Output is correct |
14 |
Correct |
166 ms |
69696 KB |
Output is correct |
15 |
Correct |
158 ms |
69692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24144 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
13 ms |
24144 KB |
Output is correct |
4 |
Correct |
13 ms |
24144 KB |
Output is correct |
5 |
Correct |
13 ms |
24144 KB |
Output is correct |
6 |
Correct |
17 ms |
24368 KB |
Output is correct |
7 |
Correct |
9 ms |
23120 KB |
Output is correct |
8 |
Correct |
7 ms |
23120 KB |
Output is correct |
9 |
Correct |
7 ms |
23120 KB |
Output is correct |
10 |
Correct |
13 ms |
24392 KB |
Output is correct |
11 |
Correct |
13 ms |
24568 KB |
Output is correct |
12 |
Correct |
12 ms |
24144 KB |
Output is correct |
13 |
Correct |
7 ms |
23120 KB |
Output is correct |
14 |
Correct |
7 ms |
23120 KB |
Output is correct |
15 |
Correct |
9 ms |
23156 KB |
Output is correct |
16 |
Correct |
1617 ms |
152688 KB |
Output is correct |
17 |
Correct |
1592 ms |
150180 KB |
Output is correct |
18 |
Correct |
1656 ms |
149836 KB |
Output is correct |
19 |
Correct |
1232 ms |
182724 KB |
Output is correct |
20 |
Correct |
1275 ms |
172200 KB |
Output is correct |
21 |
Correct |
1327 ms |
175388 KB |
Output is correct |
22 |
Correct |
158 ms |
70212 KB |
Output is correct |
23 |
Correct |
152 ms |
69440 KB |
Output is correct |
24 |
Correct |
166 ms |
69432 KB |
Output is correct |
25 |
Correct |
1268 ms |
181200 KB |
Output is correct |
26 |
Correct |
1152 ms |
178620 KB |
Output is correct |
27 |
Correct |
1305 ms |
182656 KB |
Output is correct |
28 |
Correct |
164 ms |
69436 KB |
Output is correct |
29 |
Correct |
152 ms |
69436 KB |
Output is correct |
30 |
Correct |
168 ms |
69952 KB |
Output is correct |
31 |
Correct |
1288 ms |
174012 KB |
Output is correct |
32 |
Correct |
1481 ms |
174956 KB |
Output is correct |
33 |
Correct |
1473 ms |
175248 KB |
Output is correct |
34 |
Correct |
1384 ms |
171364 KB |
Output is correct |
35 |
Correct |
1357 ms |
172440 KB |
Output is correct |
36 |
Correct |
1340 ms |
170692 KB |
Output is correct |
37 |
Correct |
1234 ms |
170272 KB |
Output is correct |
38 |
Correct |
1443 ms |
173500 KB |
Output is correct |
39 |
Correct |
1456 ms |
170920 KB |
Output is correct |
40 |
Correct |
1385 ms |
175132 KB |
Output is correct |
41 |
Correct |
1521 ms |
171668 KB |
Output is correct |
42 |
Correct |
1417 ms |
169412 KB |
Output is correct |
43 |
Correct |
1451 ms |
173560 KB |
Output is correct |
44 |
Correct |
1324 ms |
169196 KB |
Output is correct |
45 |
Correct |
1399 ms |
171620 KB |
Output is correct |
46 |
Correct |
1694 ms |
150984 KB |
Output is correct |
47 |
Correct |
1907 ms |
152596 KB |
Output is correct |
48 |
Correct |
1628 ms |
148144 KB |
Output is correct |
49 |
Correct |
1479 ms |
181636 KB |
Output is correct |
50 |
Correct |
1384 ms |
180156 KB |
Output is correct |
51 |
Correct |
1376 ms |
182332 KB |
Output is correct |
52 |
Correct |
153 ms |
69948 KB |
Output is correct |
53 |
Correct |
155 ms |
71008 KB |
Output is correct |
54 |
Correct |
173 ms |
69952 KB |
Output is correct |
55 |
Correct |
1242 ms |
179904 KB |
Output is correct |
56 |
Correct |
1398 ms |
174200 KB |
Output is correct |
57 |
Correct |
1451 ms |
180460 KB |
Output is correct |
58 |
Correct |
184 ms |
69692 KB |
Output is correct |
59 |
Correct |
191 ms |
70972 KB |
Output is correct |
60 |
Correct |
198 ms |
70204 KB |
Output is correct |
61 |
Correct |
2033 ms |
154684 KB |
Output is correct |
62 |
Correct |
1802 ms |
153280 KB |
Output is correct |
63 |
Correct |
1648 ms |
150436 KB |
Output is correct |
64 |
Correct |
1276 ms |
172992 KB |
Output is correct |
65 |
Correct |
1319 ms |
176316 KB |
Output is correct |
66 |
Correct |
1394 ms |
176632 KB |
Output is correct |
67 |
Correct |
177 ms |
69692 KB |
Output is correct |
68 |
Correct |
174 ms |
70208 KB |
Output is correct |
69 |
Correct |
181 ms |
70392 KB |
Output is correct |
70 |
Correct |
1401 ms |
170940 KB |
Output is correct |
71 |
Correct |
1394 ms |
176580 KB |
Output is correct |
72 |
Correct |
1362 ms |
177272 KB |
Output is correct |
73 |
Correct |
177 ms |
70264 KB |
Output is correct |
74 |
Correct |
166 ms |
69696 KB |
Output is correct |
75 |
Correct |
158 ms |
69692 KB |
Output is correct |
76 |
Correct |
4 ms |
22096 KB |
Output is correct |
77 |
Correct |
4 ms |
22096 KB |
Output is correct |
78 |
Correct |
4 ms |
22096 KB |
Output is correct |
79 |
Correct |
1437 ms |
147772 KB |
Output is correct |
80 |
Correct |
1707 ms |
151740 KB |
Output is correct |
81 |
Correct |
1623 ms |
152568 KB |
Output is correct |
82 |
Correct |
1333 ms |
177636 KB |
Output is correct |
83 |
Correct |
1338 ms |
175288 KB |
Output is correct |
84 |
Correct |
1429 ms |
176044 KB |
Output is correct |
85 |
Correct |
170 ms |
70968 KB |
Output is correct |
86 |
Correct |
167 ms |
70456 KB |
Output is correct |
87 |
Correct |
171 ms |
69692 KB |
Output is correct |
88 |
Correct |
1249 ms |
173768 KB |
Output is correct |
89 |
Correct |
1235 ms |
175676 KB |
Output is correct |
90 |
Correct |
1254 ms |
174056 KB |
Output is correct |
91 |
Correct |
168 ms |
70972 KB |
Output is correct |
92 |
Correct |
173 ms |
70012 KB |
Output is correct |
93 |
Correct |
173 ms |
69812 KB |
Output is correct |