传送门
文章目录
- 题意:
- 思路:
题意:
思路:
倍增经典题了,考虑维护一个前缀和sum[i]sum[i]sum[i],对于iii位置我们肯定是贪心的选能到的最远位置,即第一个>sum[i−1]+k>sum[i-1]+k>sum[i−1]+k的位置,让后让f[i][0]=posf[i][0]=posf[i][0]=pos即可。
之后预处理倍增数组,让后对于每组询问不断倍增,最后f[l][0]<=rf[l][0]<=rf[l][0]<=r的话说明无解。
注意处理倍增数组的一些细节,比如需要将n+1n+1n+1的倍增数组跳的位置置为n+1n+1n+1,不然会跳到000位置。或者特判一下000不能跳也可以。
// Problem: 河 我 车 题 错 天 乐 赛
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/16416/E
// Memory Limit: 524288 MB
// Time Limit: 14000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const LL inf=0x3f3f3f3f3f3f3f3f;
const double eps=1e-6;int n,m,k;
int a[N];
int f[N][22],st[N];
LL sum[N];int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=n;i++) {scanf("%d",&a[i]),sum[i]=sum[i-1]+a[i];st[i]=st[i-1]+(a[i]>k);}//sum[n+1]=inf;for(int i=1;i<=n;i++) {int pos=upper_bound(sum+i,sum+1+n,sum[i-1]+k)-sum;f[i][0]=pos;}for(int i=0;i<=20;i++) f[n+1][i]=n+1;for(int j=1;j<=20;j++) for(int i=1;i<=n;i++) {//cout<<i<<' '<<j<<' '<<f[i][j-1]<<' '<<f[f[i][j-1]][j-1]<<endl;f[i][j]=f[f[i][j-1]][j-1];}while(m--) {int l,r; scanf("%d%d",&l,&r);int ans=0;for(int i=20;i>=0;i--) {if(f[l][i]<=r) l=f[l][i],ans+=1<<i;}if(f[l][0]<=r) puts("Chtholly");else printf("%d\n",ans+1);}return 0;
}
/**/