#include<bits/stdc++.h>
using namespace std;
#define pb push_back
int N, M, Q;
vector<vector<int>> adj;
vector<int> par, sz, anc, flat, pos;
vector<pair<int, int>> e, ch;
vector<int> S, T, X;
vector<long long> Y;
void dfs_sz(int s) {
sz[s] = 1;
if(adj[s][0] == par[s] && (int)adj[s].size() >= 2) swap(adj[s][0], adj[s][1]);
for(auto& u : adj[s]) {
if(u == par[s]) continue;
par[u] = s;
dfs_sz(u);
sz[s] += sz[u];
if(sz[adj[s][0]] < sz[u]) swap(adj[s][0], u);
}
}
void build_hld(int s, int r) {
pos[s] = (int)flat.size();
flat.pb(s);
anc[s] = r;
for(auto& u : adj[s]) {
if(u == par[s]) continue;
build_hld(u, (u == adj[s][0] ? r : u));
}
}
int LCA(int a, int b) {
if(pos[a] > pos[b]) swap(a, b);
if(anc[a] == anc[b]) return a;
return LCA(a, par[anc[b]]);
}
vector<long long> bit;
void update(int ind, long long val) {
++ind;
while(ind <= N) {
bit[ind] += val;
ind += ind & -ind;
}
}
long long pref_sum(int ind) {
++ind;
long long sum = 0;
while(ind > 0) {
sum += bit[ind];
ind -= ind & -ind;
}
return sum;
}
long long Query(int l, int r) {
return pref_sum(r) - pref_sum(l - 1);
}
long long hld_path(int u, int v) {
if(anc[u] == anc[v]) return Query(pos[v], pos[u]);
return Query(pos[anc[u]], pos[u]) + hld_path(par[anc[u]], v);
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M >> Q;
adj.resize(N);
e.resize(N - 1);
for(int i = 0; i + 1 < N; ++i) {
int u, v; cin >> u >> v; --u; --v;
e[i] = {u, v};
adj[u].pb(v);
adj[v].pb(u);
}
{ // hld
par.resize(N);
sz.resize(N);
par[0] = -1;
dfs_sz(0);
anc.resize(N);
pos.resize(N);
build_hld(0, -1);
}
for(int i = 0; i + 1 < N; ++i) {
if(par[e[i].second] == e[i].first) swap(e[i].first, e[i].second);
}
for(int i = 0; i < M; ++i) {
int x, y; cin >> x >> y;
ch.pb({y, e[x - 1].first});
}
sort(begin(ch), end(ch));
S.resize(Q);
T.resize(Q);
X.resize(Q);
Y.resize(Q);
for(int i = 0; i < Q; ++i) {
cin >> S[i] >> T[i] >> X[i] >> Y[i];
--S[i]; --T[i];
}
vector<int> TL(Q, 0), TR(Q, M);
while(true) {
bool any = false;
vector<vector<int>> Pos(M + 1);
for(int i = 0; i < Q; ++i) {
if(TL[i] == TR[i]) continue;
any = true;
int TM = (TL[i] + TR[i] + 1) / 2;
Pos[TM].pb(i);
}
if(!any) break;
bit = vector<long long>(N + 1, 0LL);
for(int i = 0; i < M; ++i) {
update(pos[ch[i].second], ch[i].first);
for(auto& u : Pos[i + 1]) {
int L = LCA(S[u], T[u]);
long long sum = hld_path(S[u], L) + hld_path(T[u], L) - 2 * hld_path(L, L);
if(sum <= Y[u]) {
TL[u] = i + 1;
} else {
TR[u] = i;
}
}
}
}
vector<int> ans(Q);
bit = vector<long long>(N + 1, 0LL);
vector<vector<int>> Pos(M + 1);
for(int i = 0; i < Q; ++i) {
Pos[TL[i]].pb(i);
}
for(int i = M; i >= 0; --i) {
if(i < M) update(pos[ch[i].second], 1);
for(int& u : Pos[i]) {
if(i == M) {
ans[u] = X[u];
continue;
}
int L = LCA(S[u], T[u]);
long long sum = hld_path(S[u], L) + hld_path(T[u], L) - 2 * hld_path(L, L);
ans[u] = max(-1LL, X[u] - sum);
}
}
for(auto& u : ans) {
cout << u << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
5 ms |
596 KB |
Output is correct |
6 |
Correct |
6 ms |
716 KB |
Output is correct |
7 |
Correct |
5 ms |
596 KB |
Output is correct |
8 |
Correct |
7 ms |
724 KB |
Output is correct |
9 |
Correct |
7 ms |
724 KB |
Output is correct |
10 |
Correct |
7 ms |
764 KB |
Output is correct |
11 |
Correct |
7 ms |
788 KB |
Output is correct |
12 |
Correct |
7 ms |
712 KB |
Output is correct |
13 |
Correct |
5 ms |
844 KB |
Output is correct |
14 |
Correct |
5 ms |
852 KB |
Output is correct |
15 |
Correct |
5 ms |
724 KB |
Output is correct |
16 |
Correct |
5 ms |
804 KB |
Output is correct |
17 |
Correct |
6 ms |
716 KB |
Output is correct |
18 |
Correct |
5 ms |
816 KB |
Output is correct |
19 |
Correct |
4 ms |
724 KB |
Output is correct |
20 |
Correct |
4 ms |
724 KB |
Output is correct |
21 |
Correct |
4 ms |
756 KB |
Output is correct |
22 |
Correct |
4 ms |
724 KB |
Output is correct |
23 |
Correct |
6 ms |
724 KB |
Output is correct |
24 |
Correct |
6 ms |
724 KB |
Output is correct |
25 |
Correct |
6 ms |
724 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
4 ms |
760 KB |
Output is correct |
28 |
Correct |
4 ms |
724 KB |
Output is correct |
29 |
Correct |
4 ms |
724 KB |
Output is correct |
30 |
Correct |
7 ms |
760 KB |
Output is correct |
31 |
Correct |
7 ms |
676 KB |
Output is correct |
32 |
Correct |
7 ms |
796 KB |
Output is correct |
33 |
Correct |
5 ms |
852 KB |
Output is correct |
34 |
Correct |
5 ms |
852 KB |
Output is correct |
35 |
Correct |
5 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
7 ms |
724 KB |
Output is correct |
3 |
Correct |
7 ms |
724 KB |
Output is correct |
4 |
Correct |
7 ms |
720 KB |
Output is correct |
5 |
Correct |
515 ms |
20696 KB |
Output is correct |
6 |
Correct |
703 ms |
18796 KB |
Output is correct |
7 |
Correct |
663 ms |
19668 KB |
Output is correct |
8 |
Correct |
490 ms |
17968 KB |
Output is correct |
9 |
Correct |
471 ms |
17948 KB |
Output is correct |
10 |
Correct |
776 ms |
23624 KB |
Output is correct |
11 |
Correct |
824 ms |
23504 KB |
Output is correct |
12 |
Correct |
787 ms |
23928 KB |
Output is correct |
13 |
Correct |
794 ms |
23632 KB |
Output is correct |
14 |
Correct |
792 ms |
23664 KB |
Output is correct |
15 |
Correct |
376 ms |
30252 KB |
Output is correct |
16 |
Correct |
369 ms |
30944 KB |
Output is correct |
17 |
Correct |
349 ms |
30320 KB |
Output is correct |
18 |
Correct |
629 ms |
24404 KB |
Output is correct |
19 |
Correct |
611 ms |
24232 KB |
Output is correct |
20 |
Correct |
587 ms |
24076 KB |
Output is correct |
21 |
Correct |
321 ms |
23716 KB |
Output is correct |
22 |
Correct |
355 ms |
23892 KB |
Output is correct |
23 |
Correct |
319 ms |
22856 KB |
Output is correct |
24 |
Correct |
324 ms |
23576 KB |
Output is correct |
25 |
Correct |
609 ms |
23448 KB |
Output is correct |
26 |
Correct |
638 ms |
24216 KB |
Output is correct |
27 |
Correct |
629 ms |
24300 KB |
Output is correct |
28 |
Correct |
293 ms |
22460 KB |
Output is correct |
29 |
Correct |
334 ms |
22952 KB |
Output is correct |
30 |
Correct |
416 ms |
22476 KB |
Output is correct |
31 |
Correct |
394 ms |
22948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
884 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
5 ms |
920 KB |
Output is correct |
5 |
Correct |
229 ms |
25688 KB |
Output is correct |
6 |
Correct |
213 ms |
24524 KB |
Output is correct |
7 |
Correct |
290 ms |
22356 KB |
Output is correct |
8 |
Correct |
352 ms |
31200 KB |
Output is correct |
9 |
Correct |
356 ms |
30612 KB |
Output is correct |
10 |
Correct |
358 ms |
30992 KB |
Output is correct |
11 |
Correct |
306 ms |
30264 KB |
Output is correct |
12 |
Correct |
319 ms |
30672 KB |
Output is correct |
13 |
Correct |
311 ms |
30776 KB |
Output is correct |
14 |
Correct |
247 ms |
29940 KB |
Output is correct |
15 |
Correct |
229 ms |
30864 KB |
Output is correct |
16 |
Correct |
271 ms |
30252 KB |
Output is correct |
17 |
Correct |
265 ms |
30892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
5 ms |
596 KB |
Output is correct |
6 |
Correct |
6 ms |
716 KB |
Output is correct |
7 |
Correct |
5 ms |
596 KB |
Output is correct |
8 |
Correct |
7 ms |
724 KB |
Output is correct |
9 |
Correct |
7 ms |
724 KB |
Output is correct |
10 |
Correct |
7 ms |
764 KB |
Output is correct |
11 |
Correct |
7 ms |
788 KB |
Output is correct |
12 |
Correct |
7 ms |
712 KB |
Output is correct |
13 |
Correct |
5 ms |
844 KB |
Output is correct |
14 |
Correct |
5 ms |
852 KB |
Output is correct |
15 |
Correct |
5 ms |
724 KB |
Output is correct |
16 |
Correct |
5 ms |
804 KB |
Output is correct |
17 |
Correct |
6 ms |
716 KB |
Output is correct |
18 |
Correct |
5 ms |
816 KB |
Output is correct |
19 |
Correct |
4 ms |
724 KB |
Output is correct |
20 |
Correct |
4 ms |
724 KB |
Output is correct |
21 |
Correct |
4 ms |
756 KB |
Output is correct |
22 |
Correct |
4 ms |
724 KB |
Output is correct |
23 |
Correct |
6 ms |
724 KB |
Output is correct |
24 |
Correct |
6 ms |
724 KB |
Output is correct |
25 |
Correct |
6 ms |
724 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
4 ms |
760 KB |
Output is correct |
28 |
Correct |
4 ms |
724 KB |
Output is correct |
29 |
Correct |
4 ms |
724 KB |
Output is correct |
30 |
Correct |
7 ms |
760 KB |
Output is correct |
31 |
Correct |
7 ms |
676 KB |
Output is correct |
32 |
Correct |
7 ms |
796 KB |
Output is correct |
33 |
Correct |
5 ms |
852 KB |
Output is correct |
34 |
Correct |
5 ms |
852 KB |
Output is correct |
35 |
Correct |
5 ms |
852 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
7 ms |
724 KB |
Output is correct |
38 |
Correct |
7 ms |
724 KB |
Output is correct |
39 |
Correct |
7 ms |
720 KB |
Output is correct |
40 |
Correct |
515 ms |
20696 KB |
Output is correct |
41 |
Correct |
703 ms |
18796 KB |
Output is correct |
42 |
Correct |
663 ms |
19668 KB |
Output is correct |
43 |
Correct |
490 ms |
17968 KB |
Output is correct |
44 |
Correct |
471 ms |
17948 KB |
Output is correct |
45 |
Correct |
776 ms |
23624 KB |
Output is correct |
46 |
Correct |
824 ms |
23504 KB |
Output is correct |
47 |
Correct |
787 ms |
23928 KB |
Output is correct |
48 |
Correct |
794 ms |
23632 KB |
Output is correct |
49 |
Correct |
792 ms |
23664 KB |
Output is correct |
50 |
Correct |
376 ms |
30252 KB |
Output is correct |
51 |
Correct |
369 ms |
30944 KB |
Output is correct |
52 |
Correct |
349 ms |
30320 KB |
Output is correct |
53 |
Correct |
629 ms |
24404 KB |
Output is correct |
54 |
Correct |
611 ms |
24232 KB |
Output is correct |
55 |
Correct |
587 ms |
24076 KB |
Output is correct |
56 |
Correct |
321 ms |
23716 KB |
Output is correct |
57 |
Correct |
355 ms |
23892 KB |
Output is correct |
58 |
Correct |
319 ms |
22856 KB |
Output is correct |
59 |
Correct |
324 ms |
23576 KB |
Output is correct |
60 |
Correct |
609 ms |
23448 KB |
Output is correct |
61 |
Correct |
638 ms |
24216 KB |
Output is correct |
62 |
Correct |
629 ms |
24300 KB |
Output is correct |
63 |
Correct |
293 ms |
22460 KB |
Output is correct |
64 |
Correct |
334 ms |
22952 KB |
Output is correct |
65 |
Correct |
416 ms |
22476 KB |
Output is correct |
66 |
Correct |
394 ms |
22948 KB |
Output is correct |
67 |
Correct |
0 ms |
212 KB |
Output is correct |
68 |
Correct |
5 ms |
884 KB |
Output is correct |
69 |
Correct |
4 ms |
852 KB |
Output is correct |
70 |
Correct |
5 ms |
920 KB |
Output is correct |
71 |
Correct |
229 ms |
25688 KB |
Output is correct |
72 |
Correct |
213 ms |
24524 KB |
Output is correct |
73 |
Correct |
290 ms |
22356 KB |
Output is correct |
74 |
Correct |
352 ms |
31200 KB |
Output is correct |
75 |
Correct |
356 ms |
30612 KB |
Output is correct |
76 |
Correct |
358 ms |
30992 KB |
Output is correct |
77 |
Correct |
306 ms |
30264 KB |
Output is correct |
78 |
Correct |
319 ms |
30672 KB |
Output is correct |
79 |
Correct |
311 ms |
30776 KB |
Output is correct |
80 |
Correct |
247 ms |
29940 KB |
Output is correct |
81 |
Correct |
229 ms |
30864 KB |
Output is correct |
82 |
Correct |
271 ms |
30252 KB |
Output is correct |
83 |
Correct |
265 ms |
30892 KB |
Output is correct |
84 |
Correct |
515 ms |
19096 KB |
Output is correct |
85 |
Correct |
561 ms |
16600 KB |
Output is correct |
86 |
Correct |
520 ms |
14736 KB |
Output is correct |
87 |
Correct |
829 ms |
23084 KB |
Output is correct |
88 |
Correct |
824 ms |
23288 KB |
Output is correct |
89 |
Correct |
798 ms |
23092 KB |
Output is correct |
90 |
Correct |
805 ms |
23116 KB |
Output is correct |
91 |
Correct |
810 ms |
23944 KB |
Output is correct |
92 |
Correct |
360 ms |
28692 KB |
Output is correct |
93 |
Correct |
397 ms |
30068 KB |
Output is correct |
94 |
Correct |
622 ms |
24396 KB |
Output is correct |
95 |
Correct |
618 ms |
24300 KB |
Output is correct |
96 |
Correct |
616 ms |
24124 KB |
Output is correct |
97 |
Correct |
640 ms |
24336 KB |
Output is correct |
98 |
Correct |
346 ms |
23856 KB |
Output is correct |
99 |
Correct |
331 ms |
23872 KB |
Output is correct |
100 |
Correct |
337 ms |
22912 KB |
Output is correct |
101 |
Correct |
338 ms |
23676 KB |
Output is correct |
102 |
Correct |
619 ms |
23332 KB |
Output is correct |
103 |
Correct |
640 ms |
24292 KB |
Output is correct |
104 |
Correct |
602 ms |
23428 KB |
Output is correct |
105 |
Correct |
304 ms |
22912 KB |
Output is correct |
106 |
Correct |
374 ms |
23004 KB |
Output is correct |
107 |
Correct |
419 ms |
23392 KB |
Output is correct |
108 |
Correct |
367 ms |
22900 KB |
Output is correct |