求助
查看原帖
求助
768476
gf202276楼主2024/9/24 12:34

为什么代码本地飞快,交上去就TLE了?

#include<bits/stdc++.h>
using namespace std;
#define ls p<<1
#define rs p<<1|1
const int Maxn=4*5e4+1000;
int len[Maxn],pre[Maxn],suf[Maxn],tag[Maxn],all0[Maxn],sz[Maxn];
int eq(int p,int x){
	len[p]=x,pre[p]=x,suf[p]=x;
}
void push_down(int p){
	if(tag[p]!=0){
		if(tag[p]==1){
			eq(ls,0),eq(rs,0);
			all0[ls]=0,all0[rs]=0;
			tag[ls]=1,tag[rs]=1;
		}else{
			eq(ls,sz[ls]),eq(rs,sz[rs]);
			all0[ls]=1,all0[rs]=1;
			tag[ls]=-1,tag[rs]=-1;
		}
		tag[p]=0;
	}
}
void push_up(int p){
	sz[p]=sz[ls]+sz[rs];
	all0[p]=(all0[ls]&all0[rs]);
	if(all0[ls])pre[p]=sz[ls]+pre[rs];
	else pre[p]=pre[ls];
	if(all0[rs])suf[p]=sz[rs]+suf[ls];
	else suf[p]=suf[rs];
	len[p]=max(max(len[ls],len[rs]),suf[ls]+pre[rs]);
}
void build(int p,int s,int t){
	if(s==t){
		len[p]=1,pre[p]=1,suf[p]=1,all0[p]=1,sz[p]=1;
		return;
	}
	int mid=(s+t)/2;
	build(p<<1,s,mid);build(p<<1|1,mid+1,t);
	push_up(p);
} 
void update(int p,int s,int t,int l,int r,int k){
	if(l<=s&&t<=r){
		if(k==1){
			tag[p]=1;
			eq(p,0);all0[p]=0;
		}else{
			tag[p]=-1;
			eq(p,sz[p]);all0[p]=1;
		}
		return;
	}
	push_down(p);
	int mid=(s+t)/2;
	if(l<=mid)update(ls,s,mid,l,r,k);
	if(r>mid)update(rs,mid+1,t,l,r,k);
	push_up(p);
}
int query(int p,int s,int t,int x){
	if(len[p]<x)return -1;
	push_down(p);
	int mid=(s+t)/2,ans=0;
	if(len[ls]>=x){
		ans=query(ls,s,mid,x);	
	}
	else{
		if(suf[ls]+pre[rs]>=x)ans=mid-suf[ls]+1;
		else ans=query(rs,mid+1,t,x);
	} 
	push_up(p);
	return ans;
}
int n,q;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
//	freopen("cow.in","r",stdin);
	//freopen("cow.out","w",stdout);
	cin>>n>>q;
	build(1,1,n); 
	for(int i=1;i<=q;i++){
		int op;
		cin>>op;
		if(op==1){
			int x;
			cin>>x;
			int st=query(1,1,n,x);
			if(st==-1){
				cout<<0<<"\n";
			}else{
				cout<<st<<"\n";
				update(1,1,n,st,st+x-1,1);
			}
		}else{
			int x,y;
			cin>>x>>y;
			update(1,1,n,x,x+y-1,0);
		}
	}
}
/*
10 6
1 3
1 3
1 3
1 3
2 5 5
1 6*/
2024/9/24 12:34
加载中...