#include <bits/stdc++.h>
using namespace std;/*
<<<<It's never too late for a new beginning in your life>>>>
Today is hard
tomorrow will worse
but the day after tomorrow will be the sunshine..
HARD WORK BEATS TALENT WHEN TALENT DOESN'T WORK HARD............
Never give up */
//The most CHALISHKANCHIK
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define int long long
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vii;
const int N = 100005;
vi g[N];
map<pii, vi> mp;
int t;
int pre[N];
int n, q, timer;
int tin[N], tout[N];
int up[20][N];
int num[N];
void dfs(int v, int pr){
if(pr!=-1)num[v] = num[pr]+mp[{v,pr}].size();
tin[v] = ++timer;
up[0][v] = pr;
for (int i = 1; i <= 17; i++) {
up[i][v] = up[i - 1][ up[i - 1][v] ];
}
for (int to : g[v]) {
if (to == pr) continue;
pre[to] = v;
dfs(to, v);
}
tout[v] = timer;
}
bool upper(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int lca(int a, int b) {
if (upper(a, b)) return a;
if (upper(b, a)) return b;
for (int i = 17; i >= 0; i--) {
if (!upper(up[i][a], b)) {
a = up[i][a];
}
}
return up[0][a];
}
void solve(){
int n, m, q;
cin >> n >> m >> q;
vii cui;
int cc = 0;
for(int i = 0; i < n-1; i++){
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
cui.pb({a, b});
}
for(int i = 0; i < m; i++){
int j, c;
cin >> j >> c;j--;
mp[cui[j]].pb(c);
mp[{cui[j].ss, cui[j].ff}].pb(c);
cc = c;
}
if(n > 2000){
dfs(n, -1);
while(q--){
int a, b, x, y;
cin >> a >> b >> x >> y;
int cnt = num[a] + num[b] - (2 * num[lca(a, b)]);
cnt = max(0ll, cnt - (y/cc));
x -= cnt;
cout << (x < 0 ? -1 : x) << '\n';
}
}else{
vi pas;
while(q--){
int s, x, y;
cin >> s >> t >> x >> y;
dfs(s, -1);
int r = t;
while(r!=s){
for(auto i:mp[{r, pre[r]}])pas.pb(i);
r = pre[r];
}
sort(all(pas));
for(auto i:pas){
if(i <= y)y -= i;
else x--;
}pas.clear();
cout << (x<0?-1:x) << '\n';
}
}
}
main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t = 1;
//~ cin >> t;
while(t--){
solve();
}
}
Compilation message
currencies.cpp:108:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
108 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19288 KB |
Output is correct |
2 |
Correct |
3 ms |
19292 KB |
Output is correct |
3 |
Correct |
3 ms |
19292 KB |
Output is correct |
4 |
Correct |
3 ms |
19292 KB |
Output is correct |
5 |
Correct |
313 ms |
20084 KB |
Output is correct |
6 |
Correct |
534 ms |
19804 KB |
Output is correct |
7 |
Correct |
301 ms |
19720 KB |
Output is correct |
8 |
Correct |
567 ms |
19876 KB |
Output is correct |
9 |
Correct |
585 ms |
19800 KB |
Output is correct |
10 |
Correct |
606 ms |
20144 KB |
Output is correct |
11 |
Correct |
618 ms |
20132 KB |
Output is correct |
12 |
Correct |
581 ms |
20048 KB |
Output is correct |
13 |
Correct |
845 ms |
20100 KB |
Output is correct |
14 |
Correct |
810 ms |
20112 KB |
Output is correct |
15 |
Correct |
716 ms |
20056 KB |
Output is correct |
16 |
Correct |
710 ms |
20048 KB |
Output is correct |
17 |
Correct |
665 ms |
19800 KB |
Output is correct |
18 |
Correct |
719 ms |
19988 KB |
Output is correct |
19 |
Correct |
456 ms |
19804 KB |
Output is correct |
20 |
Correct |
511 ms |
19800 KB |
Output is correct |
21 |
Correct |
475 ms |
19876 KB |
Output is correct |
22 |
Correct |
478 ms |
19900 KB |
Output is correct |
23 |
Correct |
597 ms |
20052 KB |
Output is correct |
24 |
Correct |
600 ms |
19804 KB |
Output is correct |
25 |
Correct |
616 ms |
19872 KB |
Output is correct |
26 |
Correct |
587 ms |
20052 KB |
Output is correct |
27 |
Correct |
588 ms |
19864 KB |
Output is correct |
28 |
Correct |
599 ms |
19876 KB |
Output is correct |
29 |
Correct |
579 ms |
20056 KB |
Output is correct |
30 |
Correct |
603 ms |
20052 KB |
Output is correct |
31 |
Correct |
606 ms |
19888 KB |
Output is correct |
32 |
Correct |
576 ms |
19804 KB |
Output is correct |
33 |
Correct |
433 ms |
20128 KB |
Output is correct |
34 |
Correct |
436 ms |
20112 KB |
Output is correct |
35 |
Correct |
439 ms |
20056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19292 KB |
Output is correct |
2 |
Correct |
590 ms |
19904 KB |
Output is correct |
3 |
Correct |
624 ms |
20048 KB |
Output is correct |
4 |
Correct |
592 ms |
20048 KB |
Output is correct |
5 |
Incorrect |
253 ms |
43624 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19292 KB |
Output is correct |
2 |
Correct |
439 ms |
20116 KB |
Output is correct |
3 |
Correct |
437 ms |
20312 KB |
Output is correct |
4 |
Correct |
443 ms |
20056 KB |
Output is correct |
5 |
Incorrect |
124 ms |
49596 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19288 KB |
Output is correct |
2 |
Correct |
3 ms |
19292 KB |
Output is correct |
3 |
Correct |
3 ms |
19292 KB |
Output is correct |
4 |
Correct |
3 ms |
19292 KB |
Output is correct |
5 |
Correct |
313 ms |
20084 KB |
Output is correct |
6 |
Correct |
534 ms |
19804 KB |
Output is correct |
7 |
Correct |
301 ms |
19720 KB |
Output is correct |
8 |
Correct |
567 ms |
19876 KB |
Output is correct |
9 |
Correct |
585 ms |
19800 KB |
Output is correct |
10 |
Correct |
606 ms |
20144 KB |
Output is correct |
11 |
Correct |
618 ms |
20132 KB |
Output is correct |
12 |
Correct |
581 ms |
20048 KB |
Output is correct |
13 |
Correct |
845 ms |
20100 KB |
Output is correct |
14 |
Correct |
810 ms |
20112 KB |
Output is correct |
15 |
Correct |
716 ms |
20056 KB |
Output is correct |
16 |
Correct |
710 ms |
20048 KB |
Output is correct |
17 |
Correct |
665 ms |
19800 KB |
Output is correct |
18 |
Correct |
719 ms |
19988 KB |
Output is correct |
19 |
Correct |
456 ms |
19804 KB |
Output is correct |
20 |
Correct |
511 ms |
19800 KB |
Output is correct |
21 |
Correct |
475 ms |
19876 KB |
Output is correct |
22 |
Correct |
478 ms |
19900 KB |
Output is correct |
23 |
Correct |
597 ms |
20052 KB |
Output is correct |
24 |
Correct |
600 ms |
19804 KB |
Output is correct |
25 |
Correct |
616 ms |
19872 KB |
Output is correct |
26 |
Correct |
587 ms |
20052 KB |
Output is correct |
27 |
Correct |
588 ms |
19864 KB |
Output is correct |
28 |
Correct |
599 ms |
19876 KB |
Output is correct |
29 |
Correct |
579 ms |
20056 KB |
Output is correct |
30 |
Correct |
603 ms |
20052 KB |
Output is correct |
31 |
Correct |
606 ms |
19888 KB |
Output is correct |
32 |
Correct |
576 ms |
19804 KB |
Output is correct |
33 |
Correct |
433 ms |
20128 KB |
Output is correct |
34 |
Correct |
436 ms |
20112 KB |
Output is correct |
35 |
Correct |
439 ms |
20056 KB |
Output is correct |
36 |
Correct |
3 ms |
19292 KB |
Output is correct |
37 |
Correct |
590 ms |
19904 KB |
Output is correct |
38 |
Correct |
624 ms |
20048 KB |
Output is correct |
39 |
Correct |
592 ms |
20048 KB |
Output is correct |
40 |
Incorrect |
253 ms |
43624 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |