hdoj 4614 Vases and Flowers 【线段树 + 二分】

来源:互联网 发布:mac finder 编辑:程序博客网 时间:2024/05/16 01:32



Vases and Flowers

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


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
210 51 3 52 4 51 1 82 3 61 8 810 61 2 52 3 41 0 82 2 51 4 41 2 3
 

Sample Output
[pre]3 721 94Can not put any one.2 620 944 52 3[/pre]
 



题意:给你一个[0, N-1]的区间,区间的每个位置只能插一朵花。现在有Q次操作

1 x y 表示从第x个位置开始查y多花,若一朵花也插不上输出"Can not put any one.",反之输出插花的最左位置和最右位置。

2 x y 表示查询区间[x, y]有多少朵花,并清空所有的花。


思路:用线段树维护区间最左的空位置first、最右的空位置last、空位置总数sum,只需二分找到插花的最右位置lastp,在[x, lastp]区间上操作就可以了。二分的时候去掉多余的花就可以了。

主要是PushUp的操作,一开始没考虑儿子区间的first和last可能不存在,WA了一次。。。



AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF 0x3f3f3f3f#define eps 1e-8#define MAXN (50000+10)#define MAXM (500000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)using namespace std;struct Tree{    int l, r, len;    int first, last, sum;    int lazy;};Tree tree[MAXN<<2];void PushUp(int o){    if(tree[ll].first != -1)        tree[o].first = tree[ll].first;    else if(tree[ll].last != -1)        tree[o].first = tree[ll].last;    else if(tree[rr].first != -1)        tree[o].first = tree[rr].first;    else        tree[o].first = tree[rr].last;    if(tree[rr].last != -1)        tree[o].last = tree[rr].last;    else if(tree[rr].first != -1)        tree[o].last = tree[rr].first;    else if(tree[ll].last != -1)        tree[o].last = tree[ll].last;    else        tree[o].last = tree[ll].first;    tree[o].sum = tree[ll].sum + tree[rr].sum;}void PushDown(int o){    int mark = tree[o].lazy;    if(mark != -1)    {        tree[ll].lazy = tree[rr].lazy = mark;        tree[ll].first = mark ? tree[ll].l : -1;        tree[ll].last = mark ? tree[ll].r : -1;        tree[rr].first = mark ? tree[rr].l : -1;        tree[rr].last = mark ? tree[rr].r : -1;        tree[ll].sum = tree[o].lazy * tree[ll].len;        tree[rr].sum = tree[o].lazy * tree[rr].len;        tree[o].lazy = -1;    }}void Build(int o, int l, int r){    tree[o].l = l; tree[o].r = r;    tree[o].len = tree[o].sum = r-l+1;    tree[o].first = l; tree[o].last = r;    tree[o].lazy = -1;    if(l == r)        return ;    int mid = (l + r) >> 1;    Build(lson); Build(rson);}void Update(int o, int L, int R, int v){    if(tree[o].l >= L && tree[o].r <= R)    {        tree[o].lazy = v;        tree[o].sum = v * tree[o].len;        if(v == 0)            tree[o].first = tree[o].last = -1;        else            tree[o].first = tree[o].l, tree[o].last = tree[o].r;        return ;    }    PushDown(o);    int mid = (tree[o].l + tree[o].r) >> 1;    if(R <= mid)        Update(ll, L, R, v);    else if(L > mid)        Update(rr, L, R, v);    else    {        Update(ll, L, mid, v);        Update(rr, mid+1, R, v);    }    PushUp(o);}int Query(int o, int L, int R, int op){    if(tree[o].l >= L && tree[o].r <= R)    {        if(op == 0)            return tree[o].sum;        else if(op == 1)            return tree[o].first;        else            return tree[o].last;    }    PushDown(o);    int mid = (tree[o].l + tree[o].r) >> 1;    if(R <= mid)        return Query(ll, L, R, op);    else if(L > mid)        return Query(rr, L, R, op);    else    {        if(op == 0)            return Query(ll, L, mid, op) + Query(rr, mid+1, R, op);        else if(op == 1)            return Query(ll, L, mid, op) != -1 ? Query(ll, L, mid, op) : Query(rr, mid+1, R, op);        else            return Query(rr, mid+1, R, op) != -1 ? Query(rr, mid+1, R, op) : Query(ll, L, mid, op);    }}int Find(int l, int r, int val){    int have = Query(1, l, r, 0);    if(have == 0) return -1;    if(have < val) val = have;    int ans = l; int s = l;    while(r >= l)    {        int mid = (l + r) >> 1;        //printf("%d--%d--%d\n", mid, Query(1, l, mid, 0), val);        if(Query(1, s, mid, 0) >= val)        {            ans = mid;            r = mid-1;        }        else            l = mid+1;    }    return ans;}int main(){    int t; Ri(t);    W(t)    {        int N, Q;        Ri(N); Ri(Q);        Build(1, 1, N);        W(Q)        {            int op, x, y;            Ri(op); Ri(x); Ri(y);            if(op == 1)            {                x++;                int lastp = Find(x, N, y);                //Pi(lastp);                if(lastp == -1)                    printf("Can not put any one.\n");                else                    printf("%d %d\n", Query(1, x, lastp, 1) - 1, Query(1, x, lastp, 2) - 1), Update(1, x, lastp, 0);            }            else            {                x++, y++;                printf("%d\n", (y - x + 1) - Query(1, x, y, 0));                Update(1, x, y, 1);            }        }        printf("\n");    }    return 0;}


0 0
原创粉丝点击