时间复杂度正确但TLE求调
查看原帖
时间复杂度正确但TLE求调
947029
Chen_Three楼主2024/12/2 20:05
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int nMax = 1e5 + 5;
struct node{
	int D, C;
}dandiao_stack[nMax];
int nextt[nMax], c[nMax], f[nMax][100], g[nMax][100];
int top;
int work(int pos, int value){
	//cout << "pos = " << pos << " value = " << value << '\n';
	if(value <= c[pos]) return pos;
	for(int i = 32; i >= 0; i--) {
		if(g[pos][i] &&  g[pos][i] <= value && f[pos][i] && i){
			return work(f[pos][i], value - g[pos][i - 1]);
		}else{
			return work(nextt[pos], value - c[pos]);
		}
	}
	return 0;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int N, Q;
	cin >> N >> Q;
	for(int i = 1; i <= N; i++){
		int d;
		cin >> d >> c[i];
		node now = (node){d, i};
		while(top && now.D > dandiao_stack[top].D) nextt[dandiao_stack[top].C] = now.C, top--;
		dandiao_stack[++top] = now;
	}
	while(top) nextt[dandiao_stack[top].C] = 0, top--;
	//for(int i = 1; i <= N; i++) cout << nextt[i] << ' ';
	for(int i = 1; i <= N; i++) f[i][0] = nextt[i];
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= 32; j++){
			f[i][j] = f[f[i][j - 1]][j - 1];
		}
	}
	c[0] = 1e9; 
	for(int i = 1; i <= N; i++) g[i][0] = c[i] + c[nextt[i]];
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= 32; j++){
			g[i][j] = g[i][j - 1] + g[f[i][j - 1]][j - 1] + c[f[i][j]];
		}
	}
	while(Q--){
		int R, V;
		cin >> R >> V;
		cout << work(R, V) << '\n';
	}
	return 0;
}
2024/12/2 20:05
加载中...