답안 #463310

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
463310 2021-08-11T03:53:13 Z amunduzbaev Fountain (eJOI20_fountain) C++14
100 / 100
479 ms 40084 KB
#include "bits/stdc++.h"
using namespace std;

#define int long long

void solve(){
	int n, q; cin>>n>>q;
	vector<array<int, 20>> par(n),  sum(n);
	vector<array<int, 2>> a(n), ss;
	for(int i=0;i<n;i++) cin>>a[i][0]>>a[i][1], par[i][0] = i, sum[i][0] = 0;
	
	ss.push_back({(int)(1e9+1), n});
	for(int i=n-1;~i;i--){
		while(ss.back()[0] <= a[i][0]) ss.pop_back();
		par[i][1] = ss.back()[1];
		sum[i][1] = a[i][1];
		ss.push_back({a[i][0], i});
	}

	for(int j=2;j<20;j++){
		for(int i=0;i<n;i++){
			if(par[i][j-1] < n) par[i][j] = par[par[i][j-1]][j-1];
			else par[i][j] = n;
		}
	}
	
	//~ for(int i=0;i<n;i++) cout<<sum[i][0]<<" ";
	//~ cout<<"\n";
	//~ for(int i=0;i<n;i++) cout<<sum[i][1]<<" ";
	//~ cout<<"\n";
	
	for(int j=2;j<20;j++){
		for(int i=0;i<n;i++){
			if(par[i][j-1] < n){
				sum[i][j] = sum[i][j-1] + sum[par[i][j-1]][j-1];
			} else {
				sum[i][j] = sum[i][j-1];
			}
		}
		
		//~ for(int i=0;i<n;i++) cout<<sum[i][j]<<" ";
		//~ cout<<"\n";
	}
	
	//~ cout<<"\n";
	
	while(q--){
		int i, add; cin>>i>>add, i--;
		for(int j=19;~j;j--){
			
			//~ cout<<i<<" "<<sum[i][j]<<"\n";
			
			if(sum[i][j] < add){
				add -= sum[i][j];
				i = par[i][j];
			}
			
			if(i == n){
				break;
			}
		}
		
		if(i == n){
			cout<<0<<"\n";
		} else {
			cout<<i + 1<<"\n";
		}
	}
}

/*

6 5
4 10
6 8
3 5
4 14
10 9
4 20

1 25
6 30
5 8
3 13
2 8

*/

signed main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	int t = 1;
	//~ cin>>t;
	while(t--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 330 ms 35240 KB Output is correct
2 Correct 302 ms 34484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 330 ms 35240 KB Output is correct
9 Correct 302 ms 34484 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 157 ms 21072 KB Output is correct
12 Correct 479 ms 40084 KB Output is correct
13 Correct 449 ms 38332 KB Output is correct
14 Correct 299 ms 37248 KB Output is correct
15 Correct 263 ms 37524 KB Output is correct