HDU 4614 Vases and Flowers 【线段树】【二分】

Vases and Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 4402    Accepted Submission(s): 1811


Problem Description
  Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some flowers, she will try to put them in the vases, one flower in one vase. She randomly choose the vase A and try to put a flower in the vase. If the there is no flower in the vase, she will put a flower in it, otherwise she skip this vase. And then she will try put in the vase A+1, A+2, ..., N-1, until there is no flower left or she has tried the vase N-1. The left flowers will be discarded. Of course, sometimes she will clean the vases. Because there are too many vases, she randomly choose to clean the vases numbered from A to B(A <= B). The flowers in the cleaned vases will be discarded.
 

Input
  The first line contains an integer T, indicating the number of test cases.
  For each test case, the first line contains two integers N(1 < N < 50001) and M(1 < M < 50001). N is the number of vases, and M is the operations of Alice. Each of the next M lines contains three integers. The first integer of one line is K(1 or 2). If K is 1, then two integers A and F follow. It means Alice receive F flowers and try to put a flower in the vase A first. If K is 2, then two integers A and B follow. It means the owner would like to clean the vases numbered from A to B(A <= B).
 

Output
  For each operation of which K is 1, output the position of the vase in which Alice put the first flower and last one, separated by a blank. If she can not put any one, then output 'Can not put any one.'. For each operation of which K is 2, output the number of discarded flowers.
   Output one blank line after each test case.
 

Sample Input
 
  
2 10 5 1 3 5 2 4 5 1 1 8 2 3 6 1 8 8 10 6 1 2 5 2 3 4 1 0 8 2 2 5 1 4 4 1 2 3
 

Sample Output
 
  
[pre]3 7 2 1 9 4 Can not put any one. 2 6 2 0 9 4 4 5 2 3 [/pre]
#include<bits/stdc++.h>
using namespace std;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r

const int MAX = 5e4 + 7;
int a[MAX << 2];
int n;

void pushup(int rt){
    a[rt] = a[rt << 1 | 1] + a[rt << 1];
}
void pushdown(int rt, int l, int r){
    if(a[rt] == r - l + 1){
        int mid = (l + r) >> 1;
        a[rt << 1] = mid - l + 1;
        a[rt << 1 | 1] = r - mid;
    }
    else if(a[rt] == 0)
        a[rt << 1] = a[rt << 1 | 1] = 0;
}

void update(int rt, int l, int r, int x, int y, int v){
    if(x <= l && r <= y){
        a[rt] = v * (r - l + 1);
        return;
    }
    if(l == r)
        return;
    pushdown(rt, l, r);
    int mid = (l + r) >> 1;
    if(x <= mid)
        update(lson, x, y, v);
    if(mid < y)
        update(rson, x, y, v);
    pushup(rt);
}
int query(int rt, int l, int r, int x, int y){
    if(x <= l && r <= y)
        return a[rt];
    int mid = (l + r) >> 1;
    int ans = 0;
    pushdown(rt, l, r);
    if(x <= mid)
        ans += query(lson, x, y);
    if(mid < y)
        ans += query(rson, x, y);
    return ans;
}
int findl(int rt, int l, int r, int x, int y){
    if(l == r)
        return l;
    int mid = (l + r) >> 1;
    if(x <= mid && query(1, 0, n - 1, x, mid) != mid - x + 1)
        return findl(lson, x, y);
    else
        return findl(rson, x, y);
}
int findr(int rt, int l, int r, int x, int y){
    if(l == r)
        return l;
    int mid = (l + r) >> 1;
    if(mid < y && query(1, 0, n - 1, mid + 1, y) != y - mid)
        return findr(rson, x, y);
    else
        return findr(lson, x, y);
}
int main(){
    int N;
    scanf("%d", &N);
    while(N--){
        int m;
        scanf("%d%d", &n, &m);
        int s, x, y;
        memset(a, 0, sizeof a);
        for(int i = 0; i < m; i++){
            scanf("%d%d%d", &s, &x, &y);
            if(s == 1){
                if(query(1, 0, n - 1, x, n - 1) == n - x){
                    puts("Can not put any one.");
                    continue;
                }else{
                    int l = x;
                    int r = n - 1;
                    int mid;
                    while(r > l){
                        mid = (l + r) >> 1;
                        if(mid - x + 1 - query(1, 0, n - 1, x, mid) >= y)
                            r = mid;
                        else
                            l = mid + 1;
                    }
                    int L = findl(1, 0, n - 1, x, r);
                    int R = findr(1, 0, n - 1, x, r);
                    printf("%d %d\n", L, R);
                    update(1, 0, n - 1, L, R, 1);
                }
            }
            else{
                printf("%d\n", query(1, 0, n - 1, x, y));
                update(1, 0, n - 1, x, y, 0);
            }
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Head_Hard/article/details/80724165