#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
const int LOG = 18;
template <typename T>
struct binary_indexed_tree{
int N;
std::vector<T> BIT;
binary_indexed_tree(int N): N(N), BIT(N + 1, 0){
}
void add(int i, T x){
i++;
while (i <= N){
BIT[i] += x;
i += i & -i;
}
}
T sum(int i){
T ans = 0;
while (i > 0){
ans += BIT[i];
i -= i & -i;
}
return ans;
}
};
int main(){
int N, M, Q;
std::cin >> N >> M >> Q;
std::vector<int> A(N - 1), B(N - 1);
for (int i = 0; i < N - 1; i++){
std::cin >> A[i] >> B[i];
A[i]--;
B[i]--;
}
std::vector<int> P(M), C(M);
for (int i = 0; i < M; i++){
std::cin >> P[i] >> C[i];
P[i]--;
}
std::vector<int> S(Q), T(Q);
std::vector<int> X(Q);
std::vector<long long> Y(Q);
for (int i = 0; i < Q; i++){
std::cin >> S[i] >> T[i] >> X[i] >> Y[i];
S[i]--;
T[i]--;
}
std::vector<std::vector<int>> E(N);
for (int i = 0; i < N - 1; i++){
E[A[i]].push_back(B[i]);
E[B[i]].push_back(A[i]);
}
std::vector<int> p(N, -1);
std::vector<std::vector<int>> c(N);
std::vector<int> d(N, 0);
std::queue<int> q;
q.push(0);
while (!q.empty()){
int v = q.front();
q.pop();
for (int w : E[v]){
if (w != p[v]){
p[w] = v;
c[v].push_back(w);
d[w] = d[v] + 1;
q.push(w);
}
}
}
for (int i = 0; i < N - 1; i++){
if (B[i] == p[A[i]]){
std::swap(A[i], B[i]);
}
}
std::vector<std::vector<int>> pp(LOG, std::vector<int>(N, -1));
pp[0] = p;
for (int i = 0; i < LOG - 1; i++){
for (int j = 0; j < N; j++){
if (pp[i][j] != -1){
pp[i + 1][j] = pp[i][pp[i][j]];
}
}
}
std::vector<int> L(Q);
for (int i = 0; i < Q; i++){
int u = S[i], v = T[i];
if (d[u] > d[v]){
std::swap(u, v);
}
for (int j = 0; j < LOG; j++){
if (((d[v] - d[u]) >> j & 1) == 1){
v = pp[j][v];
}
}
if (u == v){
L[i] = u;
} else {
for (int j = LOG - 1; j >= 0; j--){
if (pp[j][u] != pp[j][v]){
u = pp[j][u];
v = pp[j][v];
}
}
L[i] = p[u];
}
}
std::vector<int> in(N), out(N);
int t = 0;
auto dfs = [&](auto dfs, int v = 0) -> void {
if (v != 0){
in[v] = t;
t++;
}
for (int w : c[v]){
dfs(dfs, w);
}
if (v != 0){
out[v] = t;
t++;
}
};
dfs(dfs);
in[0] = -1;
std::vector<std::pair<int, int>> D(M);
for (int i = 0; i < M; i++){
D[i] = std::make_pair(C[i], P[i]);
}
std::sort(D.begin(), D.end());
std::vector<int> tv(Q, -1), fv(Q, M + 1);
std::vector<int> cnt(Q);
while (true){
bool ok = true;
std::vector<std::vector<int>> id(M + 1);
for (int i = 0; i < Q; i++){
if (fv[i] - tv[i] > 1){
ok = false;
id[(tv[i] + fv[i]) / 2].push_back(i);
}
}
if (ok){
break;
}
binary_indexed_tree<int> BIT1(N * 2 - 2);
binary_indexed_tree<long long> BIT2(N * 2 - 2);
for (int i = 0; i < M; i++){
BIT1.add(in[B[P[i]]], 1);
BIT1.add(out[B[P[i]]], -1);
}
for (int i = 0; i <= M; i++){
for (int j : id[i]){
long long s = BIT2.sum(in[S[j]] + 1) + BIT2.sum(in[T[j]] + 1) - BIT2.sum(in[L[j]] + 1) * 2;
if (s <= Y[j]){
cnt[j] = BIT1.sum(in[S[j]] + 1) + BIT1.sum(in[T[j]] + 1) - BIT1.sum(in[L[j]] + 1) * 2;
tv[j] = i;
} else {
fv[j] = i;
}
}
if (i < M){
int x = D[i].first;
int e = D[i].second;
BIT1.add(in[B[e]], -1);
BIT1.add(out[B[e]], 1);
BIT2.add(in[B[e]], x);
BIT2.add(out[B[e]], -x);
}
}
}
for (int i = 0; i < Q; i++){
std::cout << std::max(X[i] - cnt[i], -1) << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
13 ms |
872 KB |
Output is correct |
6 |
Correct |
15 ms |
920 KB |
Output is correct |
7 |
Correct |
15 ms |
676 KB |
Output is correct |
8 |
Correct |
11 ms |
892 KB |
Output is correct |
9 |
Correct |
15 ms |
980 KB |
Output is correct |
10 |
Correct |
18 ms |
852 KB |
Output is correct |
11 |
Correct |
13 ms |
960 KB |
Output is correct |
12 |
Correct |
12 ms |
980 KB |
Output is correct |
13 |
Correct |
18 ms |
980 KB |
Output is correct |
14 |
Correct |
13 ms |
980 KB |
Output is correct |
15 |
Correct |
14 ms |
920 KB |
Output is correct |
16 |
Correct |
13 ms |
968 KB |
Output is correct |
17 |
Correct |
12 ms |
924 KB |
Output is correct |
18 |
Correct |
14 ms |
964 KB |
Output is correct |
19 |
Correct |
14 ms |
852 KB |
Output is correct |
20 |
Correct |
12 ms |
964 KB |
Output is correct |
21 |
Correct |
15 ms |
980 KB |
Output is correct |
22 |
Correct |
12 ms |
964 KB |
Output is correct |
23 |
Correct |
11 ms |
980 KB |
Output is correct |
24 |
Correct |
14 ms |
980 KB |
Output is correct |
25 |
Correct |
14 ms |
968 KB |
Output is correct |
26 |
Correct |
10 ms |
980 KB |
Output is correct |
27 |
Correct |
10 ms |
952 KB |
Output is correct |
28 |
Correct |
11 ms |
920 KB |
Output is correct |
29 |
Correct |
11 ms |
852 KB |
Output is correct |
30 |
Correct |
12 ms |
968 KB |
Output is correct |
31 |
Correct |
12 ms |
968 KB |
Output is correct |
32 |
Correct |
14 ms |
980 KB |
Output is correct |
33 |
Correct |
12 ms |
1108 KB |
Output is correct |
34 |
Correct |
12 ms |
1108 KB |
Output is correct |
35 |
Correct |
12 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
11 ms |
968 KB |
Output is correct |
3 |
Correct |
12 ms |
976 KB |
Output is correct |
4 |
Correct |
13 ms |
920 KB |
Output is correct |
5 |
Correct |
1060 ms |
32516 KB |
Output is correct |
6 |
Correct |
1122 ms |
27584 KB |
Output is correct |
7 |
Correct |
1042 ms |
29384 KB |
Output is correct |
8 |
Correct |
811 ms |
28444 KB |
Output is correct |
9 |
Correct |
736 ms |
28232 KB |
Output is correct |
10 |
Correct |
1300 ms |
35428 KB |
Output is correct |
11 |
Correct |
1296 ms |
35448 KB |
Output is correct |
12 |
Correct |
1302 ms |
35396 KB |
Output is correct |
13 |
Correct |
1414 ms |
35532 KB |
Output is correct |
14 |
Correct |
1237 ms |
35432 KB |
Output is correct |
15 |
Correct |
1432 ms |
41780 KB |
Output is correct |
16 |
Correct |
1218 ms |
42048 KB |
Output is correct |
17 |
Correct |
1307 ms |
41504 KB |
Output is correct |
18 |
Correct |
1181 ms |
37180 KB |
Output is correct |
19 |
Correct |
1311 ms |
37220 KB |
Output is correct |
20 |
Correct |
1735 ms |
37340 KB |
Output is correct |
21 |
Correct |
1050 ms |
33860 KB |
Output is correct |
22 |
Correct |
1077 ms |
34148 KB |
Output is correct |
23 |
Correct |
1133 ms |
34088 KB |
Output is correct |
24 |
Correct |
1159 ms |
34144 KB |
Output is correct |
25 |
Correct |
996 ms |
35764 KB |
Output is correct |
26 |
Correct |
989 ms |
35876 KB |
Output is correct |
27 |
Correct |
1046 ms |
35764 KB |
Output is correct |
28 |
Correct |
899 ms |
35232 KB |
Output is correct |
29 |
Correct |
923 ms |
35080 KB |
Output is correct |
30 |
Correct |
964 ms |
35580 KB |
Output is correct |
31 |
Correct |
891 ms |
35380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
16 ms |
1108 KB |
Output is correct |
3 |
Correct |
12 ms |
1052 KB |
Output is correct |
4 |
Correct |
12 ms |
1052 KB |
Output is correct |
5 |
Correct |
865 ms |
36432 KB |
Output is correct |
6 |
Correct |
712 ms |
35028 KB |
Output is correct |
7 |
Correct |
1014 ms |
28276 KB |
Output is correct |
8 |
Correct |
1283 ms |
42096 KB |
Output is correct |
9 |
Correct |
1323 ms |
42092 KB |
Output is correct |
10 |
Correct |
1454 ms |
42208 KB |
Output is correct |
11 |
Correct |
1456 ms |
42088 KB |
Output is correct |
12 |
Correct |
1282 ms |
42124 KB |
Output is correct |
13 |
Correct |
1293 ms |
42104 KB |
Output is correct |
14 |
Correct |
1303 ms |
41856 KB |
Output is correct |
15 |
Correct |
1198 ms |
41888 KB |
Output is correct |
16 |
Correct |
1286 ms |
42008 KB |
Output is correct |
17 |
Correct |
1216 ms |
41312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
13 ms |
872 KB |
Output is correct |
6 |
Correct |
15 ms |
920 KB |
Output is correct |
7 |
Correct |
15 ms |
676 KB |
Output is correct |
8 |
Correct |
11 ms |
892 KB |
Output is correct |
9 |
Correct |
15 ms |
980 KB |
Output is correct |
10 |
Correct |
18 ms |
852 KB |
Output is correct |
11 |
Correct |
13 ms |
960 KB |
Output is correct |
12 |
Correct |
12 ms |
980 KB |
Output is correct |
13 |
Correct |
18 ms |
980 KB |
Output is correct |
14 |
Correct |
13 ms |
980 KB |
Output is correct |
15 |
Correct |
14 ms |
920 KB |
Output is correct |
16 |
Correct |
13 ms |
968 KB |
Output is correct |
17 |
Correct |
12 ms |
924 KB |
Output is correct |
18 |
Correct |
14 ms |
964 KB |
Output is correct |
19 |
Correct |
14 ms |
852 KB |
Output is correct |
20 |
Correct |
12 ms |
964 KB |
Output is correct |
21 |
Correct |
15 ms |
980 KB |
Output is correct |
22 |
Correct |
12 ms |
964 KB |
Output is correct |
23 |
Correct |
11 ms |
980 KB |
Output is correct |
24 |
Correct |
14 ms |
980 KB |
Output is correct |
25 |
Correct |
14 ms |
968 KB |
Output is correct |
26 |
Correct |
10 ms |
980 KB |
Output is correct |
27 |
Correct |
10 ms |
952 KB |
Output is correct |
28 |
Correct |
11 ms |
920 KB |
Output is correct |
29 |
Correct |
11 ms |
852 KB |
Output is correct |
30 |
Correct |
12 ms |
968 KB |
Output is correct |
31 |
Correct |
12 ms |
968 KB |
Output is correct |
32 |
Correct |
14 ms |
980 KB |
Output is correct |
33 |
Correct |
12 ms |
1108 KB |
Output is correct |
34 |
Correct |
12 ms |
1108 KB |
Output is correct |
35 |
Correct |
12 ms |
1108 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
11 ms |
968 KB |
Output is correct |
38 |
Correct |
12 ms |
976 KB |
Output is correct |
39 |
Correct |
13 ms |
920 KB |
Output is correct |
40 |
Correct |
1060 ms |
32516 KB |
Output is correct |
41 |
Correct |
1122 ms |
27584 KB |
Output is correct |
42 |
Correct |
1042 ms |
29384 KB |
Output is correct |
43 |
Correct |
811 ms |
28444 KB |
Output is correct |
44 |
Correct |
736 ms |
28232 KB |
Output is correct |
45 |
Correct |
1300 ms |
35428 KB |
Output is correct |
46 |
Correct |
1296 ms |
35448 KB |
Output is correct |
47 |
Correct |
1302 ms |
35396 KB |
Output is correct |
48 |
Correct |
1414 ms |
35532 KB |
Output is correct |
49 |
Correct |
1237 ms |
35432 KB |
Output is correct |
50 |
Correct |
1432 ms |
41780 KB |
Output is correct |
51 |
Correct |
1218 ms |
42048 KB |
Output is correct |
52 |
Correct |
1307 ms |
41504 KB |
Output is correct |
53 |
Correct |
1181 ms |
37180 KB |
Output is correct |
54 |
Correct |
1311 ms |
37220 KB |
Output is correct |
55 |
Correct |
1735 ms |
37340 KB |
Output is correct |
56 |
Correct |
1050 ms |
33860 KB |
Output is correct |
57 |
Correct |
1077 ms |
34148 KB |
Output is correct |
58 |
Correct |
1133 ms |
34088 KB |
Output is correct |
59 |
Correct |
1159 ms |
34144 KB |
Output is correct |
60 |
Correct |
996 ms |
35764 KB |
Output is correct |
61 |
Correct |
989 ms |
35876 KB |
Output is correct |
62 |
Correct |
1046 ms |
35764 KB |
Output is correct |
63 |
Correct |
899 ms |
35232 KB |
Output is correct |
64 |
Correct |
923 ms |
35080 KB |
Output is correct |
65 |
Correct |
964 ms |
35580 KB |
Output is correct |
66 |
Correct |
891 ms |
35380 KB |
Output is correct |
67 |
Correct |
1 ms |
212 KB |
Output is correct |
68 |
Correct |
16 ms |
1108 KB |
Output is correct |
69 |
Correct |
12 ms |
1052 KB |
Output is correct |
70 |
Correct |
12 ms |
1052 KB |
Output is correct |
71 |
Correct |
865 ms |
36432 KB |
Output is correct |
72 |
Correct |
712 ms |
35028 KB |
Output is correct |
73 |
Correct |
1014 ms |
28276 KB |
Output is correct |
74 |
Correct |
1283 ms |
42096 KB |
Output is correct |
75 |
Correct |
1323 ms |
42092 KB |
Output is correct |
76 |
Correct |
1454 ms |
42208 KB |
Output is correct |
77 |
Correct |
1456 ms |
42088 KB |
Output is correct |
78 |
Correct |
1282 ms |
42124 KB |
Output is correct |
79 |
Correct |
1293 ms |
42104 KB |
Output is correct |
80 |
Correct |
1303 ms |
41856 KB |
Output is correct |
81 |
Correct |
1198 ms |
41888 KB |
Output is correct |
82 |
Correct |
1286 ms |
42008 KB |
Output is correct |
83 |
Correct |
1216 ms |
41312 KB |
Output is correct |
84 |
Correct |
1149 ms |
29188 KB |
Output is correct |
85 |
Correct |
895 ms |
21532 KB |
Output is correct |
86 |
Correct |
743 ms |
20424 KB |
Output is correct |
87 |
Correct |
1242 ms |
35396 KB |
Output is correct |
88 |
Correct |
1374 ms |
34760 KB |
Output is correct |
89 |
Correct |
1555 ms |
35472 KB |
Output is correct |
90 |
Correct |
1586 ms |
35484 KB |
Output is correct |
91 |
Correct |
1423 ms |
35416 KB |
Output is correct |
92 |
Correct |
1854 ms |
40448 KB |
Output is correct |
93 |
Correct |
1836 ms |
41388 KB |
Output is correct |
94 |
Correct |
1407 ms |
37428 KB |
Output is correct |
95 |
Correct |
1658 ms |
37384 KB |
Output is correct |
96 |
Correct |
1524 ms |
37264 KB |
Output is correct |
97 |
Correct |
1971 ms |
37376 KB |
Output is correct |
98 |
Correct |
1326 ms |
34056 KB |
Output is correct |
99 |
Correct |
1200 ms |
34052 KB |
Output is correct |
100 |
Correct |
1473 ms |
33744 KB |
Output is correct |
101 |
Correct |
1450 ms |
34076 KB |
Output is correct |
102 |
Correct |
1267 ms |
35832 KB |
Output is correct |
103 |
Correct |
1158 ms |
35884 KB |
Output is correct |
104 |
Correct |
1400 ms |
35872 KB |
Output is correct |
105 |
Correct |
1129 ms |
35540 KB |
Output is correct |
106 |
Correct |
1041 ms |
34960 KB |
Output is correct |
107 |
Correct |
1148 ms |
35260 KB |
Output is correct |
108 |
Correct |
1055 ms |
35440 KB |
Output is correct |