HDU4614--线段树+二分

题目:Vases and Flowers

  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]

题意:1 a b 表示从a开始插b个花,如果某一位已经占了就考虑下一位,这样依次检查,直到所有的花已经插完,或者所有的瓶子已经遍历完。

2 a b 表示清空  a ~ b 的花的总数,并输出清理的数目。

分析:

线段树可以提供区间更新,区间查询,修改,但是现在只有待修改区间的起点,不知道终点,因此是用二分的方法,找到终点。

写了两天还是有bug,参考了博客:传送门

#include 
#include 
#include 
#include 
using namespace std;

const int maxn = 50005;
struct node
{
    int l,r;
    int lazy;
    int sum;
}tree[maxn << 2];
int a[maxn << 1],n;

void pushup(int p)
{
    tree[p].sum = tree[p << 1].sum + tree[p << 1|1].sum;
}

void pushdown(int p)//向下传递信息
{
    tree[p << 1].lazy = tree[p << 1|1].lazy = tree[p].lazy;
    tree[p << 1].sum = tree[p].lazy * (tree[p << 1].r - tree[p << 1].l + 1);
    tree[p << 1|1].sum = tree[p].lazy * (tree[p << 1|1].r - tree[p << 1|1].l + 1);
    tree[p].lazy = -1;
}

void build(int p,int l,int r)
{
    tree[p].l = l;
    tree[p].r = r;
    tree[p].lazy = -1;
    tree[p].sum = 0;
    if(l == r)return ;
    int m = (l + r) >> 1;
    build(p << 1,l,m);
    build(p << 1|1, m + 1,r);
}

void update(int p,int x,int y,int c)
{
    if(x == tree[p].l && tree[p].r == y)
    {
        tree[p].lazy = c;
        tree[p].sum = c * (y - x + 1);//c = 1,插满区间;c = 0,清空区间
        return ;
    }
    if(tree[p].lazy != -1)
        pushdown(p);
    int m = (tree[p].l + tree[p].r ) >> 1;
    if(x > m) update(p << 1|1,x,y,c);
    else if(y <= m) update(p << 1,x,y,c);
    else {
        update(p << 1,x,m,c);
        update(p << 1|1,m + 1,y,c);
    }
    pushup(p);
}

int query(int p,int l,int r)
{
    if(l == tree[p].l && tree[p].r == r)
        return tree[p].sum;
    if(tree[p].lazy != -1)
        pushdown(p);
    int m = (tree[p].l + tree[p].r) >> 1;
    if(r <= m) return query(p << 1,l,r);
    else if(l >= m + 1) return query(p << 1|1,l,r);
    else return (query(p << 1,l,m) + query(p << 1|1,m + 1,r));
    pushup(p);
}

int binary(int s,int k)//找到要插的一个空位
{
    int tmp = query(1,s,n);
    if(tmp == (n - s + 1))//插满,返回-1;
        return -1;
    if(k > (n - s + 1 - tmp))//插的总数小于已有得总数更新
        k = n - s + 1 - tmp;
    int l = s;
    int r = n;
    int ans = -1;
    while(l <= r)
    {
        int m = (l + r) >> 1;
        int tmm =m - s + 1 - query(1,s,m);//n ~ s 的空位
        if(tmm < k)
            l = m + 1;
        else if(tmm > k)//空位多
            r = m - 1;
        else {
            ans = m;//保存位置。
            r  = m - 1;
        }
    }
    return ans;
}

int main()
{
    int t,m,k,a,b;
    scanf("%d",&t);
    while(t --)
    {
        scanf("%d%d",&n,&m);
        n -= 1;
        build(1,0,n);
        while(m --)
        {
            scanf("%d%d%d",&k,&a,&b);
            if(k == 1)
            {
                int tmp = binary(a,1);
                if(tmp == -1)
                    printf("Can not put any one.\n");
                else {
                    int ans = binary(a,b);
                    printf("%d %d\n",tmp,ans);
                    update(1,tmp,ans,1);
                }
            }
            else {
                    printf("%d\n",query(1,a,b));
                    update(1,a,b,0);
            }
        }
        puts("");
    }
    return 0;
}

 

你可能感兴趣的:(二分,数据结构-线段树)