AC代码:
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;
struct node
{
int l,r;
int tmax;
int lmax,rmax;
int add;
}tr[N*4];
int n,m;
void pushup(node &root,node &left,node &right)
{
root.tmax=max(left.tmax,right.tmax);
root.tmax=max(root.tmax,left.rmax+right.lmax);
root.lmax=max(left.lmax,left.tmax==(left.r-left.l+1)?(left.tmax+right.lmax):0);
root.rmax=max(right.rmax,right.tmax==(right.r-right.l+1)?(right.tmax+left.rmax):0);
}
void pushup(int u)
{
pushup(tr[u],tr[u*2],tr[u*2+1]);
}
void pushdown(node &root,node &left,node &right)
{
if(root.add!=-1)
{
left.add=root.add,left.tmax=left.lmax=left.rmax=root.add*(left.r-left.l+1);
right.add=root.add,right.tmax=right.lmax=right.rmax=root.add*(right.r-right.l+1);
root.add=-1;
}
}
void pushdown(int u)
{
pushdown(tr[u],tr[u*2],tr[u*2+1]);
}
void build(int u,int l,int r)
{
tr[u]={l,r,1,1,1,-1};
if(l==r) return;
int mid=l+r>>1;
build(u*2,l,mid),build(u*2+1,mid+1,r);
pushup(u);
}
void modify(int u,int l,int r,int c)
{
if(tr[u].l>=l&&tr[u].r<=r) tr[u].tmax=tr[u].lmax=tr[u].rmax=c*(tr[u].r-tr[u].l+1)*c,tr[u].add=c;
else
{
pushdown(u);
int mid=tr[u].l+tr[u].r>>1;
if(l<=mid) modify(u*2,l,r,c);
if(r>mid) modify(u*2+1,l,r,c);
pushup(u);
}
}
int query(int u,int x)
{
node root=tr[u],left=tr[u*2],right=tr[u*2+1];
if(root.tmax<x) return 0;
pushdown(u);
int ans=query(u*2,x);
if(ans) return ans;
int mid=root.r+root.l>>1;
if(left.rmax+right.lmax>=x) return mid-left.rmax+1;
else return query(u*2+1,x);
return 0;
}
int main()
{
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--)
{
int op,x,l,r;
scanf("%d",&op);
if(op==1)
{
scanf("%d",&x);
int res=query(1,x);
//printf("tmax: %d\n",tr[1].tmax);
if(res) modify(1,res,res+x-1,0);
printf("%d\n",res);
}
else
{
scanf("%d%d",&l,&r);
modify(1,l,l+r-1,1);
}
}
return 0;
}
92分代码:
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;
struct node
{
int l,r;
int tmax;
int lmax,rmax;
int add;
}tr[N*4];
int n,m;
void pushup(node &root,node &left,node &right)
{
root.tmax=max(left.tmax,right.tmax);
root.tmax=max(root.tmax,left.rmax+right.lmax);
root.lmax=max(left.lmax,left.tmax==(left.r-left.l+1)?(left.tmax+right.lmax):0);
root.rmax=max(right.rmax,right.tmax==(right.r-right.l+1)?(right.tmax+left.rmax):0);
}
void pushup(int u)
{
pushup(tr[u],tr[u*2],tr[u*2+1]);
}
void pushdown(node &root,node &left,node &right)
{
if(root.add!=-1)
{
left.add=root.add,left.tmax=left.lmax=left.rmax=root.add*(left.r-left.l+1);
right.add=root.add,right.tmax=right.lmax=right.rmax=root.add*(right.r-right.l+1);
root.add=-1;
}
}
void pushdown(int u)
{
pushdown(tr[u],tr[u*2],tr[u*2+1]);
}
void build(int u,int l,int r)
{
tr[u]={l,r,r-l+1,r-l+1,r-l+1,-1};
if(l==r) return;
int mid=l+r>>1;
build(u*2,l,mid),build(u*2+1,mid+1,r);
pushup(u);
}
void modify(int u,int l,int r,int c)
{
if(tr[u].l>=l&&tr[u].r<=r) tr[u].tmax=tr[u].lmax=tr[u].rmax=c*(tr[u].r-tr[u].l+1)*c,tr[u].add=c;
else
{
pushdown(u);
int mid=tr[u].l+tr[u].r>>1;
if(l<=mid) modify(u*2,l,r,c);
if(r>mid) modify(u*2+1,l,r,c);
pushup(u);
}
}
int query(int u,int x)
//问题1:为啥不能用左右加起来>=右边,2:为啥不能加当tmax<x 就直接return0
{
if(tr[u].l==tr[u].r) return tr[u].r;
node root=tr[u],left=tr[u*2],right=tr[u*2+1];
pushdown(u);
if(left.tmax>=x)
{
return query(u*2,x);
}
int mid=root.r+root.l>>1;
if(left.rmax+right.lmax>=x) return mid-left.rmax+1;
else return query(u*2+1,x);
}
int main()
{
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--)
{
int op,x,l,r;
scanf("%d",&op);
if(op==1)
{
scanf("%d",&x);
int res=query(1,x);
//printf("tmax: %d\n",tr[1].tmax);
if(res!=n) modify(1,res,res+x-1,0);
if(res==n) res=0;
printf("%d\n",res);
}
else
{
scanf("%d%d",&l,&r);
modify(1,l,l+r-1,1);
}
}
return 0;
}