#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct segmentTree{
int SZ;
vector<ll> tree, lazy;
void init(int n){
SZ = n;
tree.resize(n*4+1), lazy.resize(n*4+1);
init(1, 1, n);
}
void init(int i, int l, int r){
tree[i] = 1e18;
lazy[i] = 0;
if(l==r) return;
int m = (l+r)>>1;
init(i*2, l, m);
init(i*2+1, m+1, r);
}
void propagate(int i, int l, int r){
tree[i] += lazy[i];
if(l!=r) lazy[i*2] += lazy[i], lazy[i*2+1] += lazy[i];
lazy[i] = 0;
}
void add(int i, int l, int r, int s, int e, ll v){
propagate(i, l, r);
if(r<s || e<l) return;
if(s<=l && r<=e){
lazy[i] = v;
propagate(i, l, r);
return;
}
int m = (l+r)>>1;
add(i*2, l, m, s, e, v);
add(i*2+1, m+1, r, s, e, v);
tree[i] = min(tree[i*2], tree[i*2+1]);
}
ll query(int i, int l, int r, int s, int e){
propagate(i, l, r);
if(r<s || e<l) return 3e18;
if(s<=l && r<=e) return tree[i];
int m = (l+r)>>1;
return min(query(i*2, l, m, s, e), query(i*2+1, m+1, r, s, e));
}
ll query(){
return query(1, 1, SZ, 1, SZ);
}
} tree[100002];
struct Line{
int nxt, idx;
Line(){}
Line(int nxt, int idx): nxt(nxt), idx(idx){}
};
int n, q;
int ex[100002], ey[100002];
ll len[100002];
vector<Line> link[100002];
bool on[100002];
namespace HLD{
struct Fenwick{
int n;
ll tree[100002];
void init(int _n){
n = _n;
for(int i=1; i<=n; i++) tree[i] = 0;
}
void _add(int x, ll v){
while(x<=n){
tree[x] += v;
x += x&-x;
}
}
void add(int s, int e, ll v){
_add(s, v);
_add(e+1, -v);
}
ll _sum(int x){
ll ret = 0;
while(x){
ret += tree[x];
x -= x&-x;
}
return ret;
}
ll sum(int x){
return _sum(x);
}
} tree;
int par[100002];
int in[100002], out[100002], depth[100002], inCnt;
int LCADB[100002][20];
void dfs(int x, int p=-1){
in[x] = ++inCnt;
for(Line y: link[x]){
if(y.nxt == p) continue;
par[y.nxt] = x;
depth[y.nxt] = depth[x] + 1;
dfs(y.nxt, x);
}
out[x] = inCnt;
}
void init(){
dfs(1);
for(int i=1; i<=n; i++) LCADB[i][0] = par[i];
for(int d=1; d<20; d++) for(int i=1; i<=n; i++) LCADB[i][d] = LCADB[LCADB[i][d-1]][d-1];
tree.init(n);
for(int i=1; i<n; i++){
int x = ex[i], y = ey[i];
if(depth[x] < depth[y]) swap(x, y);
/// x�� �� �Ʒ��� �ִ� ����
tree.add(in[x], out[x], len[i]);
}
}
int getLCA(int x, int y){
if(depth[x] > depth[y]) swap(x, y);
for(int d=0; d<20; d++) if((depth[y]-depth[x])&(1<<d)) y = LCADB[y][d];
if(x==y) return x;
for(int d=19; d>=0; d--) if(LCADB[x][d] != LCADB[y][d]) x = LCADB[x][d], y = LCADB[y][d];
return par[x];
}
ll query(int a, int b){
int c = getLCA(a, b);
return tree.sum(in[a]) + tree.sum(in[b]) - tree.sum(in[c])*2;
}
void update(int x, ll v){
tree.add(in[x], out[x], v);
}
}
bool centroidUsed[100002];
int centroidRoot, centroidPar[100002], centroidDepth[100002];
int subtreeSize[100002];
int sz[100002], in[100002][20], out[100002][20], depth[100002][20];
int canFindEdges[100002][20];
void getSubtreeSize(int x, int p=-1){
subtreeSize[x] = 1;
for(auto y: link[x]){
if(y.nxt==p || centroidUsed[y.nxt]) continue;
getSubtreeSize(y.nxt, x);
subtreeSize[x] += subtreeSize[y.nxt];
}
}
int getCentroid(int x, int p, int lim){
for(auto y: link[x]){
if(y.nxt==p || centroidUsed[y.nxt]) continue;
if(subtreeSize[y.nxt] >= lim) return getCentroid(y.nxt, x, lim);
}
return x;
}
void eulerTourDfs(int x, int p, int d, int c){
in[x][d] = ++sz[c];
for(auto y: link[x]){
if(y.nxt == p || centroidUsed[y.nxt]) continue;
depth[y.nxt][d] = depth[x][d] + 1;
eulerTourDfs(y.nxt, x, d, c);
}
out[x][d] = sz[c];
}
void putLengthDfs(int x, int p, int d, int c){
for(auto y: link[x]){
if(y.nxt == p || centroidUsed[y.nxt]) continue;
// printf("Put %d-%d in %d\n", ex[y.idx], ey[y.idx], c);
canFindEdges[y.idx][d] = c;
tree[c].add(1, 1, sz[c], in[y.nxt][d], out[y.nxt][d], len[y.idx]);
putLengthDfs(y.nxt, x, d, c);
}
}
int findCentroid(int x, int d = 0){
getSubtreeSize(x);
x = getCentroid(x, -1, (subtreeSize[x] + 1) / 2);
centroidUsed[x] = 1;
centroidDepth[x] = d;
depth[x][d] = 0;
eulerTourDfs(x, -1, d, x);
tree[x].init(sz[x]);
putLengthDfs(x, -1, d, x);
for(auto y: link[x]){
if(centroidUsed[y.nxt]) continue;
int tmp = findCentroid(y.nxt, d+1);
centroidPar[tmp] = x;
}
return x;
}
ll getAnswer(int x){
int c = x;
ll ret = 1e18;
while(c){
// printf("C: %d, X: %d: XtoC %lld, Cval %lld\n", c, x, HLD::query(c, x), tree[c].query());
ret = min(ret, HLD::query(c, x) + tree[c].query());
c = centroidPar[c];
}
assert(ret <= 1e17);
return ret;
}
void changeVertex(int x){
int c = x;
ll v = (on[x] ? 1e18 : -1e18);
on[x] = !on[x];
while(c){ /// ��Ʈ���̵带 ���ƴٴϸ� +, - ���ֱ�
int d = centroidDepth[c];
tree[c].add(1, 1, sz[c], in[x][d], in[x][d], v);
c = centroidPar[c];
}
}
void changeEdge(int idx, ll v){
/// HLD ������ ����
assert(idx);
int x = ex[idx], y = ey[idx];
if(HLD::depth[x] < HLD::depth[y]) swap(x, y);
HLD::update(x, v - len[idx]);
for(int d=19; d>=0; d--){ /// ��Ʈ���̵� ������ ����
if(!canFindEdges[idx][d]) continue;
int x = ex[idx], y = ey[idx], c = canFindEdges[idx][d];
if(depth[x][d] < depth[y][d]) swap(x, y); /// ���� x�� �� �Ʒ���
tree[c].add(1, 1, sz[c], in[x][d], out[x][d], v - len[idx]);
// printf("After changing c %d: query ans %lld\n", c, tree[c].query());
}
len[idx] = v;
}
int main(){
map<pair<int, int>, int> mpEdge;
scanf("%d %d", &n, &q);
for(int i=1; i<n; i++){
int x, y; ll w;
scanf("%d %d %lld", &x, &y, &w);
ex[i] = x, ey[i] = y, len[i] = w;
link[x].push_back(Line(y, i));
link[y].push_back(Line(x, i));
mpEdge[make_pair(x, y)] = mpEdge[make_pair(y, x)] = i;
}
centroidRoot = findCentroid(1);
HLD::init();
int grapeCnt = 0;
while(q--){
int qt;
scanf("%d", &qt);
if(qt == 1){ /// Seek the nearest
int x;
scanf("%d", &x);
if(!grapeCnt) puts("-1");
else printf("%lld\n", getAnswer(x));
}
else if(qt == 2){ /// ���� ������Ʈ
int x;
scanf("%d", &x);
grapeCnt += (on[x] ? -1 : 1);
changeVertex(x);
}
else{
int a, b; ll w;
scanf("%d %d %lld", &a, &b, &w);
changeEdge(mpEdge[make_pair(a, b)], w);
}
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:261:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
261 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:264:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
264 | scanf("%d %d %lld", &x, &y, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:276:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
276 | scanf("%d", &qt);
| ~~~~~^~~~~~~~~~~
Main.cpp:279:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
279 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
Main.cpp:285:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
285 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
Main.cpp:291:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
291 | scanf("%d %d %lld", &a, &b, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
10460 KB |
Output is correct |
2 |
Correct |
13 ms |
10324 KB |
Output is correct |
3 |
Correct |
14 ms |
10544 KB |
Output is correct |
4 |
Correct |
16 ms |
10720 KB |
Output is correct |
5 |
Correct |
14 ms |
10628 KB |
Output is correct |
6 |
Correct |
13 ms |
10628 KB |
Output is correct |
7 |
Correct |
9 ms |
9688 KB |
Output is correct |
8 |
Correct |
8 ms |
9684 KB |
Output is correct |
9 |
Correct |
11 ms |
9812 KB |
Output is correct |
10 |
Correct |
14 ms |
10708 KB |
Output is correct |
11 |
Correct |
19 ms |
10732 KB |
Output is correct |
12 |
Correct |
15 ms |
10580 KB |
Output is correct |
13 |
Correct |
9 ms |
9812 KB |
Output is correct |
14 |
Correct |
8 ms |
9684 KB |
Output is correct |
15 |
Correct |
8 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1287 ms |
146552 KB |
Output is correct |
2 |
Correct |
1308 ms |
146428 KB |
Output is correct |
3 |
Correct |
1315 ms |
146532 KB |
Output is correct |
4 |
Correct |
1088 ms |
171588 KB |
Output is correct |
5 |
Correct |
1088 ms |
162728 KB |
Output is correct |
6 |
Correct |
1095 ms |
165180 KB |
Output is correct |
7 |
Correct |
281 ms |
86872 KB |
Output is correct |
8 |
Correct |
258 ms |
85088 KB |
Output is correct |
9 |
Correct |
273 ms |
85084 KB |
Output is correct |
10 |
Correct |
1076 ms |
169976 KB |
Output is correct |
11 |
Correct |
1275 ms |
167396 KB |
Output is correct |
12 |
Correct |
1083 ms |
172084 KB |
Output is correct |
13 |
Correct |
260 ms |
85344 KB |
Output is correct |
14 |
Correct |
264 ms |
85472 KB |
Output is correct |
15 |
Correct |
281 ms |
86444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1130 ms |
163860 KB |
Output is correct |
2 |
Correct |
1205 ms |
167824 KB |
Output is correct |
3 |
Correct |
1150 ms |
168332 KB |
Output is correct |
4 |
Correct |
1106 ms |
164272 KB |
Output is correct |
5 |
Correct |
1154 ms |
165448 KB |
Output is correct |
6 |
Correct |
1165 ms |
163460 KB |
Output is correct |
7 |
Correct |
1097 ms |
163136 KB |
Output is correct |
8 |
Correct |
1239 ms |
166388 KB |
Output is correct |
9 |
Correct |
1137 ms |
163744 KB |
Output is correct |
10 |
Correct |
1115 ms |
167896 KB |
Output is correct |
11 |
Correct |
1164 ms |
164424 KB |
Output is correct |
12 |
Correct |
1151 ms |
161944 KB |
Output is correct |
13 |
Correct |
1115 ms |
166256 KB |
Output is correct |
14 |
Correct |
1121 ms |
161924 KB |
Output is correct |
15 |
Correct |
1133 ms |
164460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1403 ms |
144688 KB |
Output is correct |
2 |
Correct |
1507 ms |
148348 KB |
Output is correct |
3 |
Correct |
1420 ms |
144584 KB |
Output is correct |
4 |
Correct |
1264 ms |
170908 KB |
Output is correct |
5 |
Correct |
1273 ms |
169840 KB |
Output is correct |
6 |
Correct |
1229 ms |
171544 KB |
Output is correct |
7 |
Correct |
288 ms |
85972 KB |
Output is correct |
8 |
Correct |
301 ms |
87888 KB |
Output is correct |
9 |
Correct |
297 ms |
86316 KB |
Output is correct |
10 |
Correct |
1307 ms |
169340 KB |
Output is correct |
11 |
Correct |
1099 ms |
164796 KB |
Output is correct |
12 |
Correct |
1210 ms |
170524 KB |
Output is correct |
13 |
Correct |
296 ms |
85440 KB |
Output is correct |
14 |
Correct |
296 ms |
88128 KB |
Output is correct |
15 |
Correct |
280 ms |
86808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1439 ms |
148072 KB |
Output is correct |
2 |
Correct |
1429 ms |
149956 KB |
Output is correct |
3 |
Correct |
1445 ms |
146756 KB |
Output is correct |
4 |
Correct |
1202 ms |
162940 KB |
Output is correct |
5 |
Correct |
1165 ms |
165212 KB |
Output is correct |
6 |
Correct |
1196 ms |
168136 KB |
Output is correct |
7 |
Correct |
305 ms |
85332 KB |
Output is correct |
8 |
Correct |
298 ms |
86708 KB |
Output is correct |
9 |
Correct |
291 ms |
87012 KB |
Output is correct |
10 |
Correct |
1173 ms |
161856 KB |
Output is correct |
11 |
Correct |
1238 ms |
166956 KB |
Output is correct |
12 |
Correct |
1223 ms |
168508 KB |
Output is correct |
13 |
Correct |
307 ms |
86808 KB |
Output is correct |
14 |
Correct |
290 ms |
85564 KB |
Output is correct |
15 |
Correct |
281 ms |
85692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
10460 KB |
Output is correct |
2 |
Correct |
13 ms |
10324 KB |
Output is correct |
3 |
Correct |
14 ms |
10544 KB |
Output is correct |
4 |
Correct |
16 ms |
10720 KB |
Output is correct |
5 |
Correct |
14 ms |
10628 KB |
Output is correct |
6 |
Correct |
13 ms |
10628 KB |
Output is correct |
7 |
Correct |
9 ms |
9688 KB |
Output is correct |
8 |
Correct |
8 ms |
9684 KB |
Output is correct |
9 |
Correct |
11 ms |
9812 KB |
Output is correct |
10 |
Correct |
14 ms |
10708 KB |
Output is correct |
11 |
Correct |
19 ms |
10732 KB |
Output is correct |
12 |
Correct |
15 ms |
10580 KB |
Output is correct |
13 |
Correct |
9 ms |
9812 KB |
Output is correct |
14 |
Correct |
8 ms |
9684 KB |
Output is correct |
15 |
Correct |
8 ms |
9684 KB |
Output is correct |
16 |
Correct |
1287 ms |
146552 KB |
Output is correct |
17 |
Correct |
1308 ms |
146428 KB |
Output is correct |
18 |
Correct |
1315 ms |
146532 KB |
Output is correct |
19 |
Correct |
1088 ms |
171588 KB |
Output is correct |
20 |
Correct |
1088 ms |
162728 KB |
Output is correct |
21 |
Correct |
1095 ms |
165180 KB |
Output is correct |
22 |
Correct |
281 ms |
86872 KB |
Output is correct |
23 |
Correct |
258 ms |
85088 KB |
Output is correct |
24 |
Correct |
273 ms |
85084 KB |
Output is correct |
25 |
Correct |
1076 ms |
169976 KB |
Output is correct |
26 |
Correct |
1275 ms |
167396 KB |
Output is correct |
27 |
Correct |
1083 ms |
172084 KB |
Output is correct |
28 |
Correct |
260 ms |
85344 KB |
Output is correct |
29 |
Correct |
264 ms |
85472 KB |
Output is correct |
30 |
Correct |
281 ms |
86444 KB |
Output is correct |
31 |
Correct |
1130 ms |
163860 KB |
Output is correct |
32 |
Correct |
1205 ms |
167824 KB |
Output is correct |
33 |
Correct |
1150 ms |
168332 KB |
Output is correct |
34 |
Correct |
1106 ms |
164272 KB |
Output is correct |
35 |
Correct |
1154 ms |
165448 KB |
Output is correct |
36 |
Correct |
1165 ms |
163460 KB |
Output is correct |
37 |
Correct |
1097 ms |
163136 KB |
Output is correct |
38 |
Correct |
1239 ms |
166388 KB |
Output is correct |
39 |
Correct |
1137 ms |
163744 KB |
Output is correct |
40 |
Correct |
1115 ms |
167896 KB |
Output is correct |
41 |
Correct |
1164 ms |
164424 KB |
Output is correct |
42 |
Correct |
1151 ms |
161944 KB |
Output is correct |
43 |
Correct |
1115 ms |
166256 KB |
Output is correct |
44 |
Correct |
1121 ms |
161924 KB |
Output is correct |
45 |
Correct |
1133 ms |
164460 KB |
Output is correct |
46 |
Correct |
1403 ms |
144688 KB |
Output is correct |
47 |
Correct |
1507 ms |
148348 KB |
Output is correct |
48 |
Correct |
1420 ms |
144584 KB |
Output is correct |
49 |
Correct |
1264 ms |
170908 KB |
Output is correct |
50 |
Correct |
1273 ms |
169840 KB |
Output is correct |
51 |
Correct |
1229 ms |
171544 KB |
Output is correct |
52 |
Correct |
288 ms |
85972 KB |
Output is correct |
53 |
Correct |
301 ms |
87888 KB |
Output is correct |
54 |
Correct |
297 ms |
86316 KB |
Output is correct |
55 |
Correct |
1307 ms |
169340 KB |
Output is correct |
56 |
Correct |
1099 ms |
164796 KB |
Output is correct |
57 |
Correct |
1210 ms |
170524 KB |
Output is correct |
58 |
Correct |
296 ms |
85440 KB |
Output is correct |
59 |
Correct |
296 ms |
88128 KB |
Output is correct |
60 |
Correct |
280 ms |
86808 KB |
Output is correct |
61 |
Correct |
1439 ms |
148072 KB |
Output is correct |
62 |
Correct |
1429 ms |
149956 KB |
Output is correct |
63 |
Correct |
1445 ms |
146756 KB |
Output is correct |
64 |
Correct |
1202 ms |
162940 KB |
Output is correct |
65 |
Correct |
1165 ms |
165212 KB |
Output is correct |
66 |
Correct |
1196 ms |
168136 KB |
Output is correct |
67 |
Correct |
305 ms |
85332 KB |
Output is correct |
68 |
Correct |
298 ms |
86708 KB |
Output is correct |
69 |
Correct |
291 ms |
87012 KB |
Output is correct |
70 |
Correct |
1173 ms |
161856 KB |
Output is correct |
71 |
Correct |
1238 ms |
166956 KB |
Output is correct |
72 |
Correct |
1223 ms |
168508 KB |
Output is correct |
73 |
Correct |
307 ms |
86808 KB |
Output is correct |
74 |
Correct |
290 ms |
85564 KB |
Output is correct |
75 |
Correct |
281 ms |
85692 KB |
Output is correct |
76 |
Correct |
4 ms |
8148 KB |
Output is correct |
77 |
Correct |
4 ms |
8148 KB |
Output is correct |
78 |
Correct |
4 ms |
8124 KB |
Output is correct |
79 |
Correct |
1307 ms |
144372 KB |
Output is correct |
80 |
Correct |
1415 ms |
148200 KB |
Output is correct |
81 |
Correct |
1406 ms |
149584 KB |
Output is correct |
82 |
Correct |
1218 ms |
167688 KB |
Output is correct |
83 |
Correct |
1203 ms |
166840 KB |
Output is correct |
84 |
Correct |
1302 ms |
165836 KB |
Output is correct |
85 |
Correct |
286 ms |
88128 KB |
Output is correct |
86 |
Correct |
286 ms |
87236 KB |
Output is correct |
87 |
Correct |
284 ms |
85596 KB |
Output is correct |
88 |
Correct |
1132 ms |
163668 KB |
Output is correct |
89 |
Correct |
1191 ms |
166472 KB |
Output is correct |
90 |
Correct |
1124 ms |
164128 KB |
Output is correct |
91 |
Correct |
285 ms |
88140 KB |
Output is correct |
92 |
Correct |
284 ms |
86464 KB |
Output is correct |
93 |
Correct |
290 ms |
85776 KB |
Output is correct |