#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
struct query{
int a, b, lc, gold, silver, need, l, r, i;
};
struct check{
int a, c;
};
int n, m, q;
const int N = 1e5 + 69;
vector <int> adj[N], work[N];
int seg[4 * N], lazy[4 * N], dep[N], lift[N][20], tin[N], tout[N], timer = 0;
query Q[N];
check C[N];
void dfs(int u, int par){
tin[u] = ++timer;
for (int v : adj[u]){
if (v == par) continue;
lift[v][0] = u;
dep[v] = dep[u] + 1;
dfs(v, u);
}
tout[u] = timer;
}
int findlca(int a, int b){
if (dep[a] < dep[b]) swap(a, b);
int del = dep[a] - dep[b];
for (int i = 0; i < 20; i++) if (del >> i & 1) a = lift[a][i];
if (a == b) return a;
for (int i = 19; i >= 0; i--) if (lift[a][i] != lift[b][i]){
a = lift[a][i];
b = lift[b][i];
}
return lift[a][0];
}
void updlazy(int l, int r, int pos){
seg[pos] += lazy[pos] * (r - l + 1);
if (l == r){
lazy[pos] = 0;
return;
}
lazy[pos * 2] += lazy[pos];
lazy[pos * 2 + 1] += lazy[pos];
lazy[pos] = 0;
return;
}
void upd(int l, int r, int pos, int ql, int qr, int v){
if (lazy[pos] != 0) updlazy(l, r, pos);
if (l >= ql && r <= qr){
seg[pos] += (r - l + 1) * v;
if (l == r) return;
lazy[pos * 2] += v;
lazy[pos * 2 + 1] += v;
}
else if (l > qr || r < ql) return;
else {
int mid = (l + r)/2;
upd(l, mid, pos*2, ql, qr, v);
upd(mid + 1, r, pos*2 + 1, ql, qr, v);
seg[pos] = seg[pos * 2] + seg[pos * 2 + 1];
}
}
int query(int l, int r, int pos, int qp){
if (lazy[pos] != 0) updlazy(l, r, pos);
if (l == r) return seg[pos];
int mid = (l + r)/2;
if (qp <= mid) return query(l, mid, pos*2, qp);
else return query(mid + 1, r, pos*2 + 1, qp);
}
int querypath(int a, int b, int lc){
return query(1, n, 1, tin[a]) + query(1, n, 1, tin[b]) - 2 * query(1, n, 1, tin[lc]);
}
void pbs(){
for (int i = 1; i <= m; i++) work[i].clear();
for (int i = 1; i <= 4 * n; i++) seg[i] = lazy[i] = 0;
for (int i = 1; i <= q; i++){
if (Q[i].l == Q[i].r) continue;
int m = (Q[i].l + Q[i].r + 1)/2;
work[m].push_back(i);
}
for (int i = 1; i <= m; i++){
upd(1, n, 1, tin[C[i].a], tout[C[i].a], C[i].c);
for (auto x : work[i]){
int val = querypath(Q[x].a, Q[x].b, Q[x].lc);
if (val > Q[x].silver) Q[x].r = i - 1;
else Q[x].l = i;
}
}
}
bool comp(check a, check b){
return a.c < b.c;
}
void Solve()
{
cin >> n >> m >> q;
vector <pair<int, int>> e;
for (int i = 1; i < n; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
e.push_back({u, v});
}
dfs(1, -1);
for (int j = 1; j < 20; j++) for (int i = 1; i <= n; i++) lift[i][j] = lift[lift[i][j - 1]][j - 1];
for (int i = 1; i <= m; i++){
int p, c; cin >> p >> c;
--p;
if (dep[e[p].f] > dep[e[p].s]) C[i].a = e[p].f;
else C[i].a = e[p].s;
C[i].c = c;
}
sort(C + 1, C + m + 1, comp);
for (int i = 1; i <= q; i++){
int a, b, x, y; cin >> a >> b >> x >> y;
Q[i].a = a;
Q[i].b = b;
Q[i].gold = x;
Q[i].silver = y;
Q[i].i = i;
Q[i].lc = findlca(a, b);
Q[i].l = 0;
Q[i].r = m;
}
for (int it = 0; it < 20; it++){
pbs();
}
for (int i = 1; i <= m; i++) work[i].clear();
for (int i = 1; i <= 4 * n; i++) seg[i] = lazy[i] = 0;
for (int i = 1; i <= q; i++){
assert(Q[i].l == Q[i].r);
if (Q[i].l == m){
Q[i].need = 0;
} else {
work[Q[i].l + 1].push_back(i);
}
}
for (int i = m; i >= 1; i--){
upd(1, n, 1, tin[C[i].a], tout[C[i].a], 1);
for (auto x : work[i]){
Q[x].need = querypath(Q[x].a, Q[x].b, Q[x].lc);
}
}
for (int i = 1; i <= q; i++){
int x = i;
if (Q[x].need > Q[x].gold) cout << "-1\n";
else cout << Q[x].gold - Q[x].need << "\n";
}
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5024 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
14 ms |
5844 KB |
Output is correct |
6 |
Correct |
16 ms |
6072 KB |
Output is correct |
7 |
Correct |
13 ms |
5844 KB |
Output is correct |
8 |
Correct |
17 ms |
6084 KB |
Output is correct |
9 |
Correct |
17 ms |
6196 KB |
Output is correct |
10 |
Correct |
17 ms |
6076 KB |
Output is correct |
11 |
Correct |
17 ms |
6184 KB |
Output is correct |
12 |
Correct |
17 ms |
6184 KB |
Output is correct |
13 |
Correct |
18 ms |
6284 KB |
Output is correct |
14 |
Correct |
17 ms |
6216 KB |
Output is correct |
15 |
Correct |
20 ms |
6100 KB |
Output is correct |
16 |
Correct |
20 ms |
6204 KB |
Output is correct |
17 |
Correct |
20 ms |
6184 KB |
Output is correct |
18 |
Correct |
20 ms |
6220 KB |
Output is correct |
19 |
Correct |
16 ms |
6192 KB |
Output is correct |
20 |
Correct |
15 ms |
6100 KB |
Output is correct |
21 |
Correct |
14 ms |
6200 KB |
Output is correct |
22 |
Correct |
14 ms |
6208 KB |
Output is correct |
23 |
Correct |
14 ms |
6100 KB |
Output is correct |
24 |
Correct |
13 ms |
6196 KB |
Output is correct |
25 |
Correct |
13 ms |
6124 KB |
Output is correct |
26 |
Correct |
13 ms |
6100 KB |
Output is correct |
27 |
Correct |
13 ms |
6200 KB |
Output is correct |
28 |
Correct |
13 ms |
6132 KB |
Output is correct |
29 |
Correct |
13 ms |
6100 KB |
Output is correct |
30 |
Correct |
17 ms |
6192 KB |
Output is correct |
31 |
Correct |
17 ms |
6100 KB |
Output is correct |
32 |
Correct |
16 ms |
6188 KB |
Output is correct |
33 |
Correct |
17 ms |
6312 KB |
Output is correct |
34 |
Correct |
18 ms |
6296 KB |
Output is correct |
35 |
Correct |
17 ms |
6240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
18 ms |
6084 KB |
Output is correct |
3 |
Correct |
20 ms |
6136 KB |
Output is correct |
4 |
Correct |
16 ms |
6208 KB |
Output is correct |
5 |
Correct |
1031 ms |
51680 KB |
Output is correct |
6 |
Correct |
1162 ms |
50600 KB |
Output is correct |
7 |
Correct |
1127 ms |
52768 KB |
Output is correct |
8 |
Correct |
876 ms |
48008 KB |
Output is correct |
9 |
Correct |
998 ms |
47136 KB |
Output is correct |
10 |
Correct |
1456 ms |
62060 KB |
Output is correct |
11 |
Correct |
1450 ms |
61972 KB |
Output is correct |
12 |
Correct |
1442 ms |
61960 KB |
Output is correct |
13 |
Correct |
1532 ms |
61824 KB |
Output is correct |
14 |
Correct |
1521 ms |
62176 KB |
Output is correct |
15 |
Correct |
1906 ms |
67224 KB |
Output is correct |
16 |
Correct |
1961 ms |
67628 KB |
Output is correct |
17 |
Correct |
1688 ms |
66924 KB |
Output is correct |
18 |
Correct |
1760 ms |
61604 KB |
Output is correct |
19 |
Correct |
1843 ms |
61676 KB |
Output is correct |
20 |
Correct |
1869 ms |
61620 KB |
Output is correct |
21 |
Correct |
1268 ms |
62716 KB |
Output is correct |
22 |
Correct |
1253 ms |
62516 KB |
Output is correct |
23 |
Correct |
1291 ms |
62652 KB |
Output is correct |
24 |
Correct |
1280 ms |
62572 KB |
Output is correct |
25 |
Correct |
1246 ms |
62048 KB |
Output is correct |
26 |
Correct |
1130 ms |
61428 KB |
Output is correct |
27 |
Correct |
1171 ms |
62288 KB |
Output is correct |
28 |
Correct |
966 ms |
60996 KB |
Output is correct |
29 |
Correct |
979 ms |
59624 KB |
Output is correct |
30 |
Correct |
980 ms |
59516 KB |
Output is correct |
31 |
Correct |
1063 ms |
59500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
19 ms |
6252 KB |
Output is correct |
3 |
Correct |
21 ms |
6236 KB |
Output is correct |
4 |
Correct |
16 ms |
6228 KB |
Output is correct |
5 |
Correct |
984 ms |
56244 KB |
Output is correct |
6 |
Correct |
973 ms |
55492 KB |
Output is correct |
7 |
Correct |
1284 ms |
47332 KB |
Output is correct |
8 |
Correct |
1602 ms |
67852 KB |
Output is correct |
9 |
Correct |
1596 ms |
67780 KB |
Output is correct |
10 |
Correct |
1651 ms |
67844 KB |
Output is correct |
11 |
Correct |
1268 ms |
67944 KB |
Output is correct |
12 |
Correct |
1293 ms |
67880 KB |
Output is correct |
13 |
Correct |
1204 ms |
67808 KB |
Output is correct |
14 |
Correct |
1042 ms |
67656 KB |
Output is correct |
15 |
Correct |
987 ms |
67664 KB |
Output is correct |
16 |
Correct |
1056 ms |
67704 KB |
Output is correct |
17 |
Correct |
1074 ms |
67712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5024 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
14 ms |
5844 KB |
Output is correct |
6 |
Correct |
16 ms |
6072 KB |
Output is correct |
7 |
Correct |
13 ms |
5844 KB |
Output is correct |
8 |
Correct |
17 ms |
6084 KB |
Output is correct |
9 |
Correct |
17 ms |
6196 KB |
Output is correct |
10 |
Correct |
17 ms |
6076 KB |
Output is correct |
11 |
Correct |
17 ms |
6184 KB |
Output is correct |
12 |
Correct |
17 ms |
6184 KB |
Output is correct |
13 |
Correct |
18 ms |
6284 KB |
Output is correct |
14 |
Correct |
17 ms |
6216 KB |
Output is correct |
15 |
Correct |
20 ms |
6100 KB |
Output is correct |
16 |
Correct |
20 ms |
6204 KB |
Output is correct |
17 |
Correct |
20 ms |
6184 KB |
Output is correct |
18 |
Correct |
20 ms |
6220 KB |
Output is correct |
19 |
Correct |
16 ms |
6192 KB |
Output is correct |
20 |
Correct |
15 ms |
6100 KB |
Output is correct |
21 |
Correct |
14 ms |
6200 KB |
Output is correct |
22 |
Correct |
14 ms |
6208 KB |
Output is correct |
23 |
Correct |
14 ms |
6100 KB |
Output is correct |
24 |
Correct |
13 ms |
6196 KB |
Output is correct |
25 |
Correct |
13 ms |
6124 KB |
Output is correct |
26 |
Correct |
13 ms |
6100 KB |
Output is correct |
27 |
Correct |
13 ms |
6200 KB |
Output is correct |
28 |
Correct |
13 ms |
6132 KB |
Output is correct |
29 |
Correct |
13 ms |
6100 KB |
Output is correct |
30 |
Correct |
17 ms |
6192 KB |
Output is correct |
31 |
Correct |
17 ms |
6100 KB |
Output is correct |
32 |
Correct |
16 ms |
6188 KB |
Output is correct |
33 |
Correct |
17 ms |
6312 KB |
Output is correct |
34 |
Correct |
18 ms |
6296 KB |
Output is correct |
35 |
Correct |
17 ms |
6240 KB |
Output is correct |
36 |
Correct |
2 ms |
5076 KB |
Output is correct |
37 |
Correct |
18 ms |
6084 KB |
Output is correct |
38 |
Correct |
20 ms |
6136 KB |
Output is correct |
39 |
Correct |
16 ms |
6208 KB |
Output is correct |
40 |
Correct |
1031 ms |
51680 KB |
Output is correct |
41 |
Correct |
1162 ms |
50600 KB |
Output is correct |
42 |
Correct |
1127 ms |
52768 KB |
Output is correct |
43 |
Correct |
876 ms |
48008 KB |
Output is correct |
44 |
Correct |
998 ms |
47136 KB |
Output is correct |
45 |
Correct |
1456 ms |
62060 KB |
Output is correct |
46 |
Correct |
1450 ms |
61972 KB |
Output is correct |
47 |
Correct |
1442 ms |
61960 KB |
Output is correct |
48 |
Correct |
1532 ms |
61824 KB |
Output is correct |
49 |
Correct |
1521 ms |
62176 KB |
Output is correct |
50 |
Correct |
1906 ms |
67224 KB |
Output is correct |
51 |
Correct |
1961 ms |
67628 KB |
Output is correct |
52 |
Correct |
1688 ms |
66924 KB |
Output is correct |
53 |
Correct |
1760 ms |
61604 KB |
Output is correct |
54 |
Correct |
1843 ms |
61676 KB |
Output is correct |
55 |
Correct |
1869 ms |
61620 KB |
Output is correct |
56 |
Correct |
1268 ms |
62716 KB |
Output is correct |
57 |
Correct |
1253 ms |
62516 KB |
Output is correct |
58 |
Correct |
1291 ms |
62652 KB |
Output is correct |
59 |
Correct |
1280 ms |
62572 KB |
Output is correct |
60 |
Correct |
1246 ms |
62048 KB |
Output is correct |
61 |
Correct |
1130 ms |
61428 KB |
Output is correct |
62 |
Correct |
1171 ms |
62288 KB |
Output is correct |
63 |
Correct |
966 ms |
60996 KB |
Output is correct |
64 |
Correct |
979 ms |
59624 KB |
Output is correct |
65 |
Correct |
980 ms |
59516 KB |
Output is correct |
66 |
Correct |
1063 ms |
59500 KB |
Output is correct |
67 |
Correct |
2 ms |
5076 KB |
Output is correct |
68 |
Correct |
19 ms |
6252 KB |
Output is correct |
69 |
Correct |
21 ms |
6236 KB |
Output is correct |
70 |
Correct |
16 ms |
6228 KB |
Output is correct |
71 |
Correct |
984 ms |
56244 KB |
Output is correct |
72 |
Correct |
973 ms |
55492 KB |
Output is correct |
73 |
Correct |
1284 ms |
47332 KB |
Output is correct |
74 |
Correct |
1602 ms |
67852 KB |
Output is correct |
75 |
Correct |
1596 ms |
67780 KB |
Output is correct |
76 |
Correct |
1651 ms |
67844 KB |
Output is correct |
77 |
Correct |
1268 ms |
67944 KB |
Output is correct |
78 |
Correct |
1293 ms |
67880 KB |
Output is correct |
79 |
Correct |
1204 ms |
67808 KB |
Output is correct |
80 |
Correct |
1042 ms |
67656 KB |
Output is correct |
81 |
Correct |
987 ms |
67664 KB |
Output is correct |
82 |
Correct |
1056 ms |
67704 KB |
Output is correct |
83 |
Correct |
1074 ms |
67712 KB |
Output is correct |
84 |
Correct |
1036 ms |
47536 KB |
Output is correct |
85 |
Correct |
1006 ms |
41328 KB |
Output is correct |
86 |
Correct |
818 ms |
40592 KB |
Output is correct |
87 |
Correct |
1471 ms |
62164 KB |
Output is correct |
88 |
Correct |
1506 ms |
62060 KB |
Output is correct |
89 |
Correct |
1495 ms |
61856 KB |
Output is correct |
90 |
Correct |
1485 ms |
62180 KB |
Output is correct |
91 |
Correct |
1512 ms |
62112 KB |
Output is correct |
92 |
Correct |
1706 ms |
65352 KB |
Output is correct |
93 |
Correct |
1767 ms |
66648 KB |
Output is correct |
94 |
Correct |
1963 ms |
61488 KB |
Output is correct |
95 |
Correct |
1927 ms |
61740 KB |
Output is correct |
96 |
Correct |
1751 ms |
61660 KB |
Output is correct |
97 |
Correct |
1804 ms |
61668 KB |
Output is correct |
98 |
Correct |
1191 ms |
62488 KB |
Output is correct |
99 |
Correct |
1249 ms |
62228 KB |
Output is correct |
100 |
Correct |
1281 ms |
62424 KB |
Output is correct |
101 |
Correct |
1224 ms |
62656 KB |
Output is correct |
102 |
Correct |
1210 ms |
61992 KB |
Output is correct |
103 |
Correct |
1125 ms |
62000 KB |
Output is correct |
104 |
Correct |
1059 ms |
62084 KB |
Output is correct |
105 |
Correct |
964 ms |
59020 KB |
Output is correct |
106 |
Correct |
913 ms |
60396 KB |
Output is correct |
107 |
Correct |
1000 ms |
58900 KB |
Output is correct |
108 |
Correct |
952 ms |
59240 KB |
Output is correct |