P7167 [eJOI 2020 Day1] Fountain

题目

题目

思路

首先一个单调栈求出每一个圆盘往下流到哪,以这个为边,做一个st表。
code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int n,q,stfa[100001][25],sthe[100001][25],x[100001],p[100001],tot,r,v,l;
int fa[100001],z[100001];
int QuantAsk(int r,int v)
{
    
    
	for (int i=l;i>=0;i--)
	{
    
    
		if (v>sthe[r][i]) v-=sthe[r][i],r=stfa[r][i];
	}
	return r;
}
int main()
{
    
    
	cin>>n>>q;
	l=log2(n);
	l++;
	for (int i=1;i<=n;i++)
	{
    
    
		cin>>x[i]>>sthe[i][0];
	}
	for (int i=n;i;i--)
	{
    
    
		while (tot&&x[p[tot]]<=x[i]) tot--;
		stfa[i][0]=p[tot];
		p[++tot]=i;
	}
	for (int i=1;i<=l;i++)
	{
    
    
		for (int j=1;j<=n;j++)
		{
    
    
			stfa[j][i]=stfa[stfa[j][i-1]][i-1];
			sthe[j][i]=sthe[stfa[j][i-1]][i-1]+sthe[j][i-1];
		}
	}
	while (q--)
	{
    
    
		cin>>r>>v;
		cout<<QuantAsk(r,v)<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_49843717/article/details/113832348
今日推荐