#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct HLD {
bool e;
int n, timer;
vector<int> sz, dep, par, top, pos;
vector<vector<int>> adj;
HLD(int n, bool edge) : n(n), e(edge) {
sz.resize(n);
dep.resize(n);
top.resize(n);
pos.resize(n);
adj.resize(n);
par.resize(n);
}
void add_edge(int x, int y) {
adj[x].push_back(y);
adj[y].push_back(x);
}
void init(int x) {
sz[x] = 1;
for (int &y : adj[x]) {
dep[y] = dep[x] + 1;
adj[y].erase(find(adj[y].begin(), adj[y].end(), par[y] = x));
init(y);
sz[x] += sz[y];
if (sz[y] > sz[adj[x][0]]) {
swap(y, adj[x][0]);
}
}
}
void dfs(int x) {
pos[x] = timer++;
for (int y : adj[x]) {
top[y] = y == adj[x][0] ? top[x] : y;
dfs(y);
}
}
void build() {
par[0] = -1; init(0);
timer = 0; dfs(0);
}
int lca(int x, int y) {
for (; top[x] != top[y]; x = par[top[x]]) {
if (dep[top[x]] < dep[top[y]]) {
swap(x, y);
}
}
return dep[x] < dep[y] ? x : y;
}
template <class T>
void path(int x, int y, T f) {
for (; top[x] != top[y]; x = par[top[x]]) {
if (dep[top[x]] < dep[top[y]]) {
swap(x, y);
}
f(pos[top[x]], pos[x] + 1);
}
if (dep[x] > dep[y]) {
swap(x, y);
}
f(pos[x] + e, pos[y] + 1);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M, Q;
cin >> N >> M >> Q;
vector<pair<int, int>> edges(N - 1);
vector<vector<int>> adj(N);
for (int i = 0; i < N - 1; i++) {
int x, y;
cin >> x >> y;
x--, y--;
edges[i] = {x, y};
adj[x].push_back(i);
adj[y].push_back(i);
}
vector<pair<int, int>> ch(M);
for (int i = 0; i < M; i++) {
int p, c;
cin >> p >> c;
ch[i] = {c, p - 1};
}
sort(ch.begin(), ch.end());
vector<int> t(N - 1);
{
auto dfs = [&](auto dfs, int x, int p) -> void {
for (int z : adj[x]) {
auto [u, v] = edges[z];
int y = x ^ u ^ v;
if (y != p) {
t[z] = y;
dfs(dfs, y, x);
}
}
};
dfs(dfs, 0, -1);
}
HLD hld(N, 1);
for (int i = 0; i < N; i++) {
for (int &j : adj[i]) {
auto [x, y] = edges[j];
j = i ^ x ^ y;
if (j < i) {
hld.add_edge(i, j);
}
}
}
hld.build();
vector<int> S(Q), T(Q), X(Q);
vector<ll> Y(Q);
for (int i = 0; i < Q; i++) {
cin >> S[i] >> T[i] >> X[i] >> Y[i];
S[i]--, T[i]--;
if (S[i] > T[i]) {
swap(S[i], T[i]);
}
}
vector<int> lo(Q), hi(Q, M);
vector<ll> bit(N + 1);
auto add = [&](int i, int v) {
for (i++; i <= N; i += i & -i) {
bit[i] += v;
}
};
auto sum = [&](int i) {
ll s = 0;
for (; i; i -= i & -i) {
s += bit[i];
}
return s;
};
auto get = [&](int x, int y) {
ll s = 0;
hld.path(x, y, [&](int l, int r) {
s += sum(r) - sum(l);
});
return s;
};
for (int z = 0; z < 20; z++) {
vector<int> mi(Q);
vector<vector<int>> f(M);
for (int i = 0; i < Q; i++) {
if (lo[i] != hi[i]) {
mi[i] = (lo[i] + hi[i] + 1) / 2;
f[mi[i] - 1].push_back(i);
}
}
bit.assign(N + 1, 0);
for (int i = 0; i < M; i++) {
auto [c, p] = ch[i];
add(hld.pos[t[p]], c);
for (int j : f[i]) {
if (get(S[j], T[j]) <= Y[j]) {
lo[j] = i + 1;
} else {
hi[j] = i;
}
}
}
}
vector<ll> ans(Q);
vector<vector<int>> f(M + 1);
for (int i = 0; i < Q; i++) {
f[lo[i]].push_back(i);
}
bit.assign(N + 1, 0);
for (int i = M; i >= 0; i--) {
if (i < M) {
auto [c, p] = ch[i];
add(hld.pos[t[p]], 1);
}
for (int j : f[i]) {
ans[j] = max(-1LL, X[j] - get(S[j], T[j]));
}
}
for (int i = 0; i < Q; i++) {
cout << ans[i] << "\n";
}
return 6/22;
}
Compilation message
currencies.cpp: In constructor 'HLD::HLD(int, bool)':
currencies.cpp:6:9: warning: 'HLD::n' will be initialized after [-Wreorder]
6 | int n, timer;
| ^
currencies.cpp:5:10: warning: 'bool HLD::e' [-Wreorder]
5 | bool e;
| ^
currencies.cpp:9:5: warning: when initialized here [-Wreorder]
9 | HLD(int n, bool edge) : n(n), e(edge) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
724 KB |
Output is correct |
6 |
Correct |
7 ms |
856 KB |
Output is correct |
7 |
Correct |
5 ms |
876 KB |
Output is correct |
8 |
Correct |
8 ms |
856 KB |
Output is correct |
9 |
Correct |
7 ms |
860 KB |
Output is correct |
10 |
Correct |
7 ms |
996 KB |
Output is correct |
11 |
Correct |
7 ms |
860 KB |
Output is correct |
12 |
Correct |
7 ms |
860 KB |
Output is correct |
13 |
Correct |
4 ms |
1116 KB |
Output is correct |
14 |
Correct |
5 ms |
1116 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
980 KB |
Output is correct |
17 |
Correct |
5 ms |
860 KB |
Output is correct |
18 |
Correct |
7 ms |
860 KB |
Output is correct |
19 |
Correct |
5 ms |
924 KB |
Output is correct |
20 |
Correct |
5 ms |
856 KB |
Output is correct |
21 |
Correct |
4 ms |
856 KB |
Output is correct |
22 |
Correct |
5 ms |
860 KB |
Output is correct |
23 |
Correct |
6 ms |
860 KB |
Output is correct |
24 |
Correct |
6 ms |
856 KB |
Output is correct |
25 |
Correct |
7 ms |
860 KB |
Output is correct |
26 |
Correct |
4 ms |
860 KB |
Output is correct |
27 |
Correct |
4 ms |
856 KB |
Output is correct |
28 |
Correct |
4 ms |
972 KB |
Output is correct |
29 |
Correct |
4 ms |
860 KB |
Output is correct |
30 |
Correct |
7 ms |
1112 KB |
Output is correct |
31 |
Correct |
7 ms |
856 KB |
Output is correct |
32 |
Correct |
7 ms |
944 KB |
Output is correct |
33 |
Correct |
5 ms |
1116 KB |
Output is correct |
34 |
Correct |
4 ms |
1116 KB |
Output is correct |
35 |
Correct |
4 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
860 KB |
Output is correct |
3 |
Correct |
7 ms |
860 KB |
Output is correct |
4 |
Correct |
7 ms |
860 KB |
Output is correct |
5 |
Correct |
605 ms |
27104 KB |
Output is correct |
6 |
Correct |
799 ms |
23596 KB |
Output is correct |
7 |
Correct |
755 ms |
24208 KB |
Output is correct |
8 |
Correct |
595 ms |
23284 KB |
Output is correct |
9 |
Correct |
546 ms |
23516 KB |
Output is correct |
10 |
Correct |
886 ms |
29816 KB |
Output is correct |
11 |
Correct |
891 ms |
29908 KB |
Output is correct |
12 |
Correct |
897 ms |
29980 KB |
Output is correct |
13 |
Correct |
924 ms |
29452 KB |
Output is correct |
14 |
Correct |
874 ms |
29772 KB |
Output is correct |
15 |
Correct |
364 ms |
37940 KB |
Output is correct |
16 |
Correct |
345 ms |
38004 KB |
Output is correct |
17 |
Correct |
354 ms |
37176 KB |
Output is correct |
18 |
Correct |
704 ms |
30012 KB |
Output is correct |
19 |
Correct |
634 ms |
29952 KB |
Output is correct |
20 |
Correct |
635 ms |
30196 KB |
Output is correct |
21 |
Correct |
361 ms |
29936 KB |
Output is correct |
22 |
Correct |
341 ms |
29960 KB |
Output is correct |
23 |
Correct |
369 ms |
28344 KB |
Output is correct |
24 |
Correct |
348 ms |
29764 KB |
Output is correct |
25 |
Correct |
625 ms |
30052 KB |
Output is correct |
26 |
Correct |
709 ms |
29952 KB |
Output is correct |
27 |
Correct |
665 ms |
30048 KB |
Output is correct |
28 |
Correct |
343 ms |
28472 KB |
Output is correct |
29 |
Correct |
339 ms |
28684 KB |
Output is correct |
30 |
Correct |
450 ms |
29120 KB |
Output is correct |
31 |
Correct |
442 ms |
28904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
1112 KB |
Output is correct |
3 |
Correct |
4 ms |
1116 KB |
Output is correct |
4 |
Correct |
4 ms |
1116 KB |
Output is correct |
5 |
Correct |
218 ms |
32828 KB |
Output is correct |
6 |
Correct |
203 ms |
31300 KB |
Output is correct |
7 |
Correct |
266 ms |
26200 KB |
Output is correct |
8 |
Correct |
397 ms |
38364 KB |
Output is correct |
9 |
Correct |
369 ms |
38368 KB |
Output is correct |
10 |
Correct |
349 ms |
38280 KB |
Output is correct |
11 |
Correct |
377 ms |
38296 KB |
Output is correct |
12 |
Correct |
298 ms |
38284 KB |
Output is correct |
13 |
Correct |
319 ms |
38120 KB |
Output is correct |
14 |
Correct |
277 ms |
37960 KB |
Output is correct |
15 |
Correct |
294 ms |
37864 KB |
Output is correct |
16 |
Correct |
309 ms |
38140 KB |
Output is correct |
17 |
Correct |
392 ms |
38200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
724 KB |
Output is correct |
6 |
Correct |
7 ms |
856 KB |
Output is correct |
7 |
Correct |
5 ms |
876 KB |
Output is correct |
8 |
Correct |
8 ms |
856 KB |
Output is correct |
9 |
Correct |
7 ms |
860 KB |
Output is correct |
10 |
Correct |
7 ms |
996 KB |
Output is correct |
11 |
Correct |
7 ms |
860 KB |
Output is correct |
12 |
Correct |
7 ms |
860 KB |
Output is correct |
13 |
Correct |
4 ms |
1116 KB |
Output is correct |
14 |
Correct |
5 ms |
1116 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
980 KB |
Output is correct |
17 |
Correct |
5 ms |
860 KB |
Output is correct |
18 |
Correct |
7 ms |
860 KB |
Output is correct |
19 |
Correct |
5 ms |
924 KB |
Output is correct |
20 |
Correct |
5 ms |
856 KB |
Output is correct |
21 |
Correct |
4 ms |
856 KB |
Output is correct |
22 |
Correct |
5 ms |
860 KB |
Output is correct |
23 |
Correct |
6 ms |
860 KB |
Output is correct |
24 |
Correct |
6 ms |
856 KB |
Output is correct |
25 |
Correct |
7 ms |
860 KB |
Output is correct |
26 |
Correct |
4 ms |
860 KB |
Output is correct |
27 |
Correct |
4 ms |
856 KB |
Output is correct |
28 |
Correct |
4 ms |
972 KB |
Output is correct |
29 |
Correct |
4 ms |
860 KB |
Output is correct |
30 |
Correct |
7 ms |
1112 KB |
Output is correct |
31 |
Correct |
7 ms |
856 KB |
Output is correct |
32 |
Correct |
7 ms |
944 KB |
Output is correct |
33 |
Correct |
5 ms |
1116 KB |
Output is correct |
34 |
Correct |
4 ms |
1116 KB |
Output is correct |
35 |
Correct |
4 ms |
1116 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
7 ms |
860 KB |
Output is correct |
38 |
Correct |
7 ms |
860 KB |
Output is correct |
39 |
Correct |
7 ms |
860 KB |
Output is correct |
40 |
Correct |
605 ms |
27104 KB |
Output is correct |
41 |
Correct |
799 ms |
23596 KB |
Output is correct |
42 |
Correct |
755 ms |
24208 KB |
Output is correct |
43 |
Correct |
595 ms |
23284 KB |
Output is correct |
44 |
Correct |
546 ms |
23516 KB |
Output is correct |
45 |
Correct |
886 ms |
29816 KB |
Output is correct |
46 |
Correct |
891 ms |
29908 KB |
Output is correct |
47 |
Correct |
897 ms |
29980 KB |
Output is correct |
48 |
Correct |
924 ms |
29452 KB |
Output is correct |
49 |
Correct |
874 ms |
29772 KB |
Output is correct |
50 |
Correct |
364 ms |
37940 KB |
Output is correct |
51 |
Correct |
345 ms |
38004 KB |
Output is correct |
52 |
Correct |
354 ms |
37176 KB |
Output is correct |
53 |
Correct |
704 ms |
30012 KB |
Output is correct |
54 |
Correct |
634 ms |
29952 KB |
Output is correct |
55 |
Correct |
635 ms |
30196 KB |
Output is correct |
56 |
Correct |
361 ms |
29936 KB |
Output is correct |
57 |
Correct |
341 ms |
29960 KB |
Output is correct |
58 |
Correct |
369 ms |
28344 KB |
Output is correct |
59 |
Correct |
348 ms |
29764 KB |
Output is correct |
60 |
Correct |
625 ms |
30052 KB |
Output is correct |
61 |
Correct |
709 ms |
29952 KB |
Output is correct |
62 |
Correct |
665 ms |
30048 KB |
Output is correct |
63 |
Correct |
343 ms |
28472 KB |
Output is correct |
64 |
Correct |
339 ms |
28684 KB |
Output is correct |
65 |
Correct |
450 ms |
29120 KB |
Output is correct |
66 |
Correct |
442 ms |
28904 KB |
Output is correct |
67 |
Correct |
0 ms |
344 KB |
Output is correct |
68 |
Correct |
4 ms |
1112 KB |
Output is correct |
69 |
Correct |
4 ms |
1116 KB |
Output is correct |
70 |
Correct |
4 ms |
1116 KB |
Output is correct |
71 |
Correct |
218 ms |
32828 KB |
Output is correct |
72 |
Correct |
203 ms |
31300 KB |
Output is correct |
73 |
Correct |
266 ms |
26200 KB |
Output is correct |
74 |
Correct |
397 ms |
38364 KB |
Output is correct |
75 |
Correct |
369 ms |
38368 KB |
Output is correct |
76 |
Correct |
349 ms |
38280 KB |
Output is correct |
77 |
Correct |
377 ms |
38296 KB |
Output is correct |
78 |
Correct |
298 ms |
38284 KB |
Output is correct |
79 |
Correct |
319 ms |
38120 KB |
Output is correct |
80 |
Correct |
277 ms |
37960 KB |
Output is correct |
81 |
Correct |
294 ms |
37864 KB |
Output is correct |
82 |
Correct |
309 ms |
38140 KB |
Output is correct |
83 |
Correct |
392 ms |
38200 KB |
Output is correct |
84 |
Correct |
695 ms |
24756 KB |
Output is correct |
85 |
Correct |
657 ms |
20516 KB |
Output is correct |
86 |
Correct |
625 ms |
18484 KB |
Output is correct |
87 |
Correct |
999 ms |
29512 KB |
Output is correct |
88 |
Correct |
1072 ms |
29688 KB |
Output is correct |
89 |
Correct |
992 ms |
29472 KB |
Output is correct |
90 |
Correct |
967 ms |
29712 KB |
Output is correct |
91 |
Correct |
1016 ms |
29496 KB |
Output is correct |
92 |
Correct |
401 ms |
35236 KB |
Output is correct |
93 |
Correct |
467 ms |
36744 KB |
Output is correct |
94 |
Correct |
762 ms |
29328 KB |
Output is correct |
95 |
Correct |
713 ms |
30236 KB |
Output is correct |
96 |
Correct |
799 ms |
29952 KB |
Output is correct |
97 |
Correct |
745 ms |
29820 KB |
Output is correct |
98 |
Correct |
466 ms |
30004 KB |
Output is correct |
99 |
Correct |
471 ms |
29788 KB |
Output is correct |
100 |
Correct |
445 ms |
29956 KB |
Output is correct |
101 |
Correct |
455 ms |
29576 KB |
Output is correct |
102 |
Correct |
719 ms |
30072 KB |
Output is correct |
103 |
Correct |
743 ms |
29856 KB |
Output is correct |
104 |
Correct |
717 ms |
29888 KB |
Output is correct |
105 |
Correct |
379 ms |
28476 KB |
Output is correct |
106 |
Correct |
496 ms |
28636 KB |
Output is correct |
107 |
Correct |
519 ms |
28584 KB |
Output is correct |
108 |
Correct |
438 ms |
28708 KB |
Output is correct |