제출 #468654

#제출 시각아이디문제언어결과실행 시간메모리
468654mychecksedadFountain (eJOI20_fountain)C++17
0 / 100
227 ms18220 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;
		// cout << q.back().second <<"F" << endl;
		dp[i][0] = c[q.back().second] + 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]][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>0) v = go[v][i], b -= dp[v][i];
			// cout << u<<endl;
		}
		if(dp[v][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...