제출 #1132170

#제출 시각아이디문제언어결과실행 시간메모리
1132170lopkusFountain (eJOI20_fountain)C++20
100 / 100
644 ms21932 KiB
#include <bits/stdc++.h> #define int long long using namespace std; const int N=1e5+50; int n; int q; int koliko[N]; int staje[N]; int up[N][21]; int suf[N]; stack<int>s; int P[N]; int kti(int n,int k){ for(int i=0;i<=20;i++){ if(k&(1ll<<i))n=up[n][i]; } return n; } signed main(){ cin>>n>>q; for(int i=1;i<=n;i++)cin>>koliko[i]>>staje[i]; for(int i=0;i<=n;i++)P[i]=0; for(int i=n;i>=1;i--){ while(s.size()&&koliko[s.top()]<=koliko[i])s.pop(); if(s.size())P[i]=s.top(); s.push(i); } for(int i=0;i<=n;i++)suf[i]=0; for(int i=1;i<=n;i++)if(!P[i])suf[i]=staje[i]; for(int i=n;i>=1;i--){ if(P[i])suf[i]=suf[P[i]]+staje[i]; } for(int i=0;i<=n;i++)for(int j=0;j<=20;j++)up[i][j]=0; for(int i=1;i<=n;i++){ up[i][0]=P[i]; } for(int i=1;i<=20;i++){ for(int j=1;j<=n;j++){ up[j][i]=up[up[j][i-1]][i-1]; } } while(q--){ int q,k; cin>>q>>k; if(suf[q]<k){ cout<<0; cout<<endl; continue; } int l=0,r=n;int R=-1; while(l<=r){ int mid=(l+r)/2; int X=kti(q,mid); int y=P[X]; if(suf[q]-suf[y]<k)l=mid+1; else { r=mid-1; R=mid; } } cout<<kti(q,R); cout<<endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...