#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
/*
const int MAXN = 1e5+5;
int lf[MAXN],rg[MAXN],ndcnt = 0;
ll sum[MAXN];
int new_node(){
ndcnt++;
return ndcnt;
}
int new_node(int v){
ndcnt++;
sum[ndcnt] = sum[v];
lf[ndcnt] = lf[v];
rg[ndcnt] = rg[v];
return ndcnt;
}
int upd(int v,int pos,int l,int r,ll val){
if(l==r){
ndcnt++;
sum[ndcnt]+=val;
return ndcnt;
}
int tm = (l+r)/2;
int u = new_node(v);
if(pos<=tm){
if(!lf[v])lf[v] = new_node();
lf[u] = upd(lf[v],pos,l,tm,val);
}else{
if(!rg[v])rg[v] = new_node();
rg[u] = upd(rg[v],pos,tm+1,r,val);
}
sum[u] = 0;
if(lf[u])sum[u]+=sum[lf[u]];
if(rg[u])sum[u]+=sum[rg[u]];
return u;
}
int query(int a,int b,int l,int r,ll k){
if(l==r)return l;
int tm = (l+r)/2;
if(!ch[0][a])ch[0][a] = new_node();
if(!ch[0][b])ch[0][b] = new_node();
if(sum[ch[0][b]] - sum[ch[0][a]])
} */
const int MAXN = 2e5+5;
vector<int>adj[MAXN];
int tin[MAXN],tout[MAXN],dep[MAXN],par[19][MAXN];
int timer = 0;
void dfs(int v,int u){
tin[v] = ++timer;
for(int x:adj[v]){
if(x == u)continue;
par[0][x] = v;
dep[x] = dep[v]+1;
for(int i=1;i<=18;i++){
par[i][x] = par[i-1][par[i-1][x]];
}
dfs(x,v);
}
tout[v] = ++timer;
}
bool ancestor(int v,int u){return(tin[v] >= tin[u] && tout[u] >= tout[v]);}//is u an ancestor of v?
int lca(int v,int u){
if(dep[v] > dep[u])swap(u,v);
int dist = dep[u]-dep[v];
for(int i=0;dist;i++,dist>>=1){
if(dist&1)u = par[i][u];
}
if(u == v)return u;
for(int i=18;i>=0;i--){
if(par[i][v] != par[i][u]){
u = par[i][u];
v = par[i][v];
}
}
return par[0][v];
}
int cost[MAXN];
int main()
{
owo
int n,m,q;
cin>>n>>m>>q;
vector<pii>ed(n),C(m);
for(int i=1;i<n;i++)cin>>ed[i].fi>>ed[i].se;
for(int i=0;i<m;i++)cin>>C[i].se>>C[i].fi;
vii p(n);
sort(all(C));
int N = n;
for(int i=0;i<m;i++){
N++; //label this guy as N
p[C[i].se].pb(N);
cost[N] = C[i].fi;
}
for(int i=1;i<n;i++){
int a = ed[i].fi;
int b = ed[i].se;
for(int x:p[i]){
adj[a].pb(x);
adj[x].pb(a);
a = x;
}
adj[a].pb(b);
adj[b].pb(a);
}
dfs(1,-1);
while(q--){
int s,t,x;
ll y;
cin>>s>>t>>x>>y;
int v = lca(s,t);
vector<int>path;
while(s!=v){
if(s>n)path.pb(s);
s = par[0][s];
}
while(t!=v){
if(t>n)path.pb(t);
t = par[0][t];
}
sort(all(path));
int cnt = sz(path);
for(int x:path){
if(y>=cost[x]){
y-=cost[x];
cnt--;
}else break;
}
if(x>=cnt)cout<<x-cnt<<'\n';
else cout<<-1<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5152 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
5 ms |
5716 KB |
Output is correct |
6 |
Correct |
6 ms |
5804 KB |
Output is correct |
7 |
Correct |
5 ms |
5548 KB |
Output is correct |
8 |
Correct |
5 ms |
5844 KB |
Output is correct |
9 |
Correct |
6 ms |
5844 KB |
Output is correct |
10 |
Correct |
5 ms |
5716 KB |
Output is correct |
11 |
Correct |
5 ms |
5716 KB |
Output is correct |
12 |
Correct |
5 ms |
5844 KB |
Output is correct |
13 |
Correct |
67 ms |
6124 KB |
Output is correct |
14 |
Correct |
67 ms |
5972 KB |
Output is correct |
15 |
Correct |
29 ms |
5844 KB |
Output is correct |
16 |
Correct |
23 ms |
5924 KB |
Output is correct |
17 |
Correct |
19 ms |
5800 KB |
Output is correct |
18 |
Correct |
30 ms |
5844 KB |
Output is correct |
19 |
Correct |
4 ms |
5804 KB |
Output is correct |
20 |
Correct |
4 ms |
5716 KB |
Output is correct |
21 |
Correct |
4 ms |
5716 KB |
Output is correct |
22 |
Correct |
5 ms |
5716 KB |
Output is correct |
23 |
Correct |
36 ms |
5916 KB |
Output is correct |
24 |
Correct |
37 ms |
5888 KB |
Output is correct |
25 |
Correct |
34 ms |
5908 KB |
Output is correct |
26 |
Correct |
5 ms |
5844 KB |
Output is correct |
27 |
Correct |
5 ms |
5716 KB |
Output is correct |
28 |
Correct |
5 ms |
5844 KB |
Output is correct |
29 |
Correct |
4 ms |
5792 KB |
Output is correct |
30 |
Correct |
5 ms |
5804 KB |
Output is correct |
31 |
Correct |
5 ms |
5788 KB |
Output is correct |
32 |
Correct |
5 ms |
5784 KB |
Output is correct |
33 |
Correct |
66 ms |
6144 KB |
Output is correct |
34 |
Correct |
66 ms |
6156 KB |
Output is correct |
35 |
Correct |
66 ms |
6148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
5 ms |
5784 KB |
Output is correct |
3 |
Correct |
5 ms |
5844 KB |
Output is correct |
4 |
Correct |
6 ms |
5844 KB |
Output is correct |
5 |
Correct |
206 ms |
38112 KB |
Output is correct |
6 |
Correct |
239 ms |
32528 KB |
Output is correct |
7 |
Correct |
213 ms |
32340 KB |
Output is correct |
8 |
Correct |
170 ms |
31732 KB |
Output is correct |
9 |
Correct |
190 ms |
32352 KB |
Output is correct |
10 |
Correct |
260 ms |
40252 KB |
Output is correct |
11 |
Correct |
271 ms |
40240 KB |
Output is correct |
12 |
Correct |
274 ms |
40460 KB |
Output is correct |
13 |
Correct |
279 ms |
40232 KB |
Output is correct |
14 |
Correct |
275 ms |
40312 KB |
Output is correct |
15 |
Execution timed out |
5051 ms |
53192 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
65 ms |
6100 KB |
Output is correct |
3 |
Correct |
67 ms |
6152 KB |
Output is correct |
4 |
Correct |
66 ms |
6188 KB |
Output is correct |
5 |
Execution timed out |
5065 ms |
44684 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5152 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
5 ms |
5716 KB |
Output is correct |
6 |
Correct |
6 ms |
5804 KB |
Output is correct |
7 |
Correct |
5 ms |
5548 KB |
Output is correct |
8 |
Correct |
5 ms |
5844 KB |
Output is correct |
9 |
Correct |
6 ms |
5844 KB |
Output is correct |
10 |
Correct |
5 ms |
5716 KB |
Output is correct |
11 |
Correct |
5 ms |
5716 KB |
Output is correct |
12 |
Correct |
5 ms |
5844 KB |
Output is correct |
13 |
Correct |
67 ms |
6124 KB |
Output is correct |
14 |
Correct |
67 ms |
5972 KB |
Output is correct |
15 |
Correct |
29 ms |
5844 KB |
Output is correct |
16 |
Correct |
23 ms |
5924 KB |
Output is correct |
17 |
Correct |
19 ms |
5800 KB |
Output is correct |
18 |
Correct |
30 ms |
5844 KB |
Output is correct |
19 |
Correct |
4 ms |
5804 KB |
Output is correct |
20 |
Correct |
4 ms |
5716 KB |
Output is correct |
21 |
Correct |
4 ms |
5716 KB |
Output is correct |
22 |
Correct |
5 ms |
5716 KB |
Output is correct |
23 |
Correct |
36 ms |
5916 KB |
Output is correct |
24 |
Correct |
37 ms |
5888 KB |
Output is correct |
25 |
Correct |
34 ms |
5908 KB |
Output is correct |
26 |
Correct |
5 ms |
5844 KB |
Output is correct |
27 |
Correct |
5 ms |
5716 KB |
Output is correct |
28 |
Correct |
5 ms |
5844 KB |
Output is correct |
29 |
Correct |
4 ms |
5792 KB |
Output is correct |
30 |
Correct |
5 ms |
5804 KB |
Output is correct |
31 |
Correct |
5 ms |
5788 KB |
Output is correct |
32 |
Correct |
5 ms |
5784 KB |
Output is correct |
33 |
Correct |
66 ms |
6144 KB |
Output is correct |
34 |
Correct |
66 ms |
6156 KB |
Output is correct |
35 |
Correct |
66 ms |
6148 KB |
Output is correct |
36 |
Correct |
3 ms |
5204 KB |
Output is correct |
37 |
Correct |
5 ms |
5784 KB |
Output is correct |
38 |
Correct |
5 ms |
5844 KB |
Output is correct |
39 |
Correct |
6 ms |
5844 KB |
Output is correct |
40 |
Correct |
206 ms |
38112 KB |
Output is correct |
41 |
Correct |
239 ms |
32528 KB |
Output is correct |
42 |
Correct |
213 ms |
32340 KB |
Output is correct |
43 |
Correct |
170 ms |
31732 KB |
Output is correct |
44 |
Correct |
190 ms |
32352 KB |
Output is correct |
45 |
Correct |
260 ms |
40252 KB |
Output is correct |
46 |
Correct |
271 ms |
40240 KB |
Output is correct |
47 |
Correct |
274 ms |
40460 KB |
Output is correct |
48 |
Correct |
279 ms |
40232 KB |
Output is correct |
49 |
Correct |
275 ms |
40312 KB |
Output is correct |
50 |
Execution timed out |
5051 ms |
53192 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |