#include<bits/stdc++.h>
using namespace std;
#define range(v) begin(v), end(v)
#define compact(v) v.erase(unique(range(v)), end(v))
template<class T> bool minimize(T& a, T b){
if(a > b) return a = b, true;
return false;
}
template<class T> bool maximize(T& a, T b){
if(a < b) return a = b, true;
return false;
}
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int N = 1e5 + 5;
int n, m, q, timerDFS, lift[20][N], h[N], s[N], t[N], uNodes[N], vNodes[N], w[N], x[N], tin[N], tout[N], L[N], R[N], res[N], lca[N];
ll y[N];
pair<int, int> a[N];
vector<int> g[N];
void dfs(int u, int e){
tin[u] = ++timerDFS;
//cout << u << " : " << tin[u] << '\n';
for(int id : g[u]){
int v = uNodes[id] ^ vNodes[id] ^ u;
if(v != e){
h[v] = h[u] + 1;
lift[0][v] = u;
for(int i = 1; i <= 17; ++i){
lift[i][v] = lift[i - 1][lift[i - 1][v]];
}
dfs(v, u);
}
}
tout[u] = ++timerDFS;
}
int getLCA(int u, int v){
if(h[u] != h[v]){
if(h[u] < h[v]) swap(u, v);
for(int i = 17; i >= 0; --i){
if(h[u] - (1 << i) >= h[v]){
u = lift[i][u];
}
}
}
if(u == v) return u;
for(int i = 17; i >= 0; --i){
if(lift[i][u] != lift[i][v]){
u = lift[i][u];
v = lift[i][v];
}
}
return lift[0][u];
}
struct fenwickPath{
vector<ll> bit;
fenwickPath(int n) : bit(2 * n + 1, 0) {}
void update(int i, ll v){
for(; i < (int)bit.size(); i += i & (-i)){
bit[i] += v;
}
}
ll query(int i){
ll s = 0;
for(; i > 0; i -= i & (-i)){
s += bit[i];
}
return s;
}
void resetData(){
fill(range(bit), 0LL);
}
} goldPath(0), silverPath(0);
void add(bool type, int id, int sign){
int u = a[id].second;
// cout << type << ' ' << id << ' ' << sign << '\n';
if(!type){ //silver
silverPath.update(tin[u], a[id].first * sign);
silverPath.update(tout[u], -a[id].first * sign);
}
else{ //gold
goldPath.update(tin[u], 1 * sign);
goldPath.update(tout[u], -1 * sign);
}
}
ll queryPath(bool type, int i){
if(!type) {
return silverPath.query(tin[s[i]]) + silverPath.query(tin[t[i]]) - 2LL * silverPath.query(tin[lca[i]]);
}
else{
return goldPath.query(tin[s[i]]) + goldPath.query(tin[t[i]]) - 2LL * goldPath.query(tin[lca[i]]);
}
}
void Zero_OP(){
cin >> n >> m >> q;
for(int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
g[u].push_back(i);
g[v].push_back(i);
uNodes[i] = u, vNodes[i] = v;
}
dfs(1, 1);
for(int i = 1; i <= m; ++i){
int p, c;
cin >> p >> c;
if(tin[uNodes[p]] < tin[vNodes[p]]) p = vNodes[p];
else p = uNodes[p];
a[i] = {c, p};
}
sort(a + 1, a + 1 + m);
for(int i = 1; i <= q; ++i){
cin >> s[i] >> t[i] >> x[i] >> y[i];
lca[i] = getLCA(s[i], t[i]), L[i] = 0, R[i] = m, res[i] = -1;
}
silverPath = fenwickPath(n), goldPath = fenwickPath(n);
int timesTotsBS = 20; //maybe should be 17 18
while(timesTotsBS--){
vector<pair<int, int>> events;
for(int i = 1; i <= q; ++i){
if(L[i] <= R[i]){
// cout << i << " : " << (L[i] + R[i] >> 1) << '\n';
events.push_back({L[i] + R[i] >> 1, i});
}
}
if(events.empty()) break;
sort(range(events));
int ptr = 1;
for(int i = 1; i <= m; ++i){
add(1, i, 1);
}
for(int i = 0; i < events.size(); ++i){
while(ptr <= m and ptr <= events[i].first){
add(1, ptr, -1);
add(0, ptr, 1);
++ptr;
}
int mid = events[i].first, id = events[i].second;
ll curSilver = queryPath(0, id), curGold = queryPath(1, id);
if(curSilver <= y[id] and curGold <= x[id]){
res[id] = x[id] - curGold;
L[id] = mid + 1;
}
else if(curGold > x[id]){
L[id] = mid + 1;
}
else R[id] = mid - 1;
}
silverPath.resetData();
goldPath.resetData();
}
for(int i = 1; i <= q; ++i){
cout << res[i] << '\n';
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen("antuvu.inp", "r")){
freopen("antuvu.inp", "r", stdin);
freopen("antuvu.out", "w", stdout);
}
Zero_OP();
return 0;
}
Compilation message
currencies.cpp: In function 'void Zero_OP()':
currencies.cpp:151:40: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
151 | events.push_back({L[i] + R[i] >> 1, i});
| ~~~~~^~~~~~
currencies.cpp:165:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
165 | for(int i = 0; i < events.size(); ++i){
| ~~^~~~~~~~~~~~~~~
currencies.cpp: In function 'int main()':
currencies.cpp:200:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
200 | freopen("antuvu.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
currencies.cpp:201:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
201 | freopen("antuvu.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
15092 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
7 ms |
15216 KB |
Output is correct |
6 |
Correct |
9 ms |
15196 KB |
Output is correct |
7 |
Correct |
7 ms |
14940 KB |
Output is correct |
8 |
Correct |
10 ms |
15228 KB |
Output is correct |
9 |
Correct |
9 ms |
15196 KB |
Output is correct |
10 |
Correct |
9 ms |
15196 KB |
Output is correct |
11 |
Correct |
9 ms |
15236 KB |
Output is correct |
12 |
Correct |
9 ms |
15196 KB |
Output is correct |
13 |
Correct |
9 ms |
15196 KB |
Output is correct |
14 |
Correct |
9 ms |
15196 KB |
Output is correct |
15 |
Correct |
9 ms |
15196 KB |
Output is correct |
16 |
Correct |
9 ms |
15448 KB |
Output is correct |
17 |
Correct |
11 ms |
15196 KB |
Output is correct |
18 |
Correct |
9 ms |
15352 KB |
Output is correct |
19 |
Correct |
7 ms |
15196 KB |
Output is correct |
20 |
Correct |
8 ms |
15196 KB |
Output is correct |
21 |
Correct |
8 ms |
15196 KB |
Output is correct |
22 |
Correct |
8 ms |
15192 KB |
Output is correct |
23 |
Correct |
8 ms |
15192 KB |
Output is correct |
24 |
Correct |
8 ms |
15448 KB |
Output is correct |
25 |
Correct |
8 ms |
15192 KB |
Output is correct |
26 |
Correct |
7 ms |
15192 KB |
Output is correct |
27 |
Correct |
7 ms |
15080 KB |
Output is correct |
28 |
Correct |
9 ms |
15196 KB |
Output is correct |
29 |
Correct |
7 ms |
15196 KB |
Output is correct |
30 |
Correct |
9 ms |
15196 KB |
Output is correct |
31 |
Correct |
9 ms |
15196 KB |
Output is correct |
32 |
Correct |
10 ms |
15196 KB |
Output is correct |
33 |
Correct |
9 ms |
15196 KB |
Output is correct |
34 |
Correct |
8 ms |
15344 KB |
Output is correct |
35 |
Correct |
8 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
8 ms |
15232 KB |
Output is correct |
3 |
Correct |
9 ms |
15192 KB |
Output is correct |
4 |
Correct |
9 ms |
15196 KB |
Output is correct |
5 |
Correct |
440 ms |
24248 KB |
Output is correct |
6 |
Correct |
525 ms |
22472 KB |
Output is correct |
7 |
Correct |
525 ms |
23700 KB |
Output is correct |
8 |
Correct |
381 ms |
23904 KB |
Output is correct |
9 |
Correct |
390 ms |
23148 KB |
Output is correct |
10 |
Correct |
613 ms |
25416 KB |
Output is correct |
11 |
Correct |
613 ms |
25364 KB |
Output is correct |
12 |
Correct |
600 ms |
25220 KB |
Output is correct |
13 |
Correct |
631 ms |
25232 KB |
Output is correct |
14 |
Correct |
606 ms |
25500 KB |
Output is correct |
15 |
Correct |
686 ms |
31028 KB |
Output is correct |
16 |
Correct |
677 ms |
31316 KB |
Output is correct |
17 |
Correct |
743 ms |
30676 KB |
Output is correct |
18 |
Correct |
641 ms |
25260 KB |
Output is correct |
19 |
Correct |
641 ms |
25220 KB |
Output is correct |
20 |
Correct |
675 ms |
25468 KB |
Output is correct |
21 |
Correct |
518 ms |
25648 KB |
Output is correct |
22 |
Correct |
513 ms |
25516 KB |
Output is correct |
23 |
Correct |
532 ms |
25564 KB |
Output is correct |
24 |
Correct |
520 ms |
25624 KB |
Output is correct |
25 |
Correct |
542 ms |
25232 KB |
Output is correct |
26 |
Correct |
519 ms |
25224 KB |
Output is correct |
27 |
Correct |
543 ms |
25460 KB |
Output is correct |
28 |
Correct |
492 ms |
25212 KB |
Output is correct |
29 |
Correct |
452 ms |
25224 KB |
Output is correct |
30 |
Correct |
513 ms |
25472 KB |
Output is correct |
31 |
Correct |
526 ms |
25240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15136 KB |
Output is correct |
2 |
Correct |
8 ms |
15348 KB |
Output is correct |
3 |
Correct |
9 ms |
15196 KB |
Output is correct |
4 |
Correct |
8 ms |
15196 KB |
Output is correct |
5 |
Correct |
409 ms |
29844 KB |
Output is correct |
6 |
Correct |
396 ms |
30056 KB |
Output is correct |
7 |
Correct |
558 ms |
24684 KB |
Output is correct |
8 |
Correct |
699 ms |
31384 KB |
Output is correct |
9 |
Correct |
683 ms |
31588 KB |
Output is correct |
10 |
Correct |
716 ms |
31840 KB |
Output is correct |
11 |
Correct |
615 ms |
31616 KB |
Output is correct |
12 |
Correct |
585 ms |
31372 KB |
Output is correct |
13 |
Correct |
557 ms |
31376 KB |
Output is correct |
14 |
Correct |
547 ms |
31636 KB |
Output is correct |
15 |
Correct |
552 ms |
31536 KB |
Output is correct |
16 |
Correct |
592 ms |
31376 KB |
Output is correct |
17 |
Correct |
577 ms |
31392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
15092 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
7 ms |
15216 KB |
Output is correct |
6 |
Correct |
9 ms |
15196 KB |
Output is correct |
7 |
Correct |
7 ms |
14940 KB |
Output is correct |
8 |
Correct |
10 ms |
15228 KB |
Output is correct |
9 |
Correct |
9 ms |
15196 KB |
Output is correct |
10 |
Correct |
9 ms |
15196 KB |
Output is correct |
11 |
Correct |
9 ms |
15236 KB |
Output is correct |
12 |
Correct |
9 ms |
15196 KB |
Output is correct |
13 |
Correct |
9 ms |
15196 KB |
Output is correct |
14 |
Correct |
9 ms |
15196 KB |
Output is correct |
15 |
Correct |
9 ms |
15196 KB |
Output is correct |
16 |
Correct |
9 ms |
15448 KB |
Output is correct |
17 |
Correct |
11 ms |
15196 KB |
Output is correct |
18 |
Correct |
9 ms |
15352 KB |
Output is correct |
19 |
Correct |
7 ms |
15196 KB |
Output is correct |
20 |
Correct |
8 ms |
15196 KB |
Output is correct |
21 |
Correct |
8 ms |
15196 KB |
Output is correct |
22 |
Correct |
8 ms |
15192 KB |
Output is correct |
23 |
Correct |
8 ms |
15192 KB |
Output is correct |
24 |
Correct |
8 ms |
15448 KB |
Output is correct |
25 |
Correct |
8 ms |
15192 KB |
Output is correct |
26 |
Correct |
7 ms |
15192 KB |
Output is correct |
27 |
Correct |
7 ms |
15080 KB |
Output is correct |
28 |
Correct |
9 ms |
15196 KB |
Output is correct |
29 |
Correct |
7 ms |
15196 KB |
Output is correct |
30 |
Correct |
9 ms |
15196 KB |
Output is correct |
31 |
Correct |
9 ms |
15196 KB |
Output is correct |
32 |
Correct |
10 ms |
15196 KB |
Output is correct |
33 |
Correct |
9 ms |
15196 KB |
Output is correct |
34 |
Correct |
8 ms |
15344 KB |
Output is correct |
35 |
Correct |
8 ms |
15196 KB |
Output is correct |
36 |
Correct |
2 ms |
14940 KB |
Output is correct |
37 |
Correct |
8 ms |
15232 KB |
Output is correct |
38 |
Correct |
9 ms |
15192 KB |
Output is correct |
39 |
Correct |
9 ms |
15196 KB |
Output is correct |
40 |
Correct |
440 ms |
24248 KB |
Output is correct |
41 |
Correct |
525 ms |
22472 KB |
Output is correct |
42 |
Correct |
525 ms |
23700 KB |
Output is correct |
43 |
Correct |
381 ms |
23904 KB |
Output is correct |
44 |
Correct |
390 ms |
23148 KB |
Output is correct |
45 |
Correct |
613 ms |
25416 KB |
Output is correct |
46 |
Correct |
613 ms |
25364 KB |
Output is correct |
47 |
Correct |
600 ms |
25220 KB |
Output is correct |
48 |
Correct |
631 ms |
25232 KB |
Output is correct |
49 |
Correct |
606 ms |
25500 KB |
Output is correct |
50 |
Correct |
686 ms |
31028 KB |
Output is correct |
51 |
Correct |
677 ms |
31316 KB |
Output is correct |
52 |
Correct |
743 ms |
30676 KB |
Output is correct |
53 |
Correct |
641 ms |
25260 KB |
Output is correct |
54 |
Correct |
641 ms |
25220 KB |
Output is correct |
55 |
Correct |
675 ms |
25468 KB |
Output is correct |
56 |
Correct |
518 ms |
25648 KB |
Output is correct |
57 |
Correct |
513 ms |
25516 KB |
Output is correct |
58 |
Correct |
532 ms |
25564 KB |
Output is correct |
59 |
Correct |
520 ms |
25624 KB |
Output is correct |
60 |
Correct |
542 ms |
25232 KB |
Output is correct |
61 |
Correct |
519 ms |
25224 KB |
Output is correct |
62 |
Correct |
543 ms |
25460 KB |
Output is correct |
63 |
Correct |
492 ms |
25212 KB |
Output is correct |
64 |
Correct |
452 ms |
25224 KB |
Output is correct |
65 |
Correct |
513 ms |
25472 KB |
Output is correct |
66 |
Correct |
526 ms |
25240 KB |
Output is correct |
67 |
Correct |
3 ms |
15136 KB |
Output is correct |
68 |
Correct |
8 ms |
15348 KB |
Output is correct |
69 |
Correct |
9 ms |
15196 KB |
Output is correct |
70 |
Correct |
8 ms |
15196 KB |
Output is correct |
71 |
Correct |
409 ms |
29844 KB |
Output is correct |
72 |
Correct |
396 ms |
30056 KB |
Output is correct |
73 |
Correct |
558 ms |
24684 KB |
Output is correct |
74 |
Correct |
699 ms |
31384 KB |
Output is correct |
75 |
Correct |
683 ms |
31588 KB |
Output is correct |
76 |
Correct |
716 ms |
31840 KB |
Output is correct |
77 |
Correct |
615 ms |
31616 KB |
Output is correct |
78 |
Correct |
585 ms |
31372 KB |
Output is correct |
79 |
Correct |
557 ms |
31376 KB |
Output is correct |
80 |
Correct |
547 ms |
31636 KB |
Output is correct |
81 |
Correct |
552 ms |
31536 KB |
Output is correct |
82 |
Correct |
592 ms |
31376 KB |
Output is correct |
83 |
Correct |
577 ms |
31392 KB |
Output is correct |
84 |
Correct |
489 ms |
23196 KB |
Output is correct |
85 |
Correct |
479 ms |
21232 KB |
Output is correct |
86 |
Correct |
408 ms |
21188 KB |
Output is correct |
87 |
Correct |
690 ms |
25236 KB |
Output is correct |
88 |
Correct |
648 ms |
25328 KB |
Output is correct |
89 |
Correct |
630 ms |
25232 KB |
Output is correct |
90 |
Correct |
653 ms |
25236 KB |
Output is correct |
91 |
Correct |
663 ms |
25380 KB |
Output is correct |
92 |
Correct |
712 ms |
29112 KB |
Output is correct |
93 |
Correct |
715 ms |
30440 KB |
Output is correct |
94 |
Correct |
702 ms |
25648 KB |
Output is correct |
95 |
Correct |
702 ms |
25260 KB |
Output is correct |
96 |
Correct |
675 ms |
25248 KB |
Output is correct |
97 |
Correct |
694 ms |
25244 KB |
Output is correct |
98 |
Correct |
580 ms |
25728 KB |
Output is correct |
99 |
Correct |
568 ms |
25868 KB |
Output is correct |
100 |
Correct |
549 ms |
25512 KB |
Output is correct |
101 |
Correct |
575 ms |
25540 KB |
Output is correct |
102 |
Correct |
584 ms |
25452 KB |
Output is correct |
103 |
Correct |
582 ms |
25232 KB |
Output is correct |
104 |
Correct |
572 ms |
25232 KB |
Output is correct |
105 |
Correct |
587 ms |
25396 KB |
Output is correct |
106 |
Correct |
521 ms |
25284 KB |
Output is correct |
107 |
Correct |
570 ms |
25228 KB |
Output is correct |
108 |
Correct |
533 ms |
25480 KB |
Output is correct |