HDU 4614

/*
HDU 4614
Vases and Flowers
二分 + 线段树
*/
#include
#include
#include
using namespace std;
#define lson (pos<<1)
#define rson (pos<<1|1)
const int maxn = 55555;
int n,m,pos_x,pos_y;
struct Node{
    int l,r,col,cover;
    int mid(){
        return (l + r) >> 1;
    }
    int len(){
        return  (r - l + 1);
    }
}node[maxn << 2];
void pushup(int pos){
    node[pos].cover = node[lson].cover + node[rson].cover;
}
void pushdown(int pos){
    if(node[pos].col != -1){
        node[lson].cover = node[lson].len() * node[pos].col;
        node[lson].col = node[pos].col;
        node[rson].cover = node[rson].len() * node[pos].col;
        node[rson].col = node[pos].col;
        node[pos].col = -1;
    }
}
void build(int l,int r,int pos){
    node[pos].col = -1;
    node[pos].l = l;
    node[pos].r = r;
    if(l == r){
        node[pos].cover = 1;
        return;
    }
    int mid = (l + r) >> 1;
    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){
        node[pos].cover = d * node[pos].len();
        node[pos].col   = d;
        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);
    return;
}
int query(int l,int r,int pos,int op){
    if(l <= node[pos].l && node[pos].r <= r){
        if(op == 0)
            return node[pos].len() - node[pos].cover;
        else
            return node[pos].cover;
    }
    int mid = node[pos].mid();
    pushdown(pos);
    int ans = 0;
    if(l <= mid)
        ans += query(l,r,lson,op);
    if(r  > mid)
        ans += query(l,r,rson,op);
    pushup(pos);
    return ans;
}
void solve(int x,int y){
    int  t = query(x,n - 1,1,1);
    if(t == 0){
       printf("Can not put any one.\n");
       return;
    }
    if(t < y)
        y = t;
    int l = x, r = n - 1,mid;
    while(l <= r){
        mid = (l + r) >> 1;
        int v = query(x,mid,1,1);
        if(v >= y){
            pos_y = mid;
            r = mid - 1;
        }
        else
            l = mid + 1;
    }
    l = x;
    r = pos_y;
    while(l <= r){
        mid = (l + r) >> 1;
        int v = query(mid,pos_y,1,1);
        if(v >= y){
            pos_x = mid;
            l = mid + 1;
        }
        else
            r = mid - 1;
    }
    printf("%d %d\n",pos_x,pos_y);
    update(pos_x,pos_y,1,0);
    return;
}
int main(){
    int T,op,x,y;;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        build(0,n - 1,1);
        for(int i = 0; i < m; i++){
            scanf("%d",&op);
            scanf("%d%d",&x,&y);
            if(op == 1){
                solve(x,y);
            }
            else{
                printf("%d\n",query(x,y,1,0));
                update(x,y,1,1);
            }
        }
        puts("");
    }
    return 0;
}

你可能感兴趣的:(数据结构)