#include <bits/stdc++.h>
#define all(v) v.begin(),v.end()
#define pb push_back
#define ll long long
using namespace std;
struct segmentTree {
vector<ll> tree;
void init(int n) {
tree.clear();
tree.resize(1 << (int)ceil(log2(n)) + 1);
}
void update(int lx, int rx, ll v, int l, int r, int n) {
if (r < lx || rx < l)
return;
if (lx <= l && r <= rx) {
tree[n] += v;
return;
}
int m = (l + r) >> 1;
update(lx, rx, v, l, m, n << 1);
update(lx, rx, v, m + 1, r, n << 1 | 1);
}
ll query(int x, int l, int r, int n) {
if (r < x || x < l)
return 0;
if (l == r)
return tree[n];
int m = (l + r) >> 1;
return query(x, l, m, n << 1) + query(x, m + 1, r, n << 1 | 1) + tree[n];
}
};
struct query {
int s, t;
ll x, y;
};
int N, M, Q;
vector<int> Adj[100000];
pair<int, int> Edges[100000];
pair<ll, int> CP[100000];
query Queries[100000];
int In[100000], Out[100000];
int Par[17][100000];
int Depth[100000];
int QL[100000], QR[100000], QX[100000];
segmentTree ST;
ll Ans[100000];
int dfs(int v, int par, int cur) {
In[v] = ++cur;
for (int i : Adj[v]) {
if (i != par)
cur = dfs(i, v, cur);
}
return Out[In[v]] = cur;
}
int getLCA(int u, int v) {
if (Depth[u] > Depth[v])
swap(u, v);
for (int i = 0, t = Depth[v] - Depth[u]; t; i++, t >>= 1) {
if (t & 1)
v = Par[i][v];
}
if (u == v)
return u;
for (int i = 16; i >= 0; i--) {
if (Par[i][u] != Par[i][v]) {
u = Par[i][u];
v = Par[i][v];
}
}
return Par[0][u];
}
ll pathQuery(int u, int v) {
int lca = getLCA(u, v);
return ST.query(u, 0, N - 1, 1) + ST.query(v, 0, N - 1, 1) - 2 * ST.query(lca, 0, N - 1, 1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M >> Q;
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
Edges[i] = { --a,--b };
Adj[a].pb(b);
Adj[b].pb(a);
}
for (int i = 0; i < M; i++) {
int p;
ll c;
cin >> p >> c;
CP[i] = { c,--p };
}
sort(CP, CP + M);
for (int i = 0; i < Q; i++) {
int s, t;
ll x, y;
cin >> s >> t >> x >> y;
Queries[i] = { --s,--t,x,y };
}
dfs(0, -1, -1);
for (int i = 0; i < N - 1; i++) {
pair<int, int>& cur = Edges[i];
cur.first = In[cur.first];
cur.second = In[cur.second];
if (cur.first > cur.second)
swap(cur.first, cur.second);
Par[0][cur.second] = cur.first;
}
for (int i = 1; i < N; i++)
Depth[i] = Depth[Par[0][i]] + 1;
for (int i = 0; i < Q; i++) {
query& cur = Queries[i];
cur.s = In[cur.s];
cur.t = In[cur.t];
}
for (int i = 1; i < 17; i++) {
for (int j = 0; j < N; j++)
Par[i][j] = Par[i - 1][Par[i - 1][j]];
}
fill(QR, QR + Q, M);
while (1) {
bool flag = true;
vector<pair<int, int>> v;
for (int i = 0; i < Q; i++) {
if (QL[i] <= QR[i]) {
flag = false;
v.pb({ (QL[i] + QR[i]) >> 1,i });
}
}
if (flag)
break;
sort(all(v));
ST.init(N);
for (int i = 0, j = 0; i <= M; i++) {
if (i) {
int cur = Edges[CP[i - 1].second].second;
ll c = CP[i - 1].first;
ST.update(cur, Out[cur], c, 0, N - 1, 1);
}
while (j < (int)v.size() && v[j].first == i) {
int cur = v[j++].second;
if (pathQuery(Queries[cur].s, Queries[cur].t) <= Queries[cur].y) {
QX[cur] = i;
QL[cur] = i + 1;
}
else
QR[cur] = i - 1;
}
}
}
vector<pair<int, int>> v;
for (int i = 0; i < Q; i++)
v.push_back({ QX[i],i });
sort(all(v));
ST.init(N);
for (int i = 0, j = 0; i <= M; i++) {
if (i) {
int cur = Edges[CP[i - 1].second].second;
ST.update(cur, Out[cur], 1, 0, N - 1, 1);
}
while (j < (int)v.size() && v[j].first == i) {
int cur = v[j++].second;
Ans[cur] = pathQuery(Queries[cur].s, Queries[cur].t);
}
}
for (int i = 0; i < Q; i++)
cout << max(Queries[i].x - pathQuery(Queries[i].s, Queries[i].t) + Ans[i], (ll)-1) << "\n";
return 0;
}
Compilation message
currencies.cpp: In member function 'void segmentTree::init(int)':
currencies.cpp:10:39: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
10 | tree.resize(1 << (int)ceil(log2(n)) + 1);
| ~~~~~~~~~~~~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2772 KB |
Output is correct |
5 |
Correct |
12 ms |
3136 KB |
Output is correct |
6 |
Correct |
18 ms |
3296 KB |
Output is correct |
7 |
Correct |
13 ms |
3156 KB |
Output is correct |
8 |
Correct |
17 ms |
3284 KB |
Output is correct |
9 |
Correct |
16 ms |
3288 KB |
Output is correct |
10 |
Correct |
17 ms |
3288 KB |
Output is correct |
11 |
Correct |
16 ms |
3292 KB |
Output is correct |
12 |
Correct |
17 ms |
3284 KB |
Output is correct |
13 |
Correct |
18 ms |
3400 KB |
Output is correct |
14 |
Correct |
18 ms |
3376 KB |
Output is correct |
15 |
Correct |
21 ms |
3272 KB |
Output is correct |
16 |
Correct |
21 ms |
3316 KB |
Output is correct |
17 |
Correct |
20 ms |
3260 KB |
Output is correct |
18 |
Correct |
19 ms |
3328 KB |
Output is correct |
19 |
Correct |
14 ms |
3284 KB |
Output is correct |
20 |
Correct |
15 ms |
3236 KB |
Output is correct |
21 |
Correct |
17 ms |
3380 KB |
Output is correct |
22 |
Correct |
18 ms |
3288 KB |
Output is correct |
23 |
Correct |
15 ms |
3284 KB |
Output is correct |
24 |
Correct |
15 ms |
3316 KB |
Output is correct |
25 |
Correct |
16 ms |
3312 KB |
Output is correct |
26 |
Correct |
14 ms |
3296 KB |
Output is correct |
27 |
Correct |
13 ms |
3288 KB |
Output is correct |
28 |
Correct |
14 ms |
3300 KB |
Output is correct |
29 |
Correct |
14 ms |
3284 KB |
Output is correct |
30 |
Correct |
17 ms |
3288 KB |
Output is correct |
31 |
Correct |
18 ms |
3232 KB |
Output is correct |
32 |
Correct |
16 ms |
3284 KB |
Output is correct |
33 |
Correct |
17 ms |
3360 KB |
Output is correct |
34 |
Correct |
17 ms |
3412 KB |
Output is correct |
35 |
Correct |
17 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
17 ms |
3284 KB |
Output is correct |
3 |
Correct |
17 ms |
3300 KB |
Output is correct |
4 |
Correct |
16 ms |
3284 KB |
Output is correct |
5 |
Correct |
1368 ms |
26256 KB |
Output is correct |
6 |
Correct |
1598 ms |
25564 KB |
Output is correct |
7 |
Correct |
1681 ms |
26328 KB |
Output is correct |
8 |
Correct |
1285 ms |
24724 KB |
Output is correct |
9 |
Correct |
1310 ms |
23972 KB |
Output is correct |
10 |
Correct |
2181 ms |
29620 KB |
Output is correct |
11 |
Correct |
2221 ms |
29588 KB |
Output is correct |
12 |
Correct |
2349 ms |
29688 KB |
Output is correct |
13 |
Correct |
2215 ms |
29716 KB |
Output is correct |
14 |
Correct |
2269 ms |
29768 KB |
Output is correct |
15 |
Correct |
3037 ms |
36200 KB |
Output is correct |
16 |
Correct |
2517 ms |
36524 KB |
Output is correct |
17 |
Correct |
2815 ms |
36004 KB |
Output is correct |
18 |
Correct |
2617 ms |
30084 KB |
Output is correct |
19 |
Correct |
2695 ms |
30264 KB |
Output is correct |
20 |
Correct |
2566 ms |
30392 KB |
Output is correct |
21 |
Correct |
1633 ms |
29300 KB |
Output is correct |
22 |
Correct |
1620 ms |
29356 KB |
Output is correct |
23 |
Correct |
1572 ms |
29372 KB |
Output is correct |
24 |
Correct |
1626 ms |
29552 KB |
Output is correct |
25 |
Correct |
1564 ms |
30188 KB |
Output is correct |
26 |
Correct |
1524 ms |
30008 KB |
Output is correct |
27 |
Correct |
1397 ms |
30056 KB |
Output is correct |
28 |
Correct |
930 ms |
29660 KB |
Output is correct |
29 |
Correct |
960 ms |
29508 KB |
Output is correct |
30 |
Correct |
1092 ms |
29748 KB |
Output is correct |
31 |
Correct |
1091 ms |
30144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
17 ms |
3424 KB |
Output is correct |
3 |
Correct |
17 ms |
3364 KB |
Output is correct |
4 |
Correct |
16 ms |
3412 KB |
Output is correct |
5 |
Correct |
1375 ms |
31596 KB |
Output is correct |
6 |
Correct |
1519 ms |
31424 KB |
Output is correct |
7 |
Correct |
1474 ms |
26100 KB |
Output is correct |
8 |
Correct |
2359 ms |
36640 KB |
Output is correct |
9 |
Correct |
2376 ms |
36724 KB |
Output is correct |
10 |
Correct |
2313 ms |
36708 KB |
Output is correct |
11 |
Correct |
1463 ms |
36932 KB |
Output is correct |
12 |
Correct |
1486 ms |
36740 KB |
Output is correct |
13 |
Correct |
1514 ms |
36712 KB |
Output is correct |
14 |
Correct |
991 ms |
36648 KB |
Output is correct |
15 |
Correct |
1048 ms |
36472 KB |
Output is correct |
16 |
Correct |
1235 ms |
36700 KB |
Output is correct |
17 |
Correct |
1304 ms |
36548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2772 KB |
Output is correct |
5 |
Correct |
12 ms |
3136 KB |
Output is correct |
6 |
Correct |
18 ms |
3296 KB |
Output is correct |
7 |
Correct |
13 ms |
3156 KB |
Output is correct |
8 |
Correct |
17 ms |
3284 KB |
Output is correct |
9 |
Correct |
16 ms |
3288 KB |
Output is correct |
10 |
Correct |
17 ms |
3288 KB |
Output is correct |
11 |
Correct |
16 ms |
3292 KB |
Output is correct |
12 |
Correct |
17 ms |
3284 KB |
Output is correct |
13 |
Correct |
18 ms |
3400 KB |
Output is correct |
14 |
Correct |
18 ms |
3376 KB |
Output is correct |
15 |
Correct |
21 ms |
3272 KB |
Output is correct |
16 |
Correct |
21 ms |
3316 KB |
Output is correct |
17 |
Correct |
20 ms |
3260 KB |
Output is correct |
18 |
Correct |
19 ms |
3328 KB |
Output is correct |
19 |
Correct |
14 ms |
3284 KB |
Output is correct |
20 |
Correct |
15 ms |
3236 KB |
Output is correct |
21 |
Correct |
17 ms |
3380 KB |
Output is correct |
22 |
Correct |
18 ms |
3288 KB |
Output is correct |
23 |
Correct |
15 ms |
3284 KB |
Output is correct |
24 |
Correct |
15 ms |
3316 KB |
Output is correct |
25 |
Correct |
16 ms |
3312 KB |
Output is correct |
26 |
Correct |
14 ms |
3296 KB |
Output is correct |
27 |
Correct |
13 ms |
3288 KB |
Output is correct |
28 |
Correct |
14 ms |
3300 KB |
Output is correct |
29 |
Correct |
14 ms |
3284 KB |
Output is correct |
30 |
Correct |
17 ms |
3288 KB |
Output is correct |
31 |
Correct |
18 ms |
3232 KB |
Output is correct |
32 |
Correct |
16 ms |
3284 KB |
Output is correct |
33 |
Correct |
17 ms |
3360 KB |
Output is correct |
34 |
Correct |
17 ms |
3412 KB |
Output is correct |
35 |
Correct |
17 ms |
3420 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
17 ms |
3284 KB |
Output is correct |
38 |
Correct |
17 ms |
3300 KB |
Output is correct |
39 |
Correct |
16 ms |
3284 KB |
Output is correct |
40 |
Correct |
1368 ms |
26256 KB |
Output is correct |
41 |
Correct |
1598 ms |
25564 KB |
Output is correct |
42 |
Correct |
1681 ms |
26328 KB |
Output is correct |
43 |
Correct |
1285 ms |
24724 KB |
Output is correct |
44 |
Correct |
1310 ms |
23972 KB |
Output is correct |
45 |
Correct |
2181 ms |
29620 KB |
Output is correct |
46 |
Correct |
2221 ms |
29588 KB |
Output is correct |
47 |
Correct |
2349 ms |
29688 KB |
Output is correct |
48 |
Correct |
2215 ms |
29716 KB |
Output is correct |
49 |
Correct |
2269 ms |
29768 KB |
Output is correct |
50 |
Correct |
3037 ms |
36200 KB |
Output is correct |
51 |
Correct |
2517 ms |
36524 KB |
Output is correct |
52 |
Correct |
2815 ms |
36004 KB |
Output is correct |
53 |
Correct |
2617 ms |
30084 KB |
Output is correct |
54 |
Correct |
2695 ms |
30264 KB |
Output is correct |
55 |
Correct |
2566 ms |
30392 KB |
Output is correct |
56 |
Correct |
1633 ms |
29300 KB |
Output is correct |
57 |
Correct |
1620 ms |
29356 KB |
Output is correct |
58 |
Correct |
1572 ms |
29372 KB |
Output is correct |
59 |
Correct |
1626 ms |
29552 KB |
Output is correct |
60 |
Correct |
1564 ms |
30188 KB |
Output is correct |
61 |
Correct |
1524 ms |
30008 KB |
Output is correct |
62 |
Correct |
1397 ms |
30056 KB |
Output is correct |
63 |
Correct |
930 ms |
29660 KB |
Output is correct |
64 |
Correct |
960 ms |
29508 KB |
Output is correct |
65 |
Correct |
1092 ms |
29748 KB |
Output is correct |
66 |
Correct |
1091 ms |
30144 KB |
Output is correct |
67 |
Correct |
2 ms |
2772 KB |
Output is correct |
68 |
Correct |
17 ms |
3424 KB |
Output is correct |
69 |
Correct |
17 ms |
3364 KB |
Output is correct |
70 |
Correct |
16 ms |
3412 KB |
Output is correct |
71 |
Correct |
1375 ms |
31596 KB |
Output is correct |
72 |
Correct |
1519 ms |
31424 KB |
Output is correct |
73 |
Correct |
1474 ms |
26100 KB |
Output is correct |
74 |
Correct |
2359 ms |
36640 KB |
Output is correct |
75 |
Correct |
2376 ms |
36724 KB |
Output is correct |
76 |
Correct |
2313 ms |
36708 KB |
Output is correct |
77 |
Correct |
1463 ms |
36932 KB |
Output is correct |
78 |
Correct |
1486 ms |
36740 KB |
Output is correct |
79 |
Correct |
1514 ms |
36712 KB |
Output is correct |
80 |
Correct |
991 ms |
36648 KB |
Output is correct |
81 |
Correct |
1048 ms |
36472 KB |
Output is correct |
82 |
Correct |
1235 ms |
36700 KB |
Output is correct |
83 |
Correct |
1304 ms |
36548 KB |
Output is correct |
84 |
Correct |
1361 ms |
24608 KB |
Output is correct |
85 |
Correct |
1227 ms |
21036 KB |
Output is correct |
86 |
Correct |
1132 ms |
20316 KB |
Output is correct |
87 |
Correct |
2058 ms |
29708 KB |
Output is correct |
88 |
Correct |
2140 ms |
29072 KB |
Output is correct |
89 |
Correct |
2081 ms |
29748 KB |
Output is correct |
90 |
Correct |
2177 ms |
29584 KB |
Output is correct |
91 |
Correct |
2116 ms |
29940 KB |
Output is correct |
92 |
Correct |
2800 ms |
34300 KB |
Output is correct |
93 |
Correct |
2641 ms |
35732 KB |
Output is correct |
94 |
Correct |
2555 ms |
30188 KB |
Output is correct |
95 |
Correct |
2543 ms |
30140 KB |
Output is correct |
96 |
Correct |
2706 ms |
29988 KB |
Output is correct |
97 |
Correct |
2803 ms |
30116 KB |
Output is correct |
98 |
Correct |
1982 ms |
29804 KB |
Output is correct |
99 |
Correct |
1871 ms |
29448 KB |
Output is correct |
100 |
Correct |
1898 ms |
29132 KB |
Output is correct |
101 |
Correct |
1836 ms |
29504 KB |
Output is correct |
102 |
Correct |
1550 ms |
29944 KB |
Output is correct |
103 |
Correct |
1711 ms |
30256 KB |
Output is correct |
104 |
Correct |
1756 ms |
30164 KB |
Output is correct |
105 |
Correct |
1006 ms |
29920 KB |
Output is correct |
106 |
Correct |
997 ms |
29340 KB |
Output is correct |
107 |
Correct |
1192 ms |
29536 KB |
Output is correct |
108 |
Correct |
1232 ms |
29572 KB |
Output is correct |