Submission #1087378

#TimeUsernameProblemLanguageResultExecution timeMemory
1087378MuhammadSaramFountain (eJOI20_fountain)C++17
100 / 100
442 ms20076 KiB
#include <bits/stdc++.h> using namespace std; int main() { int n,q,lg=17; cin>>n>>q; int nxt[n+1][lg]; stack<int> st; int a[n],b[n],su[n+1][lg]={}; for (int i=0;i<n;i++) cin>>a[i]>>b[i],su[i][0]=b[i]; nxt[n][0]=n; for (int i=n-1;i>=0;i--) { while (!st.empty() && a[st.top()]<=a[i]) st.pop(); if (st.empty()) nxt[i][0]=n; else nxt[i][0]=st.top(); st.push(i); } for (int p=1;p<lg;p++) for (int i=0;i<=n;i++) nxt[i][p]=nxt[nxt[i][p-1]][p-1],su[i][p]=su[i][p-1]+su[nxt[i][p-1]][p-1]; while (q--) { int id,x; cin>>id>>x; id--; for (int p=lg-1;p>=0 && id<n;p--) if (su[id][p]<x) x-=su[id][p],id=nxt[id][p]; cout<<(id+1)%(n+1)<<endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...