求助:稀疏表
查看原帖
求助:稀疏表
253936
simonG楼主2022/3/15 13:25

能过样例,但是全WA。
已经开了long long了。

#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
const int MAXN=2e5,LOGN=21;
int n,m;
ll d,f[MAXN+10][LOGN],t;
void Insert(ll x) {
	f[++n][0]=x;
	for(int i=1; i<LOGN&&n-(1<<i)+1>=1; i++)
		f[n][i]=max(f[n][i-1],f[n-(1<<i)+1][i-1]);
}
ll Query(ll x) {
	int l=n-x+1,r=n;
	int p=log(r-l+1)/log(2);
	return max(f[r][p],f[l+(1<<p)-1][p]);
}
int main() {
	scanf("%d %lld",&m,&d);
	for(int i=1; i<=m; i++) {
		ll val; char opt;
		cin>>opt>>val;
		if(opt=='A') Insert(((val+t)%d+d)%d);
		else {
			t=Query(val);
			printf("%lld\n",t);
		}
	}
	return 0;
}
2022/3/15 13:25
加载中...