HDU 4553

/*
HDU 4553
约会安排
线段树区间合并
线段树最左子区间
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson (pos<<1)
#define rson (pos<<1|1)
const int maxn = 122222;
struct Node{
    int l,r;
    int lx1,rx1,mx1,col1;
    int lx2,rx2,mx2,col2;
    int mid(){
        return (l + r) >> 1;
    }
    int len(){
        return (r - l + 1);
    }
    void change1(int t){
        if(t == 0){
            col1 = 0;
            lx1 = rx1 = mx1 = 0;
        }
        else{
            col1 = 1;
            lx1 = rx1 = mx1 = len();
        }
    }
    void change2(int t){
        if(t == 0){
            col2 = 0;
            lx2 = rx2 = mx2 = 0;
        }
        else{
            col2 = 1;
            lx2 = rx2 = mx2 = len();
        }
    }
}node[maxn << 2];
void pushdown(int pos){
    if(node[pos].col1 != -1){
        node[lson].change1(node[pos].col1);
        node[rson].change1(node[pos].col1);
        node[pos].col1 = -1;
    }
    if(node[pos].col2 != -1){
        node[lson].change2(node[pos].col2);
        node[rson].change2(node[pos].col2);
        node[pos].col2 = -1;
    }
}
void pushup(int pos){
    //包含屌丝的区间
    node[pos].lx1 = node[lson].lx1;
    node[pos].rx1 = node[rson].rx1;
    if(node[pos].lx1 == node[lson].len()) node[pos].lx1 += node[rson].lx1;
    if(node[pos].rx1 == node[rson].len()) node[pos].rx1 += node[lson].rx1;
    node[pos].mx1 = node[lson].rx1 + node[rson].lx1;
    node[pos].mx1 = max(node[pos].mx1,max(node[lson].mx1,node[rson].mx1));
    //不包含屌丝的区间
    node[pos].lx2 = node[lson].lx2;
    node[pos].rx2 = node[rson].rx2;
    if(node[pos].lx2 == node[lson].len()) node[pos].lx2 += node[rson].lx2;
    if(node[pos].rx2 == node[rson].len()) node[pos].rx2 += node[lson].rx2;
    node[pos].mx2 = node[lson].rx2 + node[rson].lx2;
    node[pos].mx2 = max(node[pos].mx2,max(node[lson].mx2,node[rson].mx2));
}
void build(int l,int r,int pos){
    node[pos].l = l;
    node[pos].r = r;
    node[pos].col1 = node[pos].col2 = -1;
    if(l == r){
        node[pos].lx1 = node[pos].rx1 = node[pos].mx1 = 1;
        node[pos].lx2 = node[pos].rx2 = node[pos].mx2 = 1;
        return;
    }
    int mid = node[pos].mid();
    build(l,mid,lson);
    build(mid + 1,r,rson);
    pushup(pos);
}
void update(int l,int r,int pos,int d){
     if(l <= node[pos].l && node[pos].r <= r){
         if(d == 0){    //清0
             node[pos].change1(1);
             node[pos].change2(1);
         }
         else if(d == 1){//放进来屌丝
             node[pos].change1(0);
         }
         else if(d == 2){//放进来女神
             node[pos].change1(0);
             node[pos].change2(0);
         }
         return;
     }
     pushdown(pos);
     int mid = node[pos].mid();
     if(l <= mid)
        update(l,r,lson,d);
     if(r  > mid)
        update(l,r,rson,d);
     pushup(pos);
}
int query(int l,int r,int pos,int d){
    if(node[pos].len() == node[pos].mx1)
        return node[pos].l;
    pushdown(pos);
    int ans;
    if(node[lson].mx1 >= d)
        ans = query(l,r,lson,d);
    else if(node[lson].rx1 + node[rson].lx1 >= d){
        int rr = node[pos].mid() + node[rson].lx1;
        int v1 = node[lson].rx1 + node[rson].lx1 - d;
        ans = rr - v1 - d + 1;
    }
    else
        ans = query(l,r,rson,d);
    pushup(pos);
    return ans;
}
int query2(int l,int r,int pos,int d){
    if(node[pos].len() == node[pos].mx2)
        return node[pos].l;
    pushdown(pos);
    int ans;
    if(node[lson].mx2 >= d)
        ans = query2(l,r,lson,d);
    else if(node[lson].rx2 + node[rson].lx2 >= d){
        int rr = node[pos].mid() + node[rson].lx2;
        int v1 = node[lson].rx2 + node[rson].lx2 - d;
        ans = rr - v1 - d + 1;
    }
    else
        ans = query2(l,r,rson,d);
    pushup(pos);
    return ans;
}
int main(){
    int T,Case = 1;
    char op[10];
    scanf("%d",&T);
    while(T--){
        int n,m,x,y;
        scanf("%d%d",&n,&m);
        build(1,n,1);
        printf("Case %d:\n",Case++);
        for(int i = 0; i < m; i++){
            scanf("%s",op);
            if(op[0] == 'D'){
                scanf("%d",&x);
                if(node[1].mx1 >= x){
                    int v = query(1,n,1,x);
                    printf("%d,let's fly\n",v);
                    update(v,v + x - 1,1,1);
                }
                else
                    printf("fly with yourself\n");
            }
            else if(op[0] == 'N'){
                scanf("%d",&x);
                if(node[1].mx1 >= x){
                    int v = query(1,n,1,x);
                    printf("%d,don't put my gezi\n",v);
                    update(v,v + x - 1,1,2);
                }
                else if(node[1].mx2 >= x){
                    int v = query2(1,n,1,x);
                    printf("%d,don't put my gezi\n",v);
                    update(v,v + x - 1,1,2);
                }
                else
                    printf("wait for me\n");
            }
            else if(op[0] == 'S'){
                scanf("%d%d",&x,&y);
                update(x,y,1,0);
                printf("I am the hope of chinese chengxuyuan!!\n");
            }
            //printf("%d %d\n",node[1].mx1,node[1].mx2);
        }
    }
    return 0;
}
/*
DS 10
NS 50
*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值