跳到正文

个人记录

T22526 Princess Principal

St_john约 2 分钟
原文
文章信息与版本

文章信息

发布
2022年8月2日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 2 分钟

版本

  1. v1当前

题目地址
吐槽一下:画面和题面没有一点关系
倍增博大精深!!!
预处理出,每个点走1,2,4,8,16……步的能到的位置。(比实际大1)
接下来从小到大倍增求解。
如果最后走到的左端点再走一步>右端点,则让答案+1
否则输出无解(有一个a[i]>k)

C++
#include<cstdio>
#include<algorithm>
#define Re register int 
#define _for(i,a,b) for(register int i=(a);i<=(b);++i)
#define __for(i,a,b) for(register int i=(a);i>=(b);--i)
#define il inline
#define pc putchar
using namespace std;
typedef long long ll;
const int N=1e6+10,M=1e9+7,inf=2147483647;
il int re(){
    int x=0;
    bool f=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')
        f|=ch=='-',ch=getchar();
    while(ch>='0'&&ch<='9')
        x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return f?-x:x;
}
void pr(int x){
    if(x<0) pc('-'),x=-x;
    if(x>9) pr(x/10);
    pc(x%10|48);
}
int n,m,k,f[N][22];
ll sum[N];
signed main(){
	n=re(),m=re(),k=re();
	_for(i,1,n)
		sum[i]=sum[i-1]+re();
	_for(i,0,20)
		f[n+1][i]=n+1;
	__for(i,n,1){
		f[i][0]=upper_bound(sum+i,sum+n+1,sum[i-1]+k)-sum;//比实际的大1
		_for(j,1,20)
			f[i][j]=f[f[i][j-1]][j-1];
	}
	while(m--){
		int l=re(),r=re(),ans=0;
		__for(i,20,0)
			if(f[l][i]<=r)
				l=f[l][i],ans|=1<<i;
		if(f[l][0]>r) pr(ans+1),pc('\n');
		else printf("Chtholly\n");
	}
    return 0;
}
/*

*/
原文