164382025-04-30 15:34:32algoproSzökőkútcpp17Wrong answer 30/100143ms14132 KiB
// UUID: 81f5612e-7c77-492a-959c-b8a9d4c128a0
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, q;cin>>n>>q;
	vector<int> a(n);
	vector<int> c(n);
	vector<int> hova(n, -1);
	stack<int> s;
	for(int i=0;i<n;i++){
		cin>>a[i]>>c[i];
		while(!s.empty() && a[s.top()]<a[i]){
			hova[s.top()]=i;
			s.pop();
		}
		s.push(i);
	}
	vector<vector<long long>> prefix(n);
	vector<vector<int>> index(n);
	vector<bool> visited(n);
	vector<int> holtalalom(n, -1);
	vector<int> hanyadikban(n, -1);
	for(int i=0;i<n;i++){
		if(visited[i]){
			continue;
		}else{
			prefix[i].push_back(0);
			index[i].push_back(0);
			int x=i;
			while(x!=-1){
				prefix[i].push_back(c[x]+prefix[i].back());
				index[i].push_back(x);
				x=hova[x];
				if (x == -1) break;
				if(visited[x]){
					break;
				}
				visited[x]=true;
				holtalalom[x]=i;
				hanyadikban[x]=prefix[i].size()-1;
			}
			visited[i]=true;
		}
	}
	while(q--){
		int x, y;cin>>x>>y;
		if(holtalalom[x-1]!=-1){
			int h=holtalalom[x-1], z=hanyadikban[x-1];
			int ossz=prefix[h][z];
			y+=ossz;
			int hi=lower_bound(prefix[h].begin(), prefix[h].end(), y)-prefix[h].begin();
			if(hi==prefix[h].size()){
				cout<<0<<'\n';
			}else{
				cout<<index[h][hi]+1<<'\n';
			}
		}else{
			int hi=lower_bound(prefix[x-1].begin(), prefix[x-1].end(), y)-prefix[x-1].begin();
			if(hi==prefix[x-1].size()){
				cout<<0<<'\n';
			}else{
				cout<<index[x-1][hi]+1<<'\n';
			}
		}
	}
}
SubtaskSumTestVerdictTimeMemory
subtask10/30
1Wrong answer1ms316 KiB
2Wrong answer1ms524 KiB
3Wrong answer1ms508 KiB
4Wrong answer2ms316 KiB
5Accepted2ms316 KiB
6Wrong answer2ms316 KiB
7Wrong answer2ms392 KiB
subtask230/30
1Accepted93ms8976 KiB
2Accepted98ms8496 KiB
subtask30/40
1Wrong answer2ms508 KiB
2Wrong answer57ms5952 KiB
3Accepted134ms9792 KiB
4Wrong answer114ms10612 KiB
5Wrong answer123ms10804 KiB
6Wrong answer143ms14132 KiB