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): 904    Accepted Submission(s): 332


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]
 

Source
2013 Multi-University Training Contest 2
 

Recommend
zhuyuanchen520
 

题意: 有N个花盆,下标为(0, N - 1)  每个花盆只能种一朵花。  然后有M个操作, a,  b, c;  如果 a == 1 表示从下标为a的花盆开始种c朵花。
已经种了的, 就跳过。
 如果一直到最后都没种上一朵花 则输出“ Can not put any one.”,
 否则输出插入的区间, 即 L = 第一朵花种的花盆的下标, R = 最后一朵花种的花盆的下标(注意: 可以不插完, 但最少插一朵, 否则就上面的情况);

思路: 区间更新线段树(赋值型)
#include 
#include 
#include 
#include 
using namespace std;

const int V = 200000 + 5;
const int MaxN = 100000 + 5;
const int mod = 1000000000 + 7;
const int v = 50000 + 50;
typedef struct node{
    int l, r, sum, add; //sum: 区间和 add:该区间是否清空  -1不更新, 0该区间清零, 1为该区间填满
}node;
node nod[v * 3];
int N, T, m;
void build(int root, int st, int ed) {
    nod[root].l = st;
    nod[root].r = ed;
    nod[root].sum = 0;
    nod[root].add = -1;
    if(ed != st) {
        int mid = (st + ed) / 2;
        build(root * 2, st, mid);
        build(root * 2 + 1, mid + 1, ed);
    }
}
void Update(int root, int l, int r, int add) {
    if(nod[root].l == l && nod[root].r == r) {
        nod[root].add = add;
        nod[root].sum = (r - l + 1) * add;
        return ;
    }
    int mid = (nod[root].l + nod[root].r) / 2;
    if(r <= mid)
        Update(root * 2, l, r, add);
    else if(l > mid)
        Update(root * 2 + 1, l, r, add);
    else {
        Update(root * 2, l, mid, add);
        Update(root * 2 + 1, mid + 1, r, add);
    }
    nod[root].sum = nod[root * 2].sum + nod[root * 2 + 1].sum;
}
int Query(int root, int l, int r) {
    if(nod[root].l == l && nod[root].r == r)
        return nod[root].sum;
    if(nod[root].add >= 0) { //有更新操作,传给儿子
        int ls = root * 2, rs = root * 2 + 1;
        nod[ls].sum = (nod[ls].r - nod[ls].l + 1) * nod[root].add;
        nod[rs].sum = (nod[rs].r - nod[rs].l + 1) * nod[root].add;
        nod[ls].add = nod[root].add;
        nod[rs].add = nod[root].add;
        nod[root].add = -1;
    }
    int ans = 0;
    int mid = (nod[root].l + nod[root].r) / 2;
    if(r <= mid)
        ans = Query(root * 2, l, r);
    else if(l > mid)
        ans = Query(root * 2 + 1, l, r);
    else {
        ans = Query(root * 2, l, mid);
        ans += Query(root * 2 + 1, mid + 1, r);
    }
    return ans;
}
int bin_ser(int l, int r, int c) {
    int ll = l;
    while(l < r) {
        int mid = (l + r) / 2;
        int temp_sum = Query(1, ll, mid);
        if(mid - ll + 1 >= temp_sum + c)
            r = mid;
        else
            l = mid + 1;
    }
    return l;
}
int main() {
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &N, &m);
        N--;
        build(1, 0, N);
        while(m--) {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            if(a == 1) {
                int right_sum = Query(1, b, N);
                if(right_sum == N - b + 1) {
                    cout << "Can not put any one." << endl;
                    continue;
                }
                int left_sum = b == 0 ? 0 : Query(1, 0, b - 1);
                int left_index = bin_ser(0, N, b - left_sum + 1);
                int right_index = bin_ser(b, N, min(N - b + 1 - right_sum, c));
                printf("%d %d\n", left_index, right_index);
                Update(1, left_index, right_index, 1);
            }
            else {
                printf("%d\n", Query(1, b, c));
                Update(1, b, c, 0);
            }
        }
        printf("\n");
    }
}


你可能感兴趣的:(线段树)