http://acm.hdu.edu.cn/showproblem.php?pid=4614

直接线段树维护

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
#include<queue>
#include<bitset>
#include<deque>
#include<numeric>

//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<int,int> pp;
const double eps=1e-9;
const int INF=0x3f3f3f3f;
const ll MOD=1000000007;
const int N=50001;
struct node
{
    int l,r,k;
    int m;
}btr[N*4];
void update(int x,int l,int r,int k)
{
    if(btr[x].l==l&&btr[x].r==r)
    {
        btr[x].k=k;
        btr[x].m=k*(btr[x].r-btr[x].l+1);
        return ;
    }
    if(btr[x].k!=-1)
    {
        btr[(x<<1)].k=btr[(x<<1)|1].k=btr[x].k;
        btr[(x<<1)].m=(btr[(x<<1)].r-btr[(x<<1)].l+1)*btr[(x<<1)].k;
        btr[(x<<1)|1].m=(btr[(x<<1)|1].r-btr[(x<<1)|1].l+1)*btr[(x<<1)|1].k;
    }
    int mid=(btr[x].l+btr[x].r)>>1;
    if(r<=mid)
    update((x<<1),l,r,k);
    else if(l>mid)
    update((x<<1)|1,l,r,k);
    else
    {
        update((x<<1),l,mid,k);
        update((x<<1)|1,mid+1,r,k);
    }
    btr[x].m=btr[(x<<1)].m+btr[(x<<1)|1].m;
    if(btr[(x<<1)].k==btr[(x<<1)|1].k)
    btr[x].k=btr[(x<<1)].k;
    else
    btr[x].k=-1;
}
int get(int x,int l,int r)
{
    if(btr[x].l==l&&btr[x].r==r)
    return btr[x].m;
    if(btr[x].k!=-1)
    return (btr[x].k*(r-l+1));
    int mid=(btr[x].l+btr[x].r)>>1;
    if(r<=mid)
    return get((x<<1),l,r);
    else if(l>mid)
    return get((x<<1)|1,l,r);
    else
    return get((x<<1),l,mid)+get((x<<1)|1,mid+1,r);
}
void build(int x,int l,int r)
{//cout<<x<<" "<<l<<" "<<r<<endl;
    btr[x].l=l;
    btr[x].r=r;
    btr[x].k=0;
    btr[x].m=0;
    if(l==r)
    return ;
    int mid=(l+r)>>1;
    build((x<<1),l,mid);
    build((x<<1)|1,mid+1,r);
}
int bs(int l,int r,int k)
{
    int x=l;
    while(l<=r)
    {
        int m=(l+r)>>1;
        if((m-x+1-get(1,x,m))>=k)
        r=m-1;
        else
        l=m+1;
    }
    return l;
}
int main()
{
    //freopen("data.in","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        build(1,0,n-1);
        while(m--)
        {
            int w;
            scanf("%d",&w);
            if(w==1)
            {
                int a,f;
                scanf("%d %d",&a,&f);
                int tmp=get(1,a,n-1);
                if(tmp==(n-a)||f==0)
                {printf("Can not put any one.\n");continue;}
                f=min(f,n-a-tmp);
                int l=bs(a,n-1,1);
                int r=bs(a,n-1,f);
                printf("%d %d\n",l,r);
                update(1,l,r,1);
            }else
            {
                int l,r;
                scanf("%d %d",&l,&r);
                printf("%d\n",get(1,l,r));
                update(1,l,r,0);
            }
        }
        printf("\n");
    }
    return 0;
}

 

posted on 2013-07-25 21:03  夜->  阅读(184)  评论(0编辑  收藏  举报