# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1038534 | 2024-07-29T22:40:23 Z | mkkkkkkkk | Fountain (eJOI20_fountain) | C++14 | 404 ms | 41108 KB |
#include <bits/stdc++.h> using namespace std; int cap[200001],parent[200001],ps[200001],depth[200001]; vector<int> adj[200001]; int sp[21][200001]; int logg[200001]; void dfs(int i,int par,int d) { ps[i]+=ps[par]; depth[i]=d; for(auto it : adj[i]) { if(par!=it) { dfs(it,i,d+1); } } } #include <bits/extc++.h> using namespace __gnu_pbds; typedef tree<pair<int,int>,null_type,less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> ost; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n,q; cin>>n>>q; ost st; vector<pair<int,int>> vec; for(int i=1;i<=n;i++) { int a,b; cin>>a>>b; cap[i]=b; ps[i]=b; vec.push_back({a,-i}); } sort(vec.begin(),vec.end()); for(int j=n;j>0;j--) { int a=vec[j-1].first,i=-(vec[j-1].second); st.insert({i,a}); int br=0; int brr=st.order_of_key({i,a}); if(brr==st.size()-1) { parent[i]=0; adj[0].push_back(i); adj[i].push_back(0); } else { br=(*st.find_by_order(brr+1)).first; adj[br].push_back(i); adj[i].push_back(br); parent[i]=br; } } dfs(0,0,0); sp[0][0]=-1; for(int i=1;i<=n;i++) { sp[0][i]=parent[i]; } for(int i=1;i<=20;i++) { for(int j=0;j<=n;j++) { if(sp[i-1][j]==-1) sp[i][j]=-1; else sp[i][j]=sp[i-1][sp[i-1][j]]; } } for(int i=1,j=0;i<=200000;i=i*2,j++) { logg[i]=j; } for(;q>0;q--) { int a,b; cin>>a>>b; if(cap[a]>=b) { cout<<a<<"\n"; continue; } int l=0,r=depth[a]-1; int m=(l+r)/2; for(;l<=r;m=(l+r)/2) { int br=m,br2=a; for(int brr=0;br>0;br=br>>1,brr++) { if(br&1==1) { br2=sp[brr][br2]; } } br2=parent[br2]; if(ps[a]-ps[br2]<b) { l=m+1; } else r=m-1; } int br=m,br2=a; for(int brr=0;br>0;br=br>>1,brr++) { if(br&1==1) { br2=sp[brr][br2]; } } br2=parent[br2]; cout<<br2<<"\n"; } return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 24924 KB | Output is correct |
2 | Correct | 3 ms | 24924 KB | Output is correct |
3 | Correct | 3 ms | 25180 KB | Output is correct |
4 | Correct | 4 ms | 25176 KB | Output is correct |
5 | Correct | 4 ms | 25180 KB | Output is correct |
6 | Correct | 4 ms | 25204 KB | Output is correct |
7 | Correct | 3 ms | 25180 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 273 ms | 39868 KB | Output is correct |
2 | Correct | 287 ms | 39084 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 24924 KB | Output is correct |
2 | Correct | 3 ms | 24924 KB | Output is correct |
3 | Correct | 3 ms | 25180 KB | Output is correct |
4 | Correct | 4 ms | 25176 KB | Output is correct |
5 | Correct | 4 ms | 25180 KB | Output is correct |
6 | Correct | 4 ms | 25204 KB | Output is correct |
7 | Correct | 3 ms | 25180 KB | Output is correct |
8 | Correct | 273 ms | 39868 KB | Output is correct |
9 | Correct | 287 ms | 39084 KB | Output is correct |
10 | Correct | 4 ms | 25180 KB | Output is correct |
11 | Correct | 152 ms | 31640 KB | Output is correct |
12 | Correct | 404 ms | 41108 KB | Output is correct |
13 | Correct | 342 ms | 37324 KB | Output is correct |
14 | Correct | 137 ms | 36296 KB | Output is correct |
15 | Correct | 87 ms | 36776 KB | Output is correct |