#include <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll = long long;
using ld = long double;
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false)
#define N 100005
#define ALL(x) x.begin(), x.end()
struct persist
{
int a; ll b;
persist *l, *r;
persist(int a0, ll b0) : a(a0), b(b0), l(nullptr), r(nullptr) {}
persist(persist *l0, persist *r0) : a((l0?l0->a:0) + (r0?r0->a:0)), b((l0?l0->b:0) + (r0?r0->b:0)), l(l0), r(r0) {}
~persist() {if (l) delete l; if (r) delete r;}
};
persist *build(int l, int r)
{
if (l == r) return new persist(0, 0);
return new persist(build(l, (l+r)/2), build((l+r)/2+1, r));
}
persist *upd(persist *v, int l, int r, ll w, int W)
{
if (l == r) return new persist(v->a + 1, v->b + w);
if (W <= (l+r)/2) return new persist(upd(v->l, l, (l+r)/2, w, W), v->r);
return new persist(v->l, upd(v->r, (l+r)/2+1, r, w, W));
}
vector<ll> silver_cord;
ll qry(persist *u, persist *v, persist *a, int l, int r, ll k)
{
if (l == r)
return min(k, u->b + v->b - 2*a->b) / silver_cord[l];
ll left = u->l->b + v->l->b - a->l->b * 2;
if (k < left) return qry(u->l, v->l, a->l, l, (l+r)/2, k);
return (u->l->a + v->l->a - a->l->a * 2) + qry(u->r, v->r, a->r, (l+r)/2+1, r, k - left);
}
ll qry_count(persist *u, persist *v, persist *a)
{
return 1ll * u->a + v->a - a->a * 2ll;
}
int n, m, q, P[18][N], dep[N], ne;
vector<int> mark[N];
vector<pair<int, int>> g[N];
persist *root[N];
int lca(int u, int v)
{
if (dep[u] < dep[v]) swap(v, u);
int dt = dep[u] - dep[v];
for (int j = 18; j--;) if (dt & (1 << j)) u = P[j][u];
if (u == v) return u;
for (int j = 18; j--;) if (P[j][u] != P[j][v]) u = P[j][u], v = P[j][v];
return P[0][u];
}
void dfs(int u, int p, int d)
{
dep[u] = d; P[0][u] = p; for (int j = 1; j < 18; ++j) P[j][u] = P[j-1][P[j-1][u]];
for (auto [v, i] : g[u])
{
if (v == p) continue;
root[v] = root[u];
for (auto w : mark[i]) root[v] = upd(root[v], 0, ne, w, lower_bound(ALL(silver_cord), w) - silver_cord.begin());
dfs(v, u, d+1);
}
}
int main()
{
ShinLena;
cin >> n >> m >> q;
for (int u, v, i = 1; i < n; ++i) cin >> u >> v, g[u].emplace_back(v, i), g[v].emplace_back(u, i);
for (int p, c; m--; ) cin >> p >> c, mark[p].push_back(c), silver_cord.push_back(c);
sort(ALL(silver_cord)); silver_cord.erase(unique(ALL(silver_cord)), silver_cord.end()); ne = silver_cord.size() - 1;
root[1] = build(0, ne); dfs(1, 1, 1);
for (ll s, t, a, gold, silver; q--; )
{
cin >> s >> t >> gold >> silver;
a = lca(s, t);
persist *ps = root[s], *pt = root[t], *pa = root[a];
int gone = qry(ps, pt, pa, 0, ne, silver), all = qry_count(ps, pt, pa);
cout << ((all - gone > gold) ? -1 : gold - (all - gone)) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12768 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
5 ms |
14188 KB |
Output is correct |
6 |
Correct |
5 ms |
14064 KB |
Output is correct |
7 |
Correct |
4 ms |
13676 KB |
Output is correct |
8 |
Correct |
5 ms |
14172 KB |
Output is correct |
9 |
Correct |
5 ms |
14172 KB |
Output is correct |
10 |
Correct |
5 ms |
14172 KB |
Output is correct |
11 |
Correct |
5 ms |
14172 KB |
Output is correct |
12 |
Correct |
5 ms |
14152 KB |
Output is correct |
13 |
Correct |
6 ms |
14508 KB |
Output is correct |
14 |
Correct |
6 ms |
14456 KB |
Output is correct |
15 |
Correct |
6 ms |
14172 KB |
Output is correct |
16 |
Correct |
5 ms |
14172 KB |
Output is correct |
17 |
Correct |
5 ms |
14364 KB |
Output is correct |
18 |
Correct |
6 ms |
14172 KB |
Output is correct |
19 |
Correct |
5 ms |
14172 KB |
Output is correct |
20 |
Correct |
5 ms |
14172 KB |
Output is correct |
21 |
Correct |
5 ms |
14340 KB |
Output is correct |
22 |
Correct |
5 ms |
14168 KB |
Output is correct |
23 |
Correct |
5 ms |
14172 KB |
Output is correct |
24 |
Correct |
5 ms |
14172 KB |
Output is correct |
25 |
Correct |
5 ms |
14172 KB |
Output is correct |
26 |
Correct |
5 ms |
14168 KB |
Output is correct |
27 |
Correct |
5 ms |
14172 KB |
Output is correct |
28 |
Correct |
5 ms |
14172 KB |
Output is correct |
29 |
Correct |
5 ms |
14204 KB |
Output is correct |
30 |
Correct |
4 ms |
12888 KB |
Output is correct |
31 |
Correct |
4 ms |
12892 KB |
Output is correct |
32 |
Correct |
4 ms |
12892 KB |
Output is correct |
33 |
Correct |
5 ms |
14428 KB |
Output is correct |
34 |
Correct |
6 ms |
14428 KB |
Output is correct |
35 |
Correct |
5 ms |
14452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
4 ms |
12892 KB |
Output is correct |
3 |
Correct |
5 ms |
13144 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
121 ms |
26316 KB |
Output is correct |
6 |
Correct |
94 ms |
24932 KB |
Output is correct |
7 |
Correct |
95 ms |
24260 KB |
Output is correct |
8 |
Correct |
88 ms |
23756 KB |
Output is correct |
9 |
Correct |
87 ms |
24108 KB |
Output is correct |
10 |
Correct |
121 ms |
27652 KB |
Output is correct |
11 |
Correct |
120 ms |
27584 KB |
Output is correct |
12 |
Correct |
130 ms |
27588 KB |
Output is correct |
13 |
Correct |
132 ms |
27664 KB |
Output is correct |
14 |
Correct |
122 ms |
27464 KB |
Output is correct |
15 |
Correct |
142 ms |
36072 KB |
Output is correct |
16 |
Correct |
160 ms |
36928 KB |
Output is correct |
17 |
Correct |
152 ms |
35524 KB |
Output is correct |
18 |
Correct |
133 ms |
27604 KB |
Output is correct |
19 |
Correct |
144 ms |
27432 KB |
Output is correct |
20 |
Correct |
137 ms |
27560 KB |
Output is correct |
21 |
Correct |
97 ms |
27364 KB |
Output is correct |
22 |
Correct |
100 ms |
27456 KB |
Output is correct |
23 |
Correct |
101 ms |
27336 KB |
Output is correct |
24 |
Correct |
99 ms |
27412 KB |
Output is correct |
25 |
Correct |
114 ms |
27332 KB |
Output is correct |
26 |
Correct |
117 ms |
27404 KB |
Output is correct |
27 |
Correct |
127 ms |
27540 KB |
Output is correct |
28 |
Correct |
106 ms |
27620 KB |
Output is correct |
29 |
Correct |
108 ms |
27592 KB |
Output is correct |
30 |
Correct |
119 ms |
27528 KB |
Output is correct |
31 |
Correct |
110 ms |
27504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
5 ms |
14424 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
280 ms |
87816 KB |
Output is correct |
6 |
Correct |
261 ms |
74704 KB |
Output is correct |
7 |
Correct |
371 ms |
110588 KB |
Output is correct |
8 |
Correct |
520 ms |
127428 KB |
Output is correct |
9 |
Correct |
518 ms |
127116 KB |
Output is correct |
10 |
Correct |
519 ms |
127276 KB |
Output is correct |
11 |
Correct |
438 ms |
126912 KB |
Output is correct |
12 |
Correct |
433 ms |
126336 KB |
Output is correct |
13 |
Correct |
442 ms |
126780 KB |
Output is correct |
14 |
Correct |
289 ms |
127168 KB |
Output is correct |
15 |
Correct |
300 ms |
127084 KB |
Output is correct |
16 |
Correct |
345 ms |
127184 KB |
Output is correct |
17 |
Correct |
383 ms |
126920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12768 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
5 ms |
14188 KB |
Output is correct |
6 |
Correct |
5 ms |
14064 KB |
Output is correct |
7 |
Correct |
4 ms |
13676 KB |
Output is correct |
8 |
Correct |
5 ms |
14172 KB |
Output is correct |
9 |
Correct |
5 ms |
14172 KB |
Output is correct |
10 |
Correct |
5 ms |
14172 KB |
Output is correct |
11 |
Correct |
5 ms |
14172 KB |
Output is correct |
12 |
Correct |
5 ms |
14152 KB |
Output is correct |
13 |
Correct |
6 ms |
14508 KB |
Output is correct |
14 |
Correct |
6 ms |
14456 KB |
Output is correct |
15 |
Correct |
6 ms |
14172 KB |
Output is correct |
16 |
Correct |
5 ms |
14172 KB |
Output is correct |
17 |
Correct |
5 ms |
14364 KB |
Output is correct |
18 |
Correct |
6 ms |
14172 KB |
Output is correct |
19 |
Correct |
5 ms |
14172 KB |
Output is correct |
20 |
Correct |
5 ms |
14172 KB |
Output is correct |
21 |
Correct |
5 ms |
14340 KB |
Output is correct |
22 |
Correct |
5 ms |
14168 KB |
Output is correct |
23 |
Correct |
5 ms |
14172 KB |
Output is correct |
24 |
Correct |
5 ms |
14172 KB |
Output is correct |
25 |
Correct |
5 ms |
14172 KB |
Output is correct |
26 |
Correct |
5 ms |
14168 KB |
Output is correct |
27 |
Correct |
5 ms |
14172 KB |
Output is correct |
28 |
Correct |
5 ms |
14172 KB |
Output is correct |
29 |
Correct |
5 ms |
14204 KB |
Output is correct |
30 |
Correct |
4 ms |
12888 KB |
Output is correct |
31 |
Correct |
4 ms |
12892 KB |
Output is correct |
32 |
Correct |
4 ms |
12892 KB |
Output is correct |
33 |
Correct |
5 ms |
14428 KB |
Output is correct |
34 |
Correct |
6 ms |
14428 KB |
Output is correct |
35 |
Correct |
5 ms |
14452 KB |
Output is correct |
36 |
Correct |
2 ms |
12636 KB |
Output is correct |
37 |
Correct |
4 ms |
12892 KB |
Output is correct |
38 |
Correct |
5 ms |
13144 KB |
Output is correct |
39 |
Correct |
4 ms |
12892 KB |
Output is correct |
40 |
Correct |
121 ms |
26316 KB |
Output is correct |
41 |
Correct |
94 ms |
24932 KB |
Output is correct |
42 |
Correct |
95 ms |
24260 KB |
Output is correct |
43 |
Correct |
88 ms |
23756 KB |
Output is correct |
44 |
Correct |
87 ms |
24108 KB |
Output is correct |
45 |
Correct |
121 ms |
27652 KB |
Output is correct |
46 |
Correct |
120 ms |
27584 KB |
Output is correct |
47 |
Correct |
130 ms |
27588 KB |
Output is correct |
48 |
Correct |
132 ms |
27664 KB |
Output is correct |
49 |
Correct |
122 ms |
27464 KB |
Output is correct |
50 |
Correct |
142 ms |
36072 KB |
Output is correct |
51 |
Correct |
160 ms |
36928 KB |
Output is correct |
52 |
Correct |
152 ms |
35524 KB |
Output is correct |
53 |
Correct |
133 ms |
27604 KB |
Output is correct |
54 |
Correct |
144 ms |
27432 KB |
Output is correct |
55 |
Correct |
137 ms |
27560 KB |
Output is correct |
56 |
Correct |
97 ms |
27364 KB |
Output is correct |
57 |
Correct |
100 ms |
27456 KB |
Output is correct |
58 |
Correct |
101 ms |
27336 KB |
Output is correct |
59 |
Correct |
99 ms |
27412 KB |
Output is correct |
60 |
Correct |
114 ms |
27332 KB |
Output is correct |
61 |
Correct |
117 ms |
27404 KB |
Output is correct |
62 |
Correct |
127 ms |
27540 KB |
Output is correct |
63 |
Correct |
106 ms |
27620 KB |
Output is correct |
64 |
Correct |
108 ms |
27592 KB |
Output is correct |
65 |
Correct |
119 ms |
27528 KB |
Output is correct |
66 |
Correct |
110 ms |
27504 KB |
Output is correct |
67 |
Correct |
2 ms |
12632 KB |
Output is correct |
68 |
Correct |
5 ms |
14424 KB |
Output is correct |
69 |
Correct |
5 ms |
14428 KB |
Output is correct |
70 |
Correct |
5 ms |
14428 KB |
Output is correct |
71 |
Correct |
280 ms |
87816 KB |
Output is correct |
72 |
Correct |
261 ms |
74704 KB |
Output is correct |
73 |
Correct |
371 ms |
110588 KB |
Output is correct |
74 |
Correct |
520 ms |
127428 KB |
Output is correct |
75 |
Correct |
518 ms |
127116 KB |
Output is correct |
76 |
Correct |
519 ms |
127276 KB |
Output is correct |
77 |
Correct |
438 ms |
126912 KB |
Output is correct |
78 |
Correct |
433 ms |
126336 KB |
Output is correct |
79 |
Correct |
442 ms |
126780 KB |
Output is correct |
80 |
Correct |
289 ms |
127168 KB |
Output is correct |
81 |
Correct |
300 ms |
127084 KB |
Output is correct |
82 |
Correct |
345 ms |
127184 KB |
Output is correct |
83 |
Correct |
383 ms |
126920 KB |
Output is correct |
84 |
Correct |
327 ms |
109232 KB |
Output is correct |
85 |
Correct |
304 ms |
98972 KB |
Output is correct |
86 |
Correct |
239 ms |
72868 KB |
Output is correct |
87 |
Correct |
434 ms |
117524 KB |
Output is correct |
88 |
Correct |
407 ms |
117508 KB |
Output is correct |
89 |
Correct |
434 ms |
117632 KB |
Output is correct |
90 |
Correct |
409 ms |
117640 KB |
Output is correct |
91 |
Correct |
412 ms |
117584 KB |
Output is correct |
92 |
Correct |
644 ms |
123880 KB |
Output is correct |
93 |
Correct |
622 ms |
125640 KB |
Output is correct |
94 |
Correct |
488 ms |
117448 KB |
Output is correct |
95 |
Correct |
532 ms |
117664 KB |
Output is correct |
96 |
Correct |
536 ms |
117644 KB |
Output is correct |
97 |
Correct |
494 ms |
117548 KB |
Output is correct |
98 |
Correct |
338 ms |
117236 KB |
Output is correct |
99 |
Correct |
330 ms |
117340 KB |
Output is correct |
100 |
Correct |
375 ms |
117128 KB |
Output is correct |
101 |
Correct |
357 ms |
117420 KB |
Output is correct |
102 |
Correct |
389 ms |
117444 KB |
Output is correct |
103 |
Correct |
386 ms |
117100 KB |
Output is correct |
104 |
Correct |
400 ms |
117516 KB |
Output is correct |
105 |
Correct |
274 ms |
117600 KB |
Output is correct |
106 |
Correct |
273 ms |
117432 KB |
Output is correct |
107 |
Correct |
305 ms |
117604 KB |
Output is correct |
108 |
Correct |
329 ms |
117528 KB |
Output is correct |