#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10;
const int M = (1 << 18);
ll t[2 * M];
void update(int ql, int qr, ll add) {
for(ql += M, qr += M + 1; ql < qr; ql >>= 1, qr >>= 1) {
if(ql & 1) t[ql++] += add;
if(qr & 1) t[--qr] += add;
}
}
ll get(int pos) {
ll res = 0;
for(pos += M; pos; pos >>= 1)
res += t[pos];
return res;
}
struct edge {
int u, v, i;
};
struct query {
ll i, s, t, x, y;
};
int n, m, q;
vector<edge> edges;
vector<pair<int, int>> checkpoints;
vector<query> queries;
vector<int> g[N];
int tin[N], tout[N], depth[N], anc[N][18], T, cnt[N], fla[N];
void precalc(int s, int p) {
tin[s] = T++;
anc[s][0] = p;
for(int i = 1; i < 18; i++)
anc[s][i] = anc[anc[s][i - 1]][i - 1];
for(int to : g[s]) {
if(to == p) continue;
depth[to] = depth[s] + 1;
precalc(to, s);
}
tout[s] = T++;
}
void dfs(int s, int p) {
cnt[s] = cnt[p] + fla[s];
for(int to : g[s]) {
if(to == p) continue;
dfs(to, s);
}
}
bool up(int u, int v) {
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v) {
if(up(u, v)) return u;
if(up(v, u)) return v;
for(int i = 17; i >= 0; i--) {
if(!up(anc[u][i], v))
u = anc[u][i];
}
return anc[u][0];
}
int count(int a, int b) {
return cnt[a] + cnt[b] - 2 * cnt[lca(a, b)];
}
ll func(int a, int b) {
return get(tin[a]) + get(tin[b]) - 2 * get(tin[lca(a, b)]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> m >> q;
edges.resize(n - 1);
checkpoints.resize(m + 1);
queries.resize(q);
vector<int> tl(q), tr(q);
for(int i = 0; i < n - 1; i++) {
cin >> edges[i].u >> edges[i].v;
edges[i].i = i;
g[edges[i].u].push_back(edges[i].v);
g[edges[i].v].push_back(edges[i].u);
}
precalc(1, 1);
for(int i = 1; i <= m; i++) {
cin >> checkpoints[i].first >> checkpoints[i].second;
auto [u, v, ix] = edges[checkpoints[i].first - 1];
if(depth[u] > depth[v]) swap(u, v);
checkpoints[i].first = v;
fla[v]++;
}
sort(checkpoints.begin(), checkpoints.end(), [](pair<int, int> r1, pair<int, int> r2) {
return (r1.second < r2.second);
});
// -- INPUT END --
dfs(1, 1);
for(int i = 0; i < q; i++) {
cin >> queries[i].s >> queries[i].t >> queries[i].x >> queries[i].y;
queries[i].i = i;
tl[i] = 0, tr[i] = m + 1;
}
for(int iter = 0; iter < 18; iter++) {
vector<int> oper[m + 1];
for(int i = 0; i < q; i++) {
oper[(tl[i] + tr[i]) / 2].push_back(i);
}
for(int i = 0; i <= m; i++) {
if(i) {auto [v, c] = checkpoints[i]; update(tin[v], tout[v], c); }
for(int p : oper[i]) {
if(func(queries[p].s, queries[p].t) <= queries[p].y)
tl[p] = i;
else
tr[p] = i;
}
}
// return 0;
for(int i = 1; i <= m; i++) {
auto [v, c] = checkpoints[i];
update(tin[v], tout[v], -c);
}
}
vector<int> oper[m + 1];
for(int i = 0; i < q; i++) {
oper[tl[i]].push_back(i);
}
int p = 0;
vector<int> answers(q);
for(int i = 0; i <= m; i++) {
if(i) {auto [v, c] = checkpoints[i]; update(tin[v], tout[v], 1);}
for(int p : oper[i]) {
int ans = queries[p].x - count(queries[p].s, queries[p].t) + func(queries[p].s, queries[p].t);
answers[p] = (ans < 0 ? -1 : ans);
}
}
for(int c : answers)
cout << c << '\n';
}
Compilation message
currencies.cpp: In function 'int main()':
currencies.cpp:136:9: warning: unused variable 'p' [-Wunused-variable]
136 | int p = 0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
8 ms |
3156 KB |
Output is correct |
6 |
Correct |
12 ms |
3248 KB |
Output is correct |
7 |
Correct |
10 ms |
3028 KB |
Output is correct |
8 |
Correct |
15 ms |
3272 KB |
Output is correct |
9 |
Correct |
15 ms |
3216 KB |
Output is correct |
10 |
Correct |
12 ms |
3284 KB |
Output is correct |
11 |
Correct |
12 ms |
3256 KB |
Output is correct |
12 |
Correct |
16 ms |
3272 KB |
Output is correct |
13 |
Correct |
11 ms |
3284 KB |
Output is correct |
14 |
Correct |
13 ms |
3284 KB |
Output is correct |
15 |
Correct |
15 ms |
3264 KB |
Output is correct |
16 |
Correct |
15 ms |
3284 KB |
Output is correct |
17 |
Correct |
14 ms |
3276 KB |
Output is correct |
18 |
Correct |
14 ms |
3264 KB |
Output is correct |
19 |
Correct |
11 ms |
3264 KB |
Output is correct |
20 |
Correct |
11 ms |
3256 KB |
Output is correct |
21 |
Correct |
11 ms |
3232 KB |
Output is correct |
22 |
Correct |
11 ms |
3284 KB |
Output is correct |
23 |
Correct |
11 ms |
3276 KB |
Output is correct |
24 |
Correct |
11 ms |
3284 KB |
Output is correct |
25 |
Correct |
14 ms |
3280 KB |
Output is correct |
26 |
Correct |
10 ms |
3156 KB |
Output is correct |
27 |
Correct |
14 ms |
3256 KB |
Output is correct |
28 |
Correct |
11 ms |
3156 KB |
Output is correct |
29 |
Correct |
12 ms |
3156 KB |
Output is correct |
30 |
Correct |
14 ms |
3264 KB |
Output is correct |
31 |
Correct |
12 ms |
3284 KB |
Output is correct |
32 |
Correct |
12 ms |
3284 KB |
Output is correct |
33 |
Correct |
10 ms |
3368 KB |
Output is correct |
34 |
Correct |
10 ms |
3372 KB |
Output is correct |
35 |
Correct |
14 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
12 ms |
3284 KB |
Output is correct |
3 |
Correct |
12 ms |
3284 KB |
Output is correct |
4 |
Correct |
14 ms |
3280 KB |
Output is correct |
5 |
Correct |
633 ms |
26180 KB |
Output is correct |
6 |
Correct |
828 ms |
22864 KB |
Output is correct |
7 |
Correct |
749 ms |
24340 KB |
Output is correct |
8 |
Correct |
613 ms |
23468 KB |
Output is correct |
9 |
Correct |
621 ms |
23164 KB |
Output is correct |
10 |
Correct |
1123 ms |
28904 KB |
Output is correct |
11 |
Correct |
967 ms |
28876 KB |
Output is correct |
12 |
Correct |
1012 ms |
28780 KB |
Output is correct |
13 |
Correct |
1056 ms |
28784 KB |
Output is correct |
14 |
Correct |
1105 ms |
28892 KB |
Output is correct |
15 |
Correct |
1004 ms |
33132 KB |
Output is correct |
16 |
Correct |
885 ms |
33524 KB |
Output is correct |
17 |
Correct |
1131 ms |
32940 KB |
Output is correct |
18 |
Correct |
1566 ms |
28888 KB |
Output is correct |
19 |
Correct |
1429 ms |
29064 KB |
Output is correct |
20 |
Correct |
1596 ms |
28968 KB |
Output is correct |
21 |
Correct |
697 ms |
27392 KB |
Output is correct |
22 |
Correct |
716 ms |
27388 KB |
Output is correct |
23 |
Correct |
765 ms |
29020 KB |
Output is correct |
24 |
Correct |
808 ms |
29176 KB |
Output is correct |
25 |
Correct |
834 ms |
29244 KB |
Output is correct |
26 |
Correct |
819 ms |
29288 KB |
Output is correct |
27 |
Correct |
840 ms |
29376 KB |
Output is correct |
28 |
Correct |
542 ms |
28492 KB |
Output is correct |
29 |
Correct |
541 ms |
28340 KB |
Output is correct |
30 |
Correct |
563 ms |
28384 KB |
Output is correct |
31 |
Correct |
642 ms |
28408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
11 ms |
3284 KB |
Output is correct |
3 |
Correct |
11 ms |
3264 KB |
Output is correct |
4 |
Correct |
10 ms |
3284 KB |
Output is correct |
5 |
Correct |
459 ms |
29800 KB |
Output is correct |
6 |
Correct |
404 ms |
29196 KB |
Output is correct |
7 |
Correct |
649 ms |
22304 KB |
Output is correct |
8 |
Correct |
760 ms |
33668 KB |
Output is correct |
9 |
Correct |
767 ms |
33640 KB |
Output is correct |
10 |
Correct |
744 ms |
33684 KB |
Output is correct |
11 |
Correct |
579 ms |
33556 KB |
Output is correct |
12 |
Correct |
563 ms |
33548 KB |
Output is correct |
13 |
Correct |
642 ms |
33544 KB |
Output is correct |
14 |
Correct |
680 ms |
32808 KB |
Output is correct |
15 |
Correct |
633 ms |
32716 KB |
Output is correct |
16 |
Correct |
715 ms |
32980 KB |
Output is correct |
17 |
Correct |
672 ms |
32912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
8 ms |
3156 KB |
Output is correct |
6 |
Correct |
12 ms |
3248 KB |
Output is correct |
7 |
Correct |
10 ms |
3028 KB |
Output is correct |
8 |
Correct |
15 ms |
3272 KB |
Output is correct |
9 |
Correct |
15 ms |
3216 KB |
Output is correct |
10 |
Correct |
12 ms |
3284 KB |
Output is correct |
11 |
Correct |
12 ms |
3256 KB |
Output is correct |
12 |
Correct |
16 ms |
3272 KB |
Output is correct |
13 |
Correct |
11 ms |
3284 KB |
Output is correct |
14 |
Correct |
13 ms |
3284 KB |
Output is correct |
15 |
Correct |
15 ms |
3264 KB |
Output is correct |
16 |
Correct |
15 ms |
3284 KB |
Output is correct |
17 |
Correct |
14 ms |
3276 KB |
Output is correct |
18 |
Correct |
14 ms |
3264 KB |
Output is correct |
19 |
Correct |
11 ms |
3264 KB |
Output is correct |
20 |
Correct |
11 ms |
3256 KB |
Output is correct |
21 |
Correct |
11 ms |
3232 KB |
Output is correct |
22 |
Correct |
11 ms |
3284 KB |
Output is correct |
23 |
Correct |
11 ms |
3276 KB |
Output is correct |
24 |
Correct |
11 ms |
3284 KB |
Output is correct |
25 |
Correct |
14 ms |
3280 KB |
Output is correct |
26 |
Correct |
10 ms |
3156 KB |
Output is correct |
27 |
Correct |
14 ms |
3256 KB |
Output is correct |
28 |
Correct |
11 ms |
3156 KB |
Output is correct |
29 |
Correct |
12 ms |
3156 KB |
Output is correct |
30 |
Correct |
14 ms |
3264 KB |
Output is correct |
31 |
Correct |
12 ms |
3284 KB |
Output is correct |
32 |
Correct |
12 ms |
3284 KB |
Output is correct |
33 |
Correct |
10 ms |
3368 KB |
Output is correct |
34 |
Correct |
10 ms |
3372 KB |
Output is correct |
35 |
Correct |
14 ms |
3284 KB |
Output is correct |
36 |
Correct |
1 ms |
2644 KB |
Output is correct |
37 |
Correct |
12 ms |
3284 KB |
Output is correct |
38 |
Correct |
12 ms |
3284 KB |
Output is correct |
39 |
Correct |
14 ms |
3280 KB |
Output is correct |
40 |
Correct |
633 ms |
26180 KB |
Output is correct |
41 |
Correct |
828 ms |
22864 KB |
Output is correct |
42 |
Correct |
749 ms |
24340 KB |
Output is correct |
43 |
Correct |
613 ms |
23468 KB |
Output is correct |
44 |
Correct |
621 ms |
23164 KB |
Output is correct |
45 |
Correct |
1123 ms |
28904 KB |
Output is correct |
46 |
Correct |
967 ms |
28876 KB |
Output is correct |
47 |
Correct |
1012 ms |
28780 KB |
Output is correct |
48 |
Correct |
1056 ms |
28784 KB |
Output is correct |
49 |
Correct |
1105 ms |
28892 KB |
Output is correct |
50 |
Correct |
1004 ms |
33132 KB |
Output is correct |
51 |
Correct |
885 ms |
33524 KB |
Output is correct |
52 |
Correct |
1131 ms |
32940 KB |
Output is correct |
53 |
Correct |
1566 ms |
28888 KB |
Output is correct |
54 |
Correct |
1429 ms |
29064 KB |
Output is correct |
55 |
Correct |
1596 ms |
28968 KB |
Output is correct |
56 |
Correct |
697 ms |
27392 KB |
Output is correct |
57 |
Correct |
716 ms |
27388 KB |
Output is correct |
58 |
Correct |
765 ms |
29020 KB |
Output is correct |
59 |
Correct |
808 ms |
29176 KB |
Output is correct |
60 |
Correct |
834 ms |
29244 KB |
Output is correct |
61 |
Correct |
819 ms |
29288 KB |
Output is correct |
62 |
Correct |
840 ms |
29376 KB |
Output is correct |
63 |
Correct |
542 ms |
28492 KB |
Output is correct |
64 |
Correct |
541 ms |
28340 KB |
Output is correct |
65 |
Correct |
563 ms |
28384 KB |
Output is correct |
66 |
Correct |
642 ms |
28408 KB |
Output is correct |
67 |
Correct |
1 ms |
2644 KB |
Output is correct |
68 |
Correct |
11 ms |
3284 KB |
Output is correct |
69 |
Correct |
11 ms |
3264 KB |
Output is correct |
70 |
Correct |
10 ms |
3284 KB |
Output is correct |
71 |
Correct |
459 ms |
29800 KB |
Output is correct |
72 |
Correct |
404 ms |
29196 KB |
Output is correct |
73 |
Correct |
649 ms |
22304 KB |
Output is correct |
74 |
Correct |
760 ms |
33668 KB |
Output is correct |
75 |
Correct |
767 ms |
33640 KB |
Output is correct |
76 |
Correct |
744 ms |
33684 KB |
Output is correct |
77 |
Correct |
579 ms |
33556 KB |
Output is correct |
78 |
Correct |
563 ms |
33548 KB |
Output is correct |
79 |
Correct |
642 ms |
33544 KB |
Output is correct |
80 |
Correct |
680 ms |
32808 KB |
Output is correct |
81 |
Correct |
633 ms |
32716 KB |
Output is correct |
82 |
Correct |
715 ms |
32980 KB |
Output is correct |
83 |
Correct |
672 ms |
32912 KB |
Output is correct |
84 |
Correct |
597 ms |
23744 KB |
Output is correct |
85 |
Correct |
584 ms |
19524 KB |
Output is correct |
86 |
Correct |
571 ms |
18460 KB |
Output is correct |
87 |
Correct |
1122 ms |
28824 KB |
Output is correct |
88 |
Correct |
1024 ms |
28860 KB |
Output is correct |
89 |
Correct |
970 ms |
28852 KB |
Output is correct |
90 |
Correct |
990 ms |
28888 KB |
Output is correct |
91 |
Correct |
998 ms |
28860 KB |
Output is correct |
92 |
Correct |
1224 ms |
31372 KB |
Output is correct |
93 |
Correct |
1056 ms |
32712 KB |
Output is correct |
94 |
Correct |
1394 ms |
28976 KB |
Output is correct |
95 |
Correct |
1539 ms |
28900 KB |
Output is correct |
96 |
Correct |
1410 ms |
28936 KB |
Output is correct |
97 |
Correct |
1660 ms |
29056 KB |
Output is correct |
98 |
Correct |
804 ms |
27336 KB |
Output is correct |
99 |
Correct |
821 ms |
27412 KB |
Output is correct |
100 |
Correct |
821 ms |
29080 KB |
Output is correct |
101 |
Correct |
852 ms |
29172 KB |
Output is correct |
102 |
Correct |
929 ms |
29216 KB |
Output is correct |
103 |
Correct |
929 ms |
29248 KB |
Output is correct |
104 |
Correct |
874 ms |
29236 KB |
Output is correct |
105 |
Correct |
525 ms |
28224 KB |
Output is correct |
106 |
Correct |
558 ms |
28424 KB |
Output is correct |
107 |
Correct |
601 ms |
28412 KB |
Output is correct |
108 |
Correct |
590 ms |
28448 KB |
Output is correct |