#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;
}
}
}
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 |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
8 ms |
1104 KB |
Output is correct |
6 |
Correct |
11 ms |
1288 KB |
Output is correct |
7 |
Correct |
9 ms |
860 KB |
Output is correct |
8 |
Correct |
12 ms |
1300 KB |
Output is correct |
9 |
Correct |
12 ms |
1116 KB |
Output is correct |
10 |
Correct |
11 ms |
1116 KB |
Output is correct |
11 |
Correct |
11 ms |
1116 KB |
Output is correct |
12 |
Correct |
12 ms |
1116 KB |
Output is correct |
13 |
Correct |
12 ms |
1372 KB |
Output is correct |
14 |
Correct |
16 ms |
1496 KB |
Output is correct |
15 |
Correct |
13 ms |
1368 KB |
Output is correct |
16 |
Correct |
17 ms |
1368 KB |
Output is correct |
17 |
Correct |
14 ms |
1116 KB |
Output is correct |
18 |
Correct |
14 ms |
1372 KB |
Output is correct |
19 |
Correct |
8 ms |
1116 KB |
Output is correct |
20 |
Correct |
11 ms |
1292 KB |
Output is correct |
21 |
Correct |
8 ms |
1116 KB |
Output is correct |
22 |
Correct |
8 ms |
1204 KB |
Output is correct |
23 |
Correct |
9 ms |
1116 KB |
Output is correct |
24 |
Correct |
9 ms |
1240 KB |
Output is correct |
25 |
Correct |
9 ms |
1116 KB |
Output is correct |
26 |
Correct |
8 ms |
1116 KB |
Output is correct |
27 |
Correct |
7 ms |
1112 KB |
Output is correct |
28 |
Correct |
8 ms |
1312 KB |
Output is correct |
29 |
Correct |
7 ms |
1116 KB |
Output is correct |
30 |
Correct |
11 ms |
1116 KB |
Output is correct |
31 |
Correct |
11 ms |
1304 KB |
Output is correct |
32 |
Correct |
11 ms |
1116 KB |
Output is correct |
33 |
Correct |
11 ms |
1488 KB |
Output is correct |
34 |
Correct |
13 ms |
1372 KB |
Output is correct |
35 |
Correct |
11 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
11 ms |
1112 KB |
Output is correct |
3 |
Correct |
11 ms |
1116 KB |
Output is correct |
4 |
Correct |
11 ms |
1116 KB |
Output is correct |
5 |
Correct |
2330 ms |
43396 KB |
Output is correct |
6 |
Correct |
2436 ms |
37728 KB |
Output is correct |
7 |
Correct |
2655 ms |
41084 KB |
Output is correct |
8 |
Correct |
2027 ms |
38896 KB |
Output is correct |
9 |
Correct |
2082 ms |
38172 KB |
Output is correct |
10 |
Correct |
3633 ms |
48424 KB |
Output is correct |
11 |
Correct |
3693 ms |
48296 KB |
Output is correct |
12 |
Correct |
3644 ms |
47804 KB |
Output is correct |
13 |
Correct |
3674 ms |
47848 KB |
Output is correct |
14 |
Correct |
3625 ms |
47804 KB |
Output is correct |
15 |
Correct |
4573 ms |
57668 KB |
Output is correct |
16 |
Correct |
4122 ms |
58596 KB |
Output is correct |
17 |
Correct |
4310 ms |
57656 KB |
Output is correct |
18 |
Correct |
4742 ms |
48436 KB |
Output is correct |
19 |
Execution timed out |
5092 ms |
48444 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
12 ms |
1400 KB |
Output is correct |
3 |
Correct |
11 ms |
1368 KB |
Output is correct |
4 |
Correct |
11 ms |
1372 KB |
Output is correct |
5 |
Correct |
2040 ms |
50076 KB |
Output is correct |
6 |
Correct |
1953 ms |
48784 KB |
Output is correct |
7 |
Correct |
1785 ms |
37796 KB |
Output is correct |
8 |
Correct |
3698 ms |
58332 KB |
Output is correct |
9 |
Correct |
4480 ms |
57848 KB |
Output is correct |
10 |
Correct |
4806 ms |
58084 KB |
Output is correct |
11 |
Correct |
2412 ms |
57920 KB |
Output is correct |
12 |
Correct |
1758 ms |
57952 KB |
Output is correct |
13 |
Correct |
1996 ms |
57896 KB |
Output is correct |
14 |
Correct |
1313 ms |
57564 KB |
Output is correct |
15 |
Correct |
1185 ms |
57208 KB |
Output is correct |
16 |
Correct |
2193 ms |
57728 KB |
Output is correct |
17 |
Correct |
1766 ms |
58264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
8 ms |
1104 KB |
Output is correct |
6 |
Correct |
11 ms |
1288 KB |
Output is correct |
7 |
Correct |
9 ms |
860 KB |
Output is correct |
8 |
Correct |
12 ms |
1300 KB |
Output is correct |
9 |
Correct |
12 ms |
1116 KB |
Output is correct |
10 |
Correct |
11 ms |
1116 KB |
Output is correct |
11 |
Correct |
11 ms |
1116 KB |
Output is correct |
12 |
Correct |
12 ms |
1116 KB |
Output is correct |
13 |
Correct |
12 ms |
1372 KB |
Output is correct |
14 |
Correct |
16 ms |
1496 KB |
Output is correct |
15 |
Correct |
13 ms |
1368 KB |
Output is correct |
16 |
Correct |
17 ms |
1368 KB |
Output is correct |
17 |
Correct |
14 ms |
1116 KB |
Output is correct |
18 |
Correct |
14 ms |
1372 KB |
Output is correct |
19 |
Correct |
8 ms |
1116 KB |
Output is correct |
20 |
Correct |
11 ms |
1292 KB |
Output is correct |
21 |
Correct |
8 ms |
1116 KB |
Output is correct |
22 |
Correct |
8 ms |
1204 KB |
Output is correct |
23 |
Correct |
9 ms |
1116 KB |
Output is correct |
24 |
Correct |
9 ms |
1240 KB |
Output is correct |
25 |
Correct |
9 ms |
1116 KB |
Output is correct |
26 |
Correct |
8 ms |
1116 KB |
Output is correct |
27 |
Correct |
7 ms |
1112 KB |
Output is correct |
28 |
Correct |
8 ms |
1312 KB |
Output is correct |
29 |
Correct |
7 ms |
1116 KB |
Output is correct |
30 |
Correct |
11 ms |
1116 KB |
Output is correct |
31 |
Correct |
11 ms |
1304 KB |
Output is correct |
32 |
Correct |
11 ms |
1116 KB |
Output is correct |
33 |
Correct |
11 ms |
1488 KB |
Output is correct |
34 |
Correct |
13 ms |
1372 KB |
Output is correct |
35 |
Correct |
11 ms |
1368 KB |
Output is correct |
36 |
Correct |
0 ms |
600 KB |
Output is correct |
37 |
Correct |
11 ms |
1112 KB |
Output is correct |
38 |
Correct |
11 ms |
1116 KB |
Output is correct |
39 |
Correct |
11 ms |
1116 KB |
Output is correct |
40 |
Correct |
2330 ms |
43396 KB |
Output is correct |
41 |
Correct |
2436 ms |
37728 KB |
Output is correct |
42 |
Correct |
2655 ms |
41084 KB |
Output is correct |
43 |
Correct |
2027 ms |
38896 KB |
Output is correct |
44 |
Correct |
2082 ms |
38172 KB |
Output is correct |
45 |
Correct |
3633 ms |
48424 KB |
Output is correct |
46 |
Correct |
3693 ms |
48296 KB |
Output is correct |
47 |
Correct |
3644 ms |
47804 KB |
Output is correct |
48 |
Correct |
3674 ms |
47848 KB |
Output is correct |
49 |
Correct |
3625 ms |
47804 KB |
Output is correct |
50 |
Correct |
4573 ms |
57668 KB |
Output is correct |
51 |
Correct |
4122 ms |
58596 KB |
Output is correct |
52 |
Correct |
4310 ms |
57656 KB |
Output is correct |
53 |
Correct |
4742 ms |
48436 KB |
Output is correct |
54 |
Execution timed out |
5092 ms |
48444 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |