160582025-03-28 15:26:06tomi7Szökőkútcpp17Wrong answer 0/100550ms262144 KiB
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main() {
	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<int>> 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);
			visited[i]=true;
			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;
				visited[x]=true;
				holtalalom[x]=i;
				hanyadikban[x]=prefix[i].size()-1;
			}
		}
	}
	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(), z+ossz)-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';
			}
		}
	}
}

// prefix: 0 10 18 27 
// index:  0  1  2  5

SubtaskSumTestVerdictTimeMemory
subtask10/30
1Wrong answer1ms508 KiB
2Wrong answer3ms508 KiB
3Wrong answer3ms416 KiB
4Wrong answer4ms564 KiB
5Wrong answer6ms416 KiB
6Wrong answer8ms1592 KiB
7Wrong answer7ms564 KiB
subtask20/30
1Wrong answer386ms13792 KiB
2Wrong answer416ms13136 KiB
subtask30/40
1Wrong answer6ms756 KiB
2Runtime error476ms262144 KiB
3Wrong answer532ms15852 KiB
4Runtime error488ms262144 KiB
5Wrong answer550ms30316 KiB
6Wrong answer544ms25904 KiB