样例过不了,调试发现修改的位置不对,但不知道是哪里写挂了
#include <bits/stdc++.h>
const int N = 5e4+4;
struct Node {
int l,r;
int ld,md,rd;
int pos,tag;
} tr[N*4];
inline int lc(int p) {return p<<1;}
inline int rc(int p) {return p<<1|1;}
inline int siz(int p) {return tr[p].r-tr[p].l+1;}
inline int mid(int p) {return (tr[p].l+tr[p].r)/2;}
void push_up(int p) {
if (!p) return;
//ld
if (tr[lc(p)].ld==siz(lc(p)))
tr[p].ld=tr[lc(p)].ld+tr[rc(p)].ld;
else
tr[p].ld=tr[lc(p)].ld;
//rd
if (tr[rc(p)].rd==siz(rc(p)))
tr[p].rd=tr[lc(p)].rd+tr[rc(p)].rd;
else
tr[p].rd=tr[rc(p)].rd;
//md
tr[p].md=0,tr[p].pos=0;
if (tr[lc(p)].md>tr[p].md)
tr[p].md=tr[lc(p)].md,
tr[p].pos=tr[lc(p)].pos;
if (tr[lc(p)].rd+tr[rc(p)].ld>tr[p].md)
tr[p].md=tr[lc(p)].rd+tr[rc(p)].ld,
tr[p].pos=tr[rc(p)].l-tr[lc(p)].rd;
if (tr[rc(p)].md>tr[p].md)
tr[p].md=tr[rc(p)].md,
tr[p].pos=tr[rc(p)].pos;
}
void set_in(int p) {
tr[p].ld=tr[p].rd=tr[p].md=0;
tr[p].pos=0;
tr[p].tag=-1;
}
void set_out(int p) {
tr[p].ld=tr[p].rd=tr[p].md=siz(p);
tr[p].pos=tr[p].l;
tr[p].tag=1;
}
void push_down(int p) {
// printf("push_down %d\n",p);
if (!p) return;
if (tr[p].tag==-1) { // out
set_out(lc(p));
set_out(rc(p));
}
else if (tr[p].tag==1) { // in
set_in(lc(p));
set_in(rc(p));
}
tr[p].tag=0;
}
void build(int p,int bl,int br) {
tr[p].l=bl,tr[p].r=br;
if (tr[p].l==tr[p].r) {
tr[p].ld=tr[p].rd=tr[p].md=1;
tr[p].pos=tr[p].l;
return;
}
build(lc(p),tr[p].l,mid(p));
build(rc(p),mid(p)+1,tr[p].r);
push_up(p);
}
void change(int p,int l,int r,int d) { // need debug
// printf("Node [%d,%d], p:%d\n",tr[p].l,tr[p].r,tr[p].pos);
push_down(p);
if (l<=tr[p].l && tr[p].r<=r) {
// printf("C [%d,%d]\n",tr[p].l,tr[p].r);
if (d==-1) set_out(p);
else if (d==1) set_in(p);
return;
}
if (l<=mid(p)) change(lc(p),l,r,d);
if (mid(p)<r) change(rc(p),l,r,d);
push_up(p);
}
int ask(int p,int len) {
push_down(p);
if (tr[p].l==tr[p].r) {
return tr[p].md>=len ? tr[p].pos : 0;
}
int t;
if (t=ask(lc(p),len)) return t;
return tr[p].md>=len ? tr[p].pos : 0;
}
void show(int p) {
push_down(p);
if (tr[p].l==tr[p].r) {
printf("%d:%d ",tr[p].l,tr[p].md);
return;
}
show(lc(p));
show(rc(p));
}
void show() {
puts("show:");
show(1);
putchar('\n');
}
int n,m;
int main() {
scanf("%d%d",&n,&m);
build(1,1,n);
// show();
int opt,x,d;
while (m--) {
scanf("%d",&opt);
if (opt==1) { // in
scanf("%d",&d);
int t=ask(1,d);
if (t) change(1,t,t+d-1,1);
printf("%d\n",t);
}
else { // out
scanf("%d%d",&x,&d);
change(1,x,x+d-1,-1);
}
// show();
}
}