为什么总是死循环(开O2优化TLE,不开MLE)
查看原帖
为什么总是死循环(开O2优化TLE,不开MLE)
369939
纪元马云飞楼主2023/7/5 13:15

能过样例,but爆0

刚开始3个特判都没加,发现全MLE,然后开了O2,全TLE

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const long long N=2e5+5;
struct node
{
	long long mx,lazy;
}tmp,t[N*4];
long long T,x,y,n,q,p;
char opt;
node operator + (node X,node Y)
{
	node Z;
	if(X.mx==-2e9||Y.mx==-2e9) Z.mx=max(X.mx,Y.mx)%p;
	else Z.mx=max(X.mx%p,Y.mx%p);
	Z.lazy=0;
	return Z;
}
void build(int l,int r,int id)
{
	if(l==r)
	{
		t[id].mx=-2e9;
		t[id].lazy=0; 
		return;
	}
	int m=(l+r)/2;
	build(l,m,id*2);
	build(m+1,r,id*2+1);
	t[id]=t[id*2]+t[id*2+1];
	return;
}
void change(int l,int r,int id)
{
	if(n<l||r<n) return;//特判1
	if(l==r)
	{
		t[id].mx=x%p;
		return;
	}
	int m=(l+r)/2;
	if(n<=m) change(l,m,id*2);
	else if(m+1<=n) change(m+1,r,id*2+1);
	t[id]=t[id*2]+t[id*2+1];
	return;
}
node query(int l,int r,int id)
{
	if(y<l||r<x) return tmp;//特判2
	if(x<=l&&r<=y) return t[id];
	int m=(l+r)/2;
	if(y<=m) return query(l,m,id*2);
	else if(x>=m+1) return query(m+1,r,id*2+1);
	else return query(l,m,id*2)+query(m+1,r,id*2+1);
}
int main()
{
	cin>>T>>p;
	tmp.mx=-2e9;
	build(1,T,1);
	for(int i=1;i<=T;i++)
	{
		getchar();
		opt=getchar();
		cin>>x;
		if(opt=='A')
		{
			x+=q;
			n++;
			change(1,T,1);
		}
		else if(opt=='Q')
		{
			y=n;
			x=n-x+1;
			q=query(1,T,1).mx; 
			if(q==-2e9) cout<<"*"<<i<<endl;//特判3 
			else cout<<q<<endl;
		}
	}
	return 0;
}

然后,目前全WA,都输出了特判3的‘*’

求大佬QWQ

2023/7/5 13:15
加载中...