제출 #631841

#제출 시각아이디문제언어결과실행 시간메모리
631841CyberCowFountain (eJOI20_fountain)C++17
100 / 100
312 ms46696 KiB
#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <string> #include <cmath> #include <map> #include <unordered_map> #include <unordered_set> #include <fstream> #include <iomanip> #include <iterator> #include <stack> #include <deque> using namespace std; using ll = long long; ll d[100005], ch[100005]; stack<int> s; pair<int, ll> p[100005][25]; void solve() { int n, i, j, q; ll x, y; cin >> n >> q; for ( i = 1; i <= n; i++) { cin >> d[i] >> ch[i]; } for ( i = n; i > 0; i--) { while (!s.empty() && d[s.top()] <= d[i]) { s.pop(); } if (!s.empty()) { p[i][0].first = s.top(); p[i][0].second = ch[s.top()]; } s.push(i); } for ( i = 1; i <= 20; i++) { for ( j = 1; j <= n; j++) { p[j][i].first = p[p[j][i - 1].first][i - 1].first; p[j][i].second = p[j][i - 1].second + p[p[j][i - 1].first][i - 1].second; } } for ( i = 0; i < q; i++) { cin >> x >> y; if (ch[x] >= y) { cout << x << '\n'; continue; } y -= ch[x]; int ans = x; for ( j = 20; j > 0; j--) { if (p[ans][j - 1].second < y) { y -= p[ans][j - 1].second; ans = p[ans][j - 1].first; } } cout << p[ans][0].first << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int tt = 1; //cin >> tt; while (tt--) { solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...