#include <bits/stdc++.h>
using namespace std;
namespace
{
template <typename Fun>
struct YCombinator
{
template <typename T>
YCombinator(T &&_fun) : fun(forward<T>(_fun)) {}
template <typename... Args>
decltype(auto) operator()(Args &&...args) { return fun(ref(*this), forward<Args>(args)...); }
private:
Fun fun;
};
template <typename T>
YCombinator(T &&) -> YCombinator<decay_t<T>>;
} // namespace
struct DSU
{
DSU(int n) : e(n, -1) {}
int find(int u) { return e[u] < 0 ? u : e[u] = find(e[u]); }
bool same(int u, int v) { return find(u) == find(v); }
bool unite(int u, int v)
{
u = find(u);
v = find(v);
if (u == v)
return 0;
if (e[u] > e[v])
swap(u, v);
e[u] += e[v];
e[v] = u;
return 1;
}
vector<int> e;
};
template <typename T>
struct Fenwick
{
Fenwick(int _n) : n(_n), tree(n + 1) {}
T sum(int p)
{
T ret = 0;
for (; p; p -= p & -p)
ret += tree[p];
return ret;
}
void add(int p, T x)
{
for (p++; p <= n; p += p & -p)
tree[p] += x;
}
int n;
vector<T> tree;
};
void solve()
{
int n, m, q;
cin >> n >> m >> q;
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < n - 1; i++)
{
int u, v;
cin >> u >> v, --u, --v;
adj[u].emplace_back(v, i);
adj[v].emplace_back(u, i);
}
vector<pair<long, int>> a(m);
for (auto &[x, j] : a)
cin >> j >> x, --j;
sort(a.begin(), a.end());
vector<int> s(q), t(q);
vector<long> gold(q), silver(q);
for (int i = 0; i < q; i++)
cin >> s[i] >> t[i] >> gold[i] >> silver[i], --s[i], --t[i];
vector<int> to(n - 1), tin(n), tout(n), pref(n);
int timer = 0;
YCombinator(
[&](auto self, int u, int p) -> void
{
tin[u] = timer++;
for (auto [v, j] : adj[u])
{
if (v != p)
{
to[j] = v;
self(v, u);
}
}
tout[u] = timer;
})(0, -1);
for (auto [x, j] : a)
pref[to[j]]++;
YCombinator(
[&](auto self, int u, int p) -> void
{
if (p != -1)
pref[u] += pref[p];
for (auto [v, j] : adj[u])
{
if (v != p)
self(v, u);
}
})(0, -1);
for (int i = 0; i < q; i++)
{
if (tin[s[i]] > tin[t[i]])
swap(s[i], t[i]);
}
vector<int> lca(q), cnt(q);
{
vector<int> anc(n);
DSU dsu(n);
vector<vector<int>> todo(n);
for (int i = 0; i < q; i++)
todo[t[i]].push_back(i);
YCombinator(
[&](auto self, int u, int p) -> void
{
anc[u] = u;
for (auto [v, j] : adj[u])
{
if (v != p)
{
self(v, u);
dsu.unite(u, v);
anc[dsu.find(u)] = u;
}
}
for (int j : todo[u])
lca[j] = anc[dsu.find(s[j])];
})(0, -1);
}
for (int i = 0; i < q; i++)
cnt[i] = pref[s[i]] + pref[t[i]] - 2 * pref[lca[i]];
vector res = cnt;
vector<int> lo(q, 0), hi(q, m);
while (1)
{
vector<vector<int>> met(m);
bool flag = 0;
for (int i = 0; i < q; i++)
{
if (lo[i] < hi[i])
{
met[(lo[i] + hi[i] - 1) / 2].push_back(i);
flag = 1;
}
}
if (!flag)
break;
Fenwick<long> fenw_cost(n);
Fenwick<int> fenw_cnt(n);
auto path_cost = [&](int k) -> long
{ return fenw_cost.sum(tin[s[k]] + 1) + fenw_cost.sum(tin[t[k]] + 1) - 2 * fenw_cost.sum(tin[lca[k]] + 1); };
auto path_cnt = [&](int k) -> int
{ return fenw_cnt.sum(tin[s[k]] + 1) + fenw_cnt.sum(tin[t[k]] + 1) - 2 * fenw_cnt.sum(tin[lca[k]] + 1); };
for (int i = 0; i < m; i++)
{
auto [x, j] = a[i];
fenw_cost.add(tin[to[j]], x);
fenw_cost.add(tout[to[j]], -x);
fenw_cnt.add(tin[to[j]], 1);
fenw_cnt.add(tout[to[j]], -1);
for (int k : met[i])
{
if (path_cost(k) > silver[k])
hi[k] = i;
else
{
res[k] = cnt[k] - path_cnt(k);
lo[k] = i + 1;
}
}
}
}
for (int i = 0; i < q; i++)
cout << max(gold[i] - res[i], -1l) << '\n';
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(NULL);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
5 ms |
676 KB |
Output is correct |
6 |
Correct |
6 ms |
776 KB |
Output is correct |
7 |
Correct |
5 ms |
660 KB |
Output is correct |
8 |
Correct |
9 ms |
776 KB |
Output is correct |
9 |
Correct |
7 ms |
680 KB |
Output is correct |
10 |
Correct |
6 ms |
724 KB |
Output is correct |
11 |
Correct |
6 ms |
724 KB |
Output is correct |
12 |
Correct |
8 ms |
680 KB |
Output is correct |
13 |
Correct |
6 ms |
1076 KB |
Output is correct |
14 |
Correct |
7 ms |
904 KB |
Output is correct |
15 |
Correct |
6 ms |
908 KB |
Output is correct |
16 |
Correct |
7 ms |
852 KB |
Output is correct |
17 |
Correct |
7 ms |
836 KB |
Output is correct |
18 |
Correct |
6 ms |
852 KB |
Output is correct |
19 |
Correct |
7 ms |
724 KB |
Output is correct |
20 |
Correct |
6 ms |
676 KB |
Output is correct |
21 |
Correct |
6 ms |
724 KB |
Output is correct |
22 |
Correct |
6 ms |
688 KB |
Output is correct |
23 |
Correct |
7 ms |
772 KB |
Output is correct |
24 |
Correct |
7 ms |
780 KB |
Output is correct |
25 |
Correct |
6 ms |
724 KB |
Output is correct |
26 |
Correct |
6 ms |
764 KB |
Output is correct |
27 |
Correct |
5 ms |
724 KB |
Output is correct |
28 |
Correct |
5 ms |
724 KB |
Output is correct |
29 |
Correct |
5 ms |
680 KB |
Output is correct |
30 |
Correct |
6 ms |
684 KB |
Output is correct |
31 |
Correct |
7 ms |
724 KB |
Output is correct |
32 |
Correct |
6 ms |
724 KB |
Output is correct |
33 |
Correct |
6 ms |
1108 KB |
Output is correct |
34 |
Correct |
6 ms |
1024 KB |
Output is correct |
35 |
Correct |
6 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
7 ms |
772 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Output is correct |
4 |
Correct |
7 ms |
724 KB |
Output is correct |
5 |
Correct |
451 ms |
17632 KB |
Output is correct |
6 |
Correct |
560 ms |
16884 KB |
Output is correct |
7 |
Correct |
461 ms |
17212 KB |
Output is correct |
8 |
Correct |
405 ms |
16448 KB |
Output is correct |
9 |
Correct |
378 ms |
15760 KB |
Output is correct |
10 |
Correct |
633 ms |
20896 KB |
Output is correct |
11 |
Correct |
655 ms |
20940 KB |
Output is correct |
12 |
Correct |
636 ms |
20408 KB |
Output is correct |
13 |
Correct |
702 ms |
20496 KB |
Output is correct |
14 |
Correct |
582 ms |
20560 KB |
Output is correct |
15 |
Correct |
675 ms |
35828 KB |
Output is correct |
16 |
Correct |
710 ms |
37288 KB |
Output is correct |
17 |
Correct |
602 ms |
35200 KB |
Output is correct |
18 |
Correct |
732 ms |
20000 KB |
Output is correct |
19 |
Correct |
698 ms |
20372 KB |
Output is correct |
20 |
Correct |
673 ms |
20524 KB |
Output is correct |
21 |
Correct |
517 ms |
20636 KB |
Output is correct |
22 |
Correct |
561 ms |
20608 KB |
Output is correct |
23 |
Correct |
558 ms |
20976 KB |
Output is correct |
24 |
Correct |
522 ms |
20348 KB |
Output is correct |
25 |
Correct |
572 ms |
20232 KB |
Output is correct |
26 |
Correct |
534 ms |
20544 KB |
Output is correct |
27 |
Correct |
569 ms |
21000 KB |
Output is correct |
28 |
Correct |
469 ms |
19600 KB |
Output is correct |
29 |
Correct |
405 ms |
19372 KB |
Output is correct |
30 |
Correct |
450 ms |
19864 KB |
Output is correct |
31 |
Correct |
436 ms |
19852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1100 KB |
Output is correct |
3 |
Correct |
8 ms |
1108 KB |
Output is correct |
4 |
Correct |
7 ms |
1108 KB |
Output is correct |
5 |
Correct |
354 ms |
32708 KB |
Output is correct |
6 |
Correct |
331 ms |
31884 KB |
Output is correct |
7 |
Correct |
465 ms |
24220 KB |
Output is correct |
8 |
Correct |
592 ms |
37740 KB |
Output is correct |
9 |
Correct |
583 ms |
36908 KB |
Output is correct |
10 |
Correct |
589 ms |
37376 KB |
Output is correct |
11 |
Correct |
583 ms |
36976 KB |
Output is correct |
12 |
Correct |
563 ms |
36752 KB |
Output is correct |
13 |
Correct |
568 ms |
36876 KB |
Output is correct |
14 |
Correct |
468 ms |
36404 KB |
Output is correct |
15 |
Correct |
448 ms |
36504 KB |
Output is correct |
16 |
Correct |
487 ms |
36644 KB |
Output is correct |
17 |
Correct |
501 ms |
36560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
5 ms |
676 KB |
Output is correct |
6 |
Correct |
6 ms |
776 KB |
Output is correct |
7 |
Correct |
5 ms |
660 KB |
Output is correct |
8 |
Correct |
9 ms |
776 KB |
Output is correct |
9 |
Correct |
7 ms |
680 KB |
Output is correct |
10 |
Correct |
6 ms |
724 KB |
Output is correct |
11 |
Correct |
6 ms |
724 KB |
Output is correct |
12 |
Correct |
8 ms |
680 KB |
Output is correct |
13 |
Correct |
6 ms |
1076 KB |
Output is correct |
14 |
Correct |
7 ms |
904 KB |
Output is correct |
15 |
Correct |
6 ms |
908 KB |
Output is correct |
16 |
Correct |
7 ms |
852 KB |
Output is correct |
17 |
Correct |
7 ms |
836 KB |
Output is correct |
18 |
Correct |
6 ms |
852 KB |
Output is correct |
19 |
Correct |
7 ms |
724 KB |
Output is correct |
20 |
Correct |
6 ms |
676 KB |
Output is correct |
21 |
Correct |
6 ms |
724 KB |
Output is correct |
22 |
Correct |
6 ms |
688 KB |
Output is correct |
23 |
Correct |
7 ms |
772 KB |
Output is correct |
24 |
Correct |
7 ms |
780 KB |
Output is correct |
25 |
Correct |
6 ms |
724 KB |
Output is correct |
26 |
Correct |
6 ms |
764 KB |
Output is correct |
27 |
Correct |
5 ms |
724 KB |
Output is correct |
28 |
Correct |
5 ms |
724 KB |
Output is correct |
29 |
Correct |
5 ms |
680 KB |
Output is correct |
30 |
Correct |
6 ms |
684 KB |
Output is correct |
31 |
Correct |
7 ms |
724 KB |
Output is correct |
32 |
Correct |
6 ms |
724 KB |
Output is correct |
33 |
Correct |
6 ms |
1108 KB |
Output is correct |
34 |
Correct |
6 ms |
1024 KB |
Output is correct |
35 |
Correct |
6 ms |
1108 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
7 ms |
772 KB |
Output is correct |
38 |
Correct |
6 ms |
768 KB |
Output is correct |
39 |
Correct |
7 ms |
724 KB |
Output is correct |
40 |
Correct |
451 ms |
17632 KB |
Output is correct |
41 |
Correct |
560 ms |
16884 KB |
Output is correct |
42 |
Correct |
461 ms |
17212 KB |
Output is correct |
43 |
Correct |
405 ms |
16448 KB |
Output is correct |
44 |
Correct |
378 ms |
15760 KB |
Output is correct |
45 |
Correct |
633 ms |
20896 KB |
Output is correct |
46 |
Correct |
655 ms |
20940 KB |
Output is correct |
47 |
Correct |
636 ms |
20408 KB |
Output is correct |
48 |
Correct |
702 ms |
20496 KB |
Output is correct |
49 |
Correct |
582 ms |
20560 KB |
Output is correct |
50 |
Correct |
675 ms |
35828 KB |
Output is correct |
51 |
Correct |
710 ms |
37288 KB |
Output is correct |
52 |
Correct |
602 ms |
35200 KB |
Output is correct |
53 |
Correct |
732 ms |
20000 KB |
Output is correct |
54 |
Correct |
698 ms |
20372 KB |
Output is correct |
55 |
Correct |
673 ms |
20524 KB |
Output is correct |
56 |
Correct |
517 ms |
20636 KB |
Output is correct |
57 |
Correct |
561 ms |
20608 KB |
Output is correct |
58 |
Correct |
558 ms |
20976 KB |
Output is correct |
59 |
Correct |
522 ms |
20348 KB |
Output is correct |
60 |
Correct |
572 ms |
20232 KB |
Output is correct |
61 |
Correct |
534 ms |
20544 KB |
Output is correct |
62 |
Correct |
569 ms |
21000 KB |
Output is correct |
63 |
Correct |
469 ms |
19600 KB |
Output is correct |
64 |
Correct |
405 ms |
19372 KB |
Output is correct |
65 |
Correct |
450 ms |
19864 KB |
Output is correct |
66 |
Correct |
436 ms |
19852 KB |
Output is correct |
67 |
Correct |
0 ms |
212 KB |
Output is correct |
68 |
Correct |
6 ms |
1100 KB |
Output is correct |
69 |
Correct |
8 ms |
1108 KB |
Output is correct |
70 |
Correct |
7 ms |
1108 KB |
Output is correct |
71 |
Correct |
354 ms |
32708 KB |
Output is correct |
72 |
Correct |
331 ms |
31884 KB |
Output is correct |
73 |
Correct |
465 ms |
24220 KB |
Output is correct |
74 |
Correct |
592 ms |
37740 KB |
Output is correct |
75 |
Correct |
583 ms |
36908 KB |
Output is correct |
76 |
Correct |
589 ms |
37376 KB |
Output is correct |
77 |
Correct |
583 ms |
36976 KB |
Output is correct |
78 |
Correct |
563 ms |
36752 KB |
Output is correct |
79 |
Correct |
568 ms |
36876 KB |
Output is correct |
80 |
Correct |
468 ms |
36404 KB |
Output is correct |
81 |
Correct |
448 ms |
36504 KB |
Output is correct |
82 |
Correct |
487 ms |
36644 KB |
Output is correct |
83 |
Correct |
501 ms |
36560 KB |
Output is correct |
84 |
Correct |
481 ms |
16624 KB |
Output is correct |
85 |
Correct |
436 ms |
14208 KB |
Output is correct |
86 |
Correct |
391 ms |
13056 KB |
Output is correct |
87 |
Correct |
746 ms |
20944 KB |
Output is correct |
88 |
Correct |
686 ms |
20576 KB |
Output is correct |
89 |
Correct |
721 ms |
20808 KB |
Output is correct |
90 |
Correct |
777 ms |
20576 KB |
Output is correct |
91 |
Correct |
728 ms |
20964 KB |
Output is correct |
92 |
Correct |
667 ms |
30860 KB |
Output is correct |
93 |
Correct |
680 ms |
34100 KB |
Output is correct |
94 |
Correct |
752 ms |
21112 KB |
Output is correct |
95 |
Correct |
865 ms |
20500 KB |
Output is correct |
96 |
Correct |
830 ms |
20228 KB |
Output is correct |
97 |
Correct |
740 ms |
20420 KB |
Output is correct |
98 |
Correct |
638 ms |
20620 KB |
Output is correct |
99 |
Correct |
645 ms |
20428 KB |
Output is correct |
100 |
Correct |
578 ms |
20852 KB |
Output is correct |
101 |
Correct |
630 ms |
20580 KB |
Output is correct |
102 |
Correct |
630 ms |
20396 KB |
Output is correct |
103 |
Correct |
653 ms |
20784 KB |
Output is correct |
104 |
Correct |
602 ms |
19832 KB |
Output is correct |
105 |
Correct |
531 ms |
19696 KB |
Output is correct |
106 |
Correct |
535 ms |
19468 KB |
Output is correct |
107 |
Correct |
548 ms |
19744 KB |
Output is correct |
108 |
Correct |
462 ms |
19716 KB |
Output is correct |