Submission #468659

#TimeUsernameProblemLanguageResultExecution timeMemory
468659mychecksedadFountain (eJOI20_fountain)C++17
100 / 100
318 ms23536 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5+10, K = 20; #define pb push_back int n, qq, c[N], d[N], dp[N][K+1], go[N][K+1], a, b; int main(){ cin.tie(0); ios::sync_with_stdio(0); cin >> n >> qq; for(int i = 1; i <= n; i++){ cin >> d[i] >> c[i]; } deque<pair<int, int>> q; q.pb({1e9+7, n + 1}); go[n + 1][0] = n + 1; c[n + 1] = 0; dp[n + 1][0] = 0; for(int i = n; i >= 1; i--){ while(!q.empty() && q.back().first <= d[i]) q.pop_back(); go[i][0] = q.back().second; dp[i][0] = c[i]; q.pb({d[i], i}); } for(int k = 1; k <= K; k++){ for(int v = 1; v <= n+1; v++){ go[v][k] = go[go[v][k - 1]][k - 1]; dp[v][k] = dp[v][k - 1] + dp[go[v][k-1]][k-1]; // cout << go[v][k] << ' ' << v << ' ' << k << endl; } // cout << endl; } for(int i = 0; i < qq; i++){ cin >> a >> b; int v = a; if(c[v] >= b){ cout << v << '\n'; continue; } for(int i = K; i > 0; i--){ if(dp[v][i] < b) b -= dp[v][i], v = go[v][i]; // cout << v<<endl; } b -= dp[v][0]; if(dp[go[v][0]][0] >= b && b>0) v = go[v][0]; cout << (v==n+1?0:v) << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...