Submission #1359977

#TimeUsernameProblemLanguageResultExecution timeMemory
1359977cz_daniFountain (eJOI20_fountain)C++20
100 / 100
367 ms40052 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pii pair<int, int>
#define fi first
#define se second

signed main() {
	int n, q;
	cin >> n >> q;
	vector<int> at(n+1), kap(n+1);
	at[0]=10000000000;
	kap[0]=10000000000;
	for (int i = 1; i <= n; i++) {
		cin >> at[i] >> kap[i];
	}
	vector<vector<pii>> up(n+1, vector<pii>(20));
	stack<pii> st;
	st.push({10000000000, 0});
	for (int i = n; i >= 1; i--) {
		while (st.top().fi<=at[i])st.pop();
		up[i][0]={st.top().se, kap[i]};
		st.push({at[i], i});
	}
	for (int i = n; i >= 1; i--) {
		for (int j = 1; j < 20; j++) {
			up[i][j]={up[up[i][j-1].fi][j-1].fi, up[i][j-1].se+up[up[i][j-1].fi][j-1].se};
		}
	}
	while (q--) {
		int x, y;
		cin >> x >> y;
		for (int j = 19; j >= 0; j--) {
			if (up[x][j].se<y) {
				y-=up[x][j].se;
				x=up[x][j].fi;
			}
		}
		cout << x << '\n';
	}
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...