求助一道倍增题目
  • 板块学术版
  • 楼主czy0323
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/6/25 11:36
  • 上次更新2023/11/3 12:27:51
查看原帖
求助一道倍增题目
538427
czy0323楼主2023/6/25 11:36

P7167

调了很久了,调不出哪里错了,发题目版完全没人看qwq

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define data first
#define idx second
const int N = 1e5+5;
int n, q;
int D[N], C[N], idx[N][25], st[N][25];

stack<pair<int,int>> s;

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	
	cin >> n >> q;
	for(int i = 1; i <= n; i++)
		cin >> D[i] >> C[i];
	for(int i = n; i >= 1; i--){
		while( !s.empty() && s.top().data <= D[i] )
			s.pop();
		if( !s.empty() )
			idx[i][0] = s.top().idx;
		st[i][0] = C[i];
		s.push(make_pair(D[i], i));
	}
	
	for(int j = 1; (1 << j) <= n; j++)
		for(int i = 1; i + (1 << j) - 1 <= n; i++){
			idx[i][j] = idx[idx[i][j - 1]][j - 1];
			st[i][j] = st[i][j - 1] + st[idx[i][j - 1]][j - 1];
		}
	
	while( q-- ){
		int now, v;
		cin >> now >> v;
		while( true ){
			int cnt = 0;
			if( st[now][0] >= v ){
				cout << now << "\n";
				break;
			}
			else if( idx[now][0] == 0 ){
				cout << 0 << "\n";
				break;
			}
			while( st[now][cnt] < v && st[now][cnt] != 0 )
				cnt++;
			cnt--;
			v -= st[now][cnt];
			now = idx[now][cnt];
		}
	}
	return 0;
}
2023/6/25 11:36
加载中...