poj 3667 hotel 线段树成段更新区间合并

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<sstream>
#include<string>
#include<climits>
#include<stack>
#include<set>
#include<bitset>
#include<cmath>
#include<deque>
#include<map>
#include<queue>
#define iinf 2000000000
#define linf 1000000000000000000LL
#define dinf 1e200
#define eps 1e-5
#define all(v) (v).begin(),(v).end()
#define sz(x)  x.size()
#define pb push_back
#define mp make_pair
#define lng long long
#define sqr(a) ((a)*(a))
#define pii pair<int,int>
#define pll pair<lng,lng>
#define pss pair<string,string>
#define pdd pair<double,double>
#define X first
#define Y second
#define pi 3.14159265359
#define ff(i,xi,n) for(int i=xi;i<=(int)(n);++i)
#define ffd(i,xi,n) for(int i=xi;i>=(int)(n);--i)
#define ffl(i,r) for(int i=head[r];i!=-1;i=edge[i].next)
#define cc(i,j) memset(i,j,sizeof(i))
#define two(x)			((lng)1<<(x))
#define N 55555
#define M 1000000
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define Mod  n
#define Pmod(x) (x%Mod+Mod)%Mod
using namespace std;
typedef vector<int>  vi;
typedef vector<string>  vs;
typedef unsigned int uint;
typedef unsigned lng ulng;
template<class T> inline void checkmax(T &x,T y){if(x<y) x=y;}
template<class T> inline void checkmin(T &x,T y){if(x>y) x=y;}
template<class T> inline T Min(T x,T y){return (x>y?y:x);}
template<class T> inline T Max(T x,T y){return (x<y?y:x);}
template<class T> T gcd(T a,T  b){return (a%b)==0?b:gcd(b,a%b);}
template<class T> T lcm(T a,T b){return a*b/gcd(a,b);}
template<class T> T Abs(T a){return a>0?a:(-a);}
template<class T> inline T lowbit(T n){return (n^(n-1))&n;}
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}
template<class T> inline bool isPrimeNumber(T n)
{if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;}
int msum[N<<2],lsum[N<<2],rsum[N<<2],n,m,opt,clo[N<<2];
void build(int l,int r,int rt)
{
    lsum[rt]=rsum[rt]=msum[rt]=r-l+1;
    clo[rt]=-1;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
}
inline void pushup(int rt,int len)
{
    lsum[rt]=lsum[rt<<1],rsum[rt]=rsum[rt<<1|1];
    if(lsum[rt]==(len-len/2))   lsum[rt]+=lsum[rt<<1|1];
    if(rsum[rt]==len/2) rsum[rt]+=rsum[rt<<1];
    msum[rt]=Max(Max(msum[rt<<1],msum[rt<<1|1]),rsum[rt<<1]+lsum[rt<<1|1]);
}
inline void  pushdown(int rt,int len)
{
    if(clo[rt]!=-1)
    {
        clo[rt<<1]=clo[rt<<1|1]=clo[rt];
        msum[rt<<1]=lsum[rt<<1]=rsum[rt<<1]=clo[rt]?0:len-len/2;
        msum[rt<<1|1]=lsum[rt<<1|1]=rsum[rt<<1|1]=clo[rt]?0:len/2;
        clo[rt]=-1;
    }
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        msum[rt]=lsum[rt]=rsum[rt]=c?0:r-l+1;
        clo[rt]=c;
        return;
    }
    int mid=(l+r)>>1;
    pushdown(rt,r-l+1);
    if(L<=mid) update(L,R,c,lson);
    if(R>mid) update(L,R,c,rson);
    pushup(rt,r-l+1);
}
int query(int k,int l,int r,int rt)
{
    if(l==r) return l;
    pushdown(rt,r-l+1);
    int mid=(l+r)>>1;
    if(msum[rt<<1]>=k) return query(k,lson);
    else
    if(rsum[rt<<1]+lsum[rt<<1|1]>=k) return mid-rsum[rt<<1]+1;
    else
    return query(k,rson);
}
int main()
{
    while(scanf("%d%d",&n,&m)==2)
{
    build(1,n,1);
    ff(i,1,m)
    {
        scanf("%d",&opt);
        if(opt==1)
        {
            int k;
            scanf("%d",&k);
            if(msum[1]<k)
            puts("0");
            else
            {
                int u=query(k,1,n,1);
                printf("%d\n",u);
               update(u,u+k-1,1,1,n,1);
            }
        }
        else
        {
            int x,y;
            scanf("%d%d",&x,&y);
            update(x,y+x-1,0,1,n,1);
        }
    }
}
    return 0;
}

你可能感兴趣的:(c,query,Build,n2)