Submission #1020126

#TimeUsernameProblemLanguageResultExecution timeMemory
1020126biserailievaFountain (eJOI20_fountain)C++14
100 / 100
499 ms42948 KiB
#include <bits/stdc++.h> using namespace std; #define int long long int rad[500001],cap[500001]; array <int,2> sum[500001][21]; vector <int> v; signed main() { int n,q; cin>>n>>q; for(int i=1;i<=n;i++) cin>>rad[i]>>cap[i]; for(int i=n;i>=1;i--){ while(v.size()!=0&&rad[v.back()]<=rad[i]) v.pop_back(); if(v.size()!=0) sum[i][0]={cap[i],v.back()}; else sum[i][0]={cap[i],0}; v.push_back(i); } for(int k=1;k<=20;k++){ for(int i=1;i<=n;i++){ sum[i][k][1]=sum[sum[i][k-1][1]][k-1][1]; sum[i][k][0]=sum[i][k-1][0]+sum[sum[i][k-1][1]][k-1][0]; } } while(q--){ int i,val; cin>>i>>val; for(int k=20;k>=0;k--){ if(val-sum[i][k][0]>0){ val-=sum[i][k][0]; i=sum[i][k][1]; } } cout<<i<<endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...