Vases and Flowers HDU - 4614

思路

线段树维护区间空瓶的数量,然后每次放花二分位置得到左右端点的位置.

代码

#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iomanip>
#include<sstream>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define ll __int64
//#define int ll
//typedef  long long ll;
typedef unsigned long long ull;
const int MAXN=5e5+10;
const int MOD=1e9+7;
const double eps=1e-6;
using namespace std;
//-------------------------------------------//
int n,m;
int lazy[MAXN<<2],num[MAXN<<2];
void pushup(int rt)
{
    
    
    num[rt]=num[rt<<1]+num[rt<<1|1];
}
void pushdown(int l,int r,int rt)
{
    
    
    if(lazy[rt]==-1)return;
    int len=r-l+1;
    num[rt<<1]=(len-len/2)*lazy[rt];
    num[rt<<1|1]=(len/2)*lazy[rt];
    lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
    lazy[rt]=-1;
}
void build(int l,int r,int rt)
{
    
    
    lazy[rt]=-1;
    if(l==r)
    {
    
    
        num[rt]=1;
        return;
    }
    int m=l+r>>1;
    build(ls);
    build(rs);
    pushup(rt);
}

void updata(int op,int L,int R,int l,int r,int rt)
{
    
    

    if(L<=l&&r<=R)
    {
    
    
        num[rt]=(r-l+1)*op;
        lazy[rt]=op;
        return;
    }
    pushdown(l,r,rt);
    int m=l+r>>1;
    if(L<=m)updata(op,L,R,ls);
    if(m<R)updata(op,L,R,rs);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    
    
    if(L<=l&&r<=R)
        return num[rt];
    pushdown(l,r,rt);
    int m=l+r>>1,res=0;
    if(L<=m)res+=query(L,R,ls);
    if(m<R)res+=query(L,R,rs);
    return res;
}
int binary(int x,int f)
{
    
    
    int l=x,r=n,ans=0;
    while(l<=r)
    {
    
    
        int m=l+r>>1;
        if(query(x,m,1,n,1)>=f)
        {
    
    
            ans=m;
            r=m-1;
        }
        else l=m+1;
    }
    return ans;
}
int main()
{
    
    
    //ios::sync_with_stdio(false);
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
       scanf("%d%d",&n,&m);
       build(1,n,1);
       while(m--)
       {
    
    
           int op,a,b;
           scanf("%d%d%d",&op,&a,&b);
           if(op==1)
           {
    
    
               //cout<<query(a+1,8,1,n,1)<<endl;
               int cnt=query(a+1,n,1,n,1);
               if(!cnt)
               {
    
    
                   printf("Can not put any one.\n");
                   continue;
               }
               int l=binary(a+1,1);
               int r=binary(a+1,min(b,cnt));
               updata(0,l,r,1,n,1);
               printf("%d %d\n",l-1,r-1);
           }
           else
           {
    
    
               printf("%d\n",b-a+1-query(a+1,b+1,1,n,1));
               updata(1,a+1,b+1,1,n,1);
           }
       }
       printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43638337/article/details/103899043