P2894
其实这道题以前也写过博客
可惜当时年纪轻轻不懂事 竟然在学算法的第一个月学XX线段树不说 还竟然用一句话题解
这题我们怎么做呢?我们要记录 前缀最长连续0的个数 ls
后缀最长连续0的个数 rs
区间最长连续0的个数 ms
然后区间替换和一般的区间替换做法相同
找的时候先找左儿子的 ms是否大于 v
如果不满足 找 左儿子rs+右儿子ls 是否大于v
如果还不满足 就找右儿子 ms是否大于v
然后找到以后还要区间替换成 1 即可
2操作就是裸的区间替换成 0了
代码如下
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 50025;
int Max(int a,int b)
{
if(a>b) return a;
return b;
}
int Min(int a,int b)
{
if(a<b) return a;
return b;
}
struct node
{
int ls,rs,ms,down;
}arr[MAX_N<<2];
void up(int rt,int l,int r)
{
arr[rt].ls = arr[rt<<1].ls;
arr[rt].rs = arr[rt<<1|1].rs;
if(arr[rt].ls==mid-l+1) arr[rt].ls += arr[rt<<1|1].ls;
if(arr[rt].rs==r-mid) arr[rt].rs += arr[rt<<1].rs;
arr[rt].ms = Max(arr[rt<<1].ms,arr[rt<<1|1].ms);
arr[rt].ms = Max(arr[rt].ms,Max(arr[rt<<1].rs+arr[rt<<1|1].ls,Max(arr[rt].ls,arr[rt].rs)));
}
void down(int rt,int l,int r)
{
if(arr[rt].down!=-1)
{
arr[rt<<1].down = arr[rt].down;
arr[rt<<1|1].down = arr[rt].down;
arr[rt<<1].rs = arr[rt<<1].ms = arr[rt<<1].ls = (mid- l+1)*(1-arr[rt].down);
arr[rt<<1|1].ls = arr[rt<<1|1].rs = arr[rt<<1|1].ms = (r-mid)*(1-arr[rt].down);
arr[rt].down = -1;
}
}
void build(int rt,int l,int r)
{
arr[rt].down = -1;arr[rt].ls = 0,arr[rt].rs = 0,arr[rt].ms = 0;
if(l==r)
{
arr[rt].ls = arr[rt].rs = arr[rt].ms = 1;
return ;
}
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
up(rt,l,r);
}
void update(int rt,int l,int r,int x,int y,int v)
{
if(x<=l&&r<=y)
{
arr[rt].down = v;
arr[rt].ls = arr[rt].rs = arr[rt].ms = (1-v)*(r-l+1);
return ;
}
down(rt,l,r);
if(x<=mid) update(rt<<1,l,mid,x,y,v);
if(mid<y) update(rt<<1|1,mid+1,r,x,y,v);
up(rt,l,r);
}
int query(int rt,int l,int r,int x,int y,int v)
{
if(l==r)
{
return l;
}
down(rt,l,r);
if(arr[rt<<1].ms>=v) return query(rt<<1,l,mid,x,y,v);
else if(arr[rt<<1].rs+arr[rt<<1|1].ls>=v) return mid - arr[rt<<1].rs + 1;
else if(arr[rt<<1|1].ms>=v) return query(rt<<1|1,mid+1,r,x,y,v);
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,Q,opt,x,y;
scanf("%d%d",&n,&Q);
build(1,1,n);
while(Q--)
{
scanf("%d%d",&opt,&x);
if(opt==1)
{
if(arr[1].ms<x)
{
printf("0\n");
}
else
{
int ans = query(1,1,n,1,n,x);
printf("%d\n",ans);
update(1,1,n,ans,ans+x-1,1);
}
}
else
{
scanf("%d",&y);
update(1,1,n,x,x+y-1,0);
}
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}