제출 #868960

#제출 시각아이디문제언어결과실행 시간메모리
868960AlphaMale06Fountain (eJOI20_fountain)C++14
100 / 100
249 ms35328 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define F first #define S second #define int long long const int N = 1e5+3; const int inf=1e9+10; int p[N][17]; int sz[N][17]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n>> q; pair<int, int> a[n+1]; for(int i=1; i<= n; i++)cin >> a[i].F >> a[i].S; stack<pair<int, int>> st; st.push(mp(inf, 0)); for(int i=n; i>=1; i--){ while(st.top().F<=a[i].F){ st.pop(); } p[i][0]=st.top().S; sz[i][0]=a[i].S; st.push(mp(a[i].F, i)); } for(int j=0; j<17; j++){ sz[0][j]=inf; } for(int i=n; i>=1; i--){ for(int j=1; j<17; j++){ p[i][j]=p[p[i][j-1]][j-1]; if(p[i][j]==0){ sz[i][j]=inf; } else{ sz[i][j]=sz[i][j-1]+sz[p[i][j-1]][j-1]; } } } while(q--){ int x, val; cin >> x >> val; int ans=x; for(int i=16; i>=0; i--){ if(sz[ans][i]<val){ val-=sz[ans][i]; ans=p[ans][i]; } } if(a[ans].S>=val){ cout << ans << '\n'; } else cout << p[ans][0] << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...