#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define int ll
using namespace std;
using ll = long long;
using db = long double; // or double, if TL is tight
using str = string;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
template<class T>
struct AIB {
int n;
vector<T> V;
AIB(int N) : n(N + 1), V(N + 1) {}
void update(int p, T v) {
++p;
while(p < n) {
V[p] += v;
p += p & -p;
}
}
T query(int p) {
++p;
T re = 0;
while(p) {
re += V[p];
p -= p & -p;
}
return re;
}
};
struct tree {
int n;
vi par, niv, in, out;
vector<vector<pair<int, ll> > > L;
vector<vi> Anc;
AIB<int> NR;
AIB<ll> COST;
tree(int N) : n(N), L(N), par(N), niv(N), in(N),
out(N), NR(2 * N), COST(2 * N) {}
void add_edge(int u, int v, ll w) {
L[u].push_back({v, w});
L[v].push_back({u, w});
}
void init() {
function<void(int, int, int)> dfs = [&](int u, int p, int li) {
static int tmr = 0;
niv[u] = li;
in[u] = tmr++;
par[u] = p;
for(auto [it, w] : L[u]) {
if(it != p) {
dfs(it, u, li + 1);
}
}
out[u] = tmr++;
};
dfs(0, -1, 0);
Anc.push_back(par);
for(int k = 1; (1 << k) <= n; ++k) {
Anc.push_back(vi(n, -1));
for(int i = 0; i < n; ++i) {
Anc[k][i] = Anc[k-1][i] == -1 ? -1 : Anc[k-1][Anc[k-1][i]];
}
}
}
int lift(int u, int nr) {
for(int i = 0; i < (int)sz(Anc); ++i) {
if(nr & (1 << i))
u = Anc[i][u];
}
return u;
}
int lca(int u, int v) {
if(niv[u] > niv[v]) swap(u, v);
v = lift(v, niv[v] - niv[u]);
if(u == v) return u;
for(int i = int(sz(Anc)) - 1; i >= 0; --i) {
if(Anc[i][u] != Anc[i][v]) {
u = Anc[i][u];
v = Anc[i][v];
}
}
return par[u];
}
int dist(int u, int v) { return niv[u] + niv[v] - 2 * niv[lca(u, v)]; }
void enable(int u, int v, ll w) {
if(niv[u] > niv[v]) swap(u, v);
NR.update(in[v], 1);
NR.update(out[v], -1);
COST.update(in[v], w);
COST.update(out[v], -w);
}
void disable(int u, int v, ll w) {
if(niv[u] > niv[v]) swap(u, v);
NR.update(in[v], -1);
NR.update(out[v], 1);
COST.update(in[v], -w);
COST.update(out[v], w);
}
pair<ll, int> query(int u, int v) {
int l = lca(u, v);
int nr = NR.query(in[u]) + NR.query(in[v]) - 2 * NR.query(in[l]);
ll re = COST.query(in[u]) + COST.query(in[v]) - 2 * COST.query(in[l]);
return {re, nr};
}
};
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, m, q;
cin >> n >> m >> q;
vector<tuple<int, int, ll> > E;
for(int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
--u; --v;
E.push_back({u, v, 0});
}
vector<pair<int, ll> > P;
for(int i = 0; i < m; ++i) {
int p, c;
cin >> p >> c;
P.emplace_back(p, c);
}
tree Tr(n);
for(auto [u, v, w] : E) {
Tr.add_edge(u, v, w);
}
Tr.init();
vi S(q), T(q), X(q), Y(q), St(q), Dr(q), Mij(q), UltNr(q), Total(q);
for(int i = 0; i < q; ++i) {
cin >> S[i] >> T[i] >> X[i] >> Y[i];
--S[i]; --T[i];
St[i] = 0;
Dr[i] = m;
}
sort(all(P), [&](auto a, auto b) { return a.second < b.second; });
int schimbat = 1;
while(schimbat) {
schimbat = 0;
vector<vi> Q(m + 1);///query-uri de dinainte de a aduaga a i - 1 -a muchie
for(int i = 0; i < q; ++i) {
if(St[i] != Dr[i]) {
schimbat = 1;
}
Mij[i] = (St[i] + Dr[i] + 1) / 2;
Q[Mij[i]].push_back(i);
}
for(int i = 0; i <= m; ++i) {
if(i) {
auto [p, w] = P[i - 1];
auto [u, v, ceva] = E[p - 1];
Tr.enable(u, v, w);
}
for(auto it : Q[i]) {
auto [cost, nr] = Tr.query(S[it], T[it]);
if(cost <= Y[it]) {
St[it] = Mij[it];
UltNr[it] = nr;
} else {
Dr[it] = Mij[it] - 1;
}
}
}
if(!schimbat) {
for(int i = 0; i < q; ++i) {
Total[i] = Tr.query(S[i], T[i]).second;
}
}
for(int i = 0; i <= m; ++i) {
if(i) {
auto [p, w] = P[i - 1];
auto [u, v, ceva] = E[p - 1];
Tr.disable(u, v, w);
}
}
}
for(int i = 0; i < q; ++i) {
int ramase = X[i] - Total[i] + UltNr[i];
//printf("Pt query %d dist=%d UltNr=%d\n", i, Tr.dist(S[i], T[i]), UltNr[i]);
cout << max(ramase, -1ll) << "\n";
}
return 0;
}
Compilation message
currencies.cpp: In constructor 'tree::tree(ll)':
currencies.cpp:48:37: warning: 'tree::L' will be initialized after [-Wreorder]
48 | vector<vector<pair<int, ll> > > L;
| ^
currencies.cpp:47:8: warning: 'vi tree::par' [-Wreorder]
47 | vi par, niv, in, out;
| ^~~
currencies.cpp:54:5: warning: when initialized here [-Wreorder]
54 | tree(int N) : n(N), L(N), par(N), niv(N), in(N),
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
860 KB |
Output is correct |
6 |
Correct |
13 ms |
1116 KB |
Output is correct |
7 |
Correct |
10 ms |
1004 KB |
Output is correct |
8 |
Correct |
14 ms |
1116 KB |
Output is correct |
9 |
Correct |
9 ms |
1116 KB |
Output is correct |
10 |
Correct |
9 ms |
1112 KB |
Output is correct |
11 |
Correct |
14 ms |
1212 KB |
Output is correct |
12 |
Correct |
14 ms |
1116 KB |
Output is correct |
13 |
Correct |
13 ms |
1368 KB |
Output is correct |
14 |
Correct |
10 ms |
1116 KB |
Output is correct |
15 |
Correct |
10 ms |
1116 KB |
Output is correct |
16 |
Correct |
16 ms |
1112 KB |
Output is correct |
17 |
Correct |
10 ms |
1116 KB |
Output is correct |
18 |
Correct |
10 ms |
1112 KB |
Output is correct |
19 |
Correct |
8 ms |
1116 KB |
Output is correct |
20 |
Correct |
8 ms |
1116 KB |
Output is correct |
21 |
Correct |
6 ms |
1208 KB |
Output is correct |
22 |
Correct |
7 ms |
1112 KB |
Output is correct |
23 |
Correct |
8 ms |
1116 KB |
Output is correct |
24 |
Correct |
8 ms |
1220 KB |
Output is correct |
25 |
Correct |
7 ms |
1116 KB |
Output is correct |
26 |
Correct |
6 ms |
1116 KB |
Output is correct |
27 |
Correct |
6 ms |
1212 KB |
Output is correct |
28 |
Correct |
11 ms |
1112 KB |
Output is correct |
29 |
Correct |
6 ms |
1112 KB |
Output is correct |
30 |
Correct |
8 ms |
1116 KB |
Output is correct |
31 |
Correct |
8 ms |
1116 KB |
Output is correct |
32 |
Correct |
9 ms |
1112 KB |
Output is correct |
33 |
Correct |
10 ms |
1368 KB |
Output is correct |
34 |
Correct |
8 ms |
1368 KB |
Output is correct |
35 |
Correct |
13 ms |
1400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
14 ms |
1116 KB |
Output is correct |
3 |
Correct |
9 ms |
1116 KB |
Output is correct |
4 |
Correct |
9 ms |
1116 KB |
Output is correct |
5 |
Correct |
2009 ms |
39044 KB |
Output is correct |
6 |
Correct |
1984 ms |
32536 KB |
Output is correct |
7 |
Correct |
2082 ms |
35464 KB |
Output is correct |
8 |
Correct |
1841 ms |
35320 KB |
Output is correct |
9 |
Correct |
1863 ms |
34096 KB |
Output is correct |
10 |
Correct |
3088 ms |
42628 KB |
Output is correct |
11 |
Correct |
2904 ms |
42632 KB |
Output is correct |
12 |
Correct |
2829 ms |
42624 KB |
Output is correct |
13 |
Correct |
2731 ms |
42784 KB |
Output is correct |
14 |
Correct |
3180 ms |
42632 KB |
Output is correct |
15 |
Correct |
4035 ms |
51816 KB |
Output is correct |
16 |
Correct |
4153 ms |
52116 KB |
Output is correct |
17 |
Correct |
3926 ms |
51428 KB |
Output is correct |
18 |
Correct |
3757 ms |
42868 KB |
Output is correct |
19 |
Correct |
4492 ms |
43012 KB |
Output is correct |
20 |
Correct |
3605 ms |
48628 KB |
Output is correct |
21 |
Correct |
2244 ms |
46836 KB |
Output is correct |
22 |
Correct |
2279 ms |
47000 KB |
Output is correct |
23 |
Correct |
2598 ms |
47052 KB |
Output is correct |
24 |
Correct |
2428 ms |
47064 KB |
Output is correct |
25 |
Correct |
2153 ms |
48512 KB |
Output is correct |
26 |
Correct |
2634 ms |
49024 KB |
Output is correct |
27 |
Correct |
2201 ms |
48280 KB |
Output is correct |
28 |
Correct |
1070 ms |
47848 KB |
Output is correct |
29 |
Correct |
1412 ms |
47576 KB |
Output is correct |
30 |
Correct |
1684 ms |
47804 KB |
Output is correct |
31 |
Correct |
1600 ms |
47852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
10 ms |
1372 KB |
Output is correct |
3 |
Correct |
13 ms |
1372 KB |
Output is correct |
4 |
Correct |
13 ms |
1368 KB |
Output is correct |
5 |
Correct |
2260 ms |
46300 KB |
Output is correct |
6 |
Correct |
1717 ms |
44632 KB |
Output is correct |
7 |
Correct |
1793 ms |
32868 KB |
Output is correct |
8 |
Correct |
3757 ms |
52104 KB |
Output is correct |
9 |
Correct |
3558 ms |
52104 KB |
Output is correct |
10 |
Correct |
3284 ms |
52268 KB |
Output is correct |
11 |
Correct |
2041 ms |
52044 KB |
Output is correct |
12 |
Correct |
1319 ms |
52092 KB |
Output is correct |
13 |
Correct |
1252 ms |
52384 KB |
Output is correct |
14 |
Correct |
1020 ms |
52372 KB |
Output is correct |
15 |
Correct |
1048 ms |
52468 KB |
Output is correct |
16 |
Correct |
1388 ms |
51944 KB |
Output is correct |
17 |
Correct |
1365 ms |
51928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
860 KB |
Output is correct |
6 |
Correct |
13 ms |
1116 KB |
Output is correct |
7 |
Correct |
10 ms |
1004 KB |
Output is correct |
8 |
Correct |
14 ms |
1116 KB |
Output is correct |
9 |
Correct |
9 ms |
1116 KB |
Output is correct |
10 |
Correct |
9 ms |
1112 KB |
Output is correct |
11 |
Correct |
14 ms |
1212 KB |
Output is correct |
12 |
Correct |
14 ms |
1116 KB |
Output is correct |
13 |
Correct |
13 ms |
1368 KB |
Output is correct |
14 |
Correct |
10 ms |
1116 KB |
Output is correct |
15 |
Correct |
10 ms |
1116 KB |
Output is correct |
16 |
Correct |
16 ms |
1112 KB |
Output is correct |
17 |
Correct |
10 ms |
1116 KB |
Output is correct |
18 |
Correct |
10 ms |
1112 KB |
Output is correct |
19 |
Correct |
8 ms |
1116 KB |
Output is correct |
20 |
Correct |
8 ms |
1116 KB |
Output is correct |
21 |
Correct |
6 ms |
1208 KB |
Output is correct |
22 |
Correct |
7 ms |
1112 KB |
Output is correct |
23 |
Correct |
8 ms |
1116 KB |
Output is correct |
24 |
Correct |
8 ms |
1220 KB |
Output is correct |
25 |
Correct |
7 ms |
1116 KB |
Output is correct |
26 |
Correct |
6 ms |
1116 KB |
Output is correct |
27 |
Correct |
6 ms |
1212 KB |
Output is correct |
28 |
Correct |
11 ms |
1112 KB |
Output is correct |
29 |
Correct |
6 ms |
1112 KB |
Output is correct |
30 |
Correct |
8 ms |
1116 KB |
Output is correct |
31 |
Correct |
8 ms |
1116 KB |
Output is correct |
32 |
Correct |
9 ms |
1112 KB |
Output is correct |
33 |
Correct |
10 ms |
1368 KB |
Output is correct |
34 |
Correct |
8 ms |
1368 KB |
Output is correct |
35 |
Correct |
13 ms |
1400 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
14 ms |
1116 KB |
Output is correct |
38 |
Correct |
9 ms |
1116 KB |
Output is correct |
39 |
Correct |
9 ms |
1116 KB |
Output is correct |
40 |
Correct |
2009 ms |
39044 KB |
Output is correct |
41 |
Correct |
1984 ms |
32536 KB |
Output is correct |
42 |
Correct |
2082 ms |
35464 KB |
Output is correct |
43 |
Correct |
1841 ms |
35320 KB |
Output is correct |
44 |
Correct |
1863 ms |
34096 KB |
Output is correct |
45 |
Correct |
3088 ms |
42628 KB |
Output is correct |
46 |
Correct |
2904 ms |
42632 KB |
Output is correct |
47 |
Correct |
2829 ms |
42624 KB |
Output is correct |
48 |
Correct |
2731 ms |
42784 KB |
Output is correct |
49 |
Correct |
3180 ms |
42632 KB |
Output is correct |
50 |
Correct |
4035 ms |
51816 KB |
Output is correct |
51 |
Correct |
4153 ms |
52116 KB |
Output is correct |
52 |
Correct |
3926 ms |
51428 KB |
Output is correct |
53 |
Correct |
3757 ms |
42868 KB |
Output is correct |
54 |
Correct |
4492 ms |
43012 KB |
Output is correct |
55 |
Correct |
3605 ms |
48628 KB |
Output is correct |
56 |
Correct |
2244 ms |
46836 KB |
Output is correct |
57 |
Correct |
2279 ms |
47000 KB |
Output is correct |
58 |
Correct |
2598 ms |
47052 KB |
Output is correct |
59 |
Correct |
2428 ms |
47064 KB |
Output is correct |
60 |
Correct |
2153 ms |
48512 KB |
Output is correct |
61 |
Correct |
2634 ms |
49024 KB |
Output is correct |
62 |
Correct |
2201 ms |
48280 KB |
Output is correct |
63 |
Correct |
1070 ms |
47848 KB |
Output is correct |
64 |
Correct |
1412 ms |
47576 KB |
Output is correct |
65 |
Correct |
1684 ms |
47804 KB |
Output is correct |
66 |
Correct |
1600 ms |
47852 KB |
Output is correct |
67 |
Correct |
1 ms |
344 KB |
Output is correct |
68 |
Correct |
10 ms |
1372 KB |
Output is correct |
69 |
Correct |
13 ms |
1372 KB |
Output is correct |
70 |
Correct |
13 ms |
1368 KB |
Output is correct |
71 |
Correct |
2260 ms |
46300 KB |
Output is correct |
72 |
Correct |
1717 ms |
44632 KB |
Output is correct |
73 |
Correct |
1793 ms |
32868 KB |
Output is correct |
74 |
Correct |
3757 ms |
52104 KB |
Output is correct |
75 |
Correct |
3558 ms |
52104 KB |
Output is correct |
76 |
Correct |
3284 ms |
52268 KB |
Output is correct |
77 |
Correct |
2041 ms |
52044 KB |
Output is correct |
78 |
Correct |
1319 ms |
52092 KB |
Output is correct |
79 |
Correct |
1252 ms |
52384 KB |
Output is correct |
80 |
Correct |
1020 ms |
52372 KB |
Output is correct |
81 |
Correct |
1048 ms |
52468 KB |
Output is correct |
82 |
Correct |
1388 ms |
51944 KB |
Output is correct |
83 |
Correct |
1365 ms |
51928 KB |
Output is correct |
84 |
Correct |
1512 ms |
38720 KB |
Output is correct |
85 |
Correct |
1255 ms |
30148 KB |
Output is correct |
86 |
Correct |
1043 ms |
28608 KB |
Output is correct |
87 |
Correct |
2521 ms |
48088 KB |
Output is correct |
88 |
Correct |
2520 ms |
47704 KB |
Output is correct |
89 |
Correct |
2460 ms |
47552 KB |
Output is correct |
90 |
Correct |
2530 ms |
47692 KB |
Output is correct |
91 |
Correct |
2565 ms |
47752 KB |
Output is correct |
92 |
Correct |
3444 ms |
54528 KB |
Output is correct |
93 |
Correct |
3412 ms |
56652 KB |
Output is correct |
94 |
Correct |
3308 ms |
48544 KB |
Output is correct |
95 |
Correct |
3328 ms |
48484 KB |
Output is correct |
96 |
Correct |
3273 ms |
48516 KB |
Output is correct |
97 |
Correct |
3424 ms |
48468 KB |
Output is correct |
98 |
Correct |
2152 ms |
47200 KB |
Output is correct |
99 |
Correct |
2147 ms |
47012 KB |
Output is correct |
100 |
Correct |
2184 ms |
47428 KB |
Output is correct |
101 |
Correct |
2225 ms |
47076 KB |
Output is correct |
102 |
Correct |
1596 ms |
48300 KB |
Output is correct |
103 |
Correct |
1766 ms |
48868 KB |
Output is correct |
104 |
Correct |
1745 ms |
48464 KB |
Output is correct |
105 |
Correct |
947 ms |
48276 KB |
Output is correct |
106 |
Correct |
943 ms |
47912 KB |
Output is correct |
107 |
Correct |
1245 ms |
47544 KB |
Output is correct |
108 |
Correct |
1156 ms |
47548 KB |
Output is correct |