HDU 4614 Vases and Flowers (2013多校2 1004 线段树)

Vases and Flowers

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

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  

 

 

 

 

很裸的线段树。

但是写起来很麻烦。

1~N 的区间,用1表示空的,0表示放了花的。

维护一个sum,就是和

first : 区间最左边的1

last: 区间最右边的1

 

然后一个更新操作,一个求区间和操作。

 

查询区间第一个1,和最后一个1.

 

二分确定区间。

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <math.h>
using namespace std;
const int MAXN = 50010;
struct Node
{
    int l,r;
    int sum;
    int lazy;
    int first;
    int last;
}segTree[MAXN*3];
void push_up(int i)
{
    if(segTree[i].l==segTree[i].r)return;
    segTree[i].sum = segTree[i<<1].sum+segTree[(i<<1)|1].sum;
    if(segTree[i<<1].first != -1)segTree[i].first = segTree[i<<1].first;
    else segTree[i].first = segTree[(i<<1)|1].first;
    if(segTree[(i<<1)|1].last != -1)segTree[i].last = segTree[(i<<1)|1].last;
    else segTree[i].last = segTree[(i<<1)].last;
}
void push_down(int i)
{
    if(segTree[i].r == segTree[i].l)return;
    if(segTree[i].lazy==1)
    {
        segTree[i<<1].first = segTree[i<<1].l;
        segTree[i<<1].last = segTree[i<<1].r;
        segTree[i<<1].sum = segTree[i<<1].r-segTree[i<<1].l+1;
        segTree[i<<1].lazy=1;
        segTree[(i<<1)|1].first = segTree[(i<<1)|1].l;
        segTree[(i<<1)|1].last = segTree[(i<<1)|1].r;
        segTree[(i<<1)|1].sum = segTree[(i<<1)|1].r-segTree[(i<<1)|1].l+1;
        segTree[(i<<1)|1].lazy=1;
    }
    if(segTree[i].lazy == -1)
    {
        segTree[i<<1].first = -1;
        segTree[i<<1].last = -1;
        segTree[i<<1].sum = 0;
        segTree[i<<1].lazy=-1;
        segTree[(i<<1)|1].first = -1;
        segTree[(i<<1)|1].last = -1;
        segTree[(i<<1)|1].sum = 0;
        segTree[(i<<1)|1].lazy=-1;
    }
    segTree[i].lazy = 0;
}
void build(int i,int l,int r)
{
    segTree[i].l = l;
    segTree[i].r = r;
    segTree[i].sum = r-l+1;
    segTree[i].lazy = 0;
    segTree[i].first = l;
    segTree[i].last = r;
    if(l==r)return ;
    int mid = (l+r)/2;
    build(i<<1,l,mid);
    build((i<<1)|1,mid+1,r);
}
void update(int i,int l,int r,int type)
{
    if(segTree[i].l == l && segTree[i].r==r)
    {
        if(type == 0)
        {
            if(segTree[i].sum == 0)return;
            segTree[i].sum = 0;
            segTree[i].lazy = -1;
            segTree[i].first = -1;
            segTree[i].last = -1;
            return;
        }
        else if(type == 1)
        {
            if(segTree[i].sum == segTree[i].r-segTree[i].l+1)return;
            segTree[i].sum = segTree[i].r-segTree[i].l+1;
            segTree[i].lazy = 1;
            segTree[i].first = segTree[i].l;
            segTree[i].last = segTree[i].r;
            return;
        }

    }
    push_down(i);
    int mid = (segTree[i].l + segTree[i].r)/2;
    if(r <= mid)update(i<<1,l,r,type);
    else if(l > mid)update((i<<1)|1,l,r,type);
    else
    {
        update(i<<1,l,mid,type);
        update((i<<1)|1,mid+1,r,type);
    }
    push_up(i);
}
int sum(int i,int l,int r)
{
    if(segTree[i].l == l && segTree[i].r == r)
    {
        return segTree[i].sum;
    }
    push_down(i);
    int mid = (segTree[i].l + segTree[i].r)/2;
    if(r <= mid)return sum(i<<1,l,r);
    else if(l > mid)return sum((i<<1)|1,l,r);
    else return sum((i<<1)|1,mid+1,r)+sum(i<<1,l,mid);
}
int n,m;
int query1(int i,int l,int r)
{
    if(segTree[i].l == l && segTree[i].r == r)
    {
        return segTree[i].first;
    }
    push_down(i);
    int mid = (segTree[i].l + segTree[i].r)/2;
    int ans1,ans2;
    if(r <= mid)return query1(i<<1,l,r);
    else if(l > mid)return query1((i<<1)|1,l,r);
    else
    {
        ans1 = query1(i<<1,l,mid);
        if(ans1 != -1)return ans1;
        return query1((i<<1)|1,mid+1,r);
    }
}
int query2(int i,int l,int r)
{
    if(segTree[i].l == l && segTree[i].r == r)
    {
        return segTree[i].last;
    }
    push_down(i);
    int mid = (segTree[i].l + segTree[i].r)/2;
    int ans1,ans2;
    if(r <= mid)return query2(i<<1,l,r);
    else if(l > mid)return query2((i<<1)|1,l,r);
    else
    {
        ans1 = query2((i<<1)|1,mid+1,r);
        if(ans1 != -1)return ans1;
        return query2(i<<1,l,mid);
    }
}
int bisearch(int A,int F)
{
    if(sum(1,A,n)==0)return -1;
    if(sum(1,A,n)<F)return n;
    int l= A,r = n;
    int ans=A;
    while(l<=r)
    {
        int mid = (l+r)/2;
        if(sum(1,A,mid)>=F)
        {
            ans = mid;
            r = mid-1;
        }
        else l = mid+1;
    }
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        build(1,1,n);
        int op,u,v;
        while(m--)
        {
            scanf("%d%d%d",&op,&u,&v);
            if(op == 1)
            {
                u++;
                int t = bisearch(u,v);
                //printf("t:%d\n",t);
                if(t==-1)
                {
                    printf("Can not put any one.\n");
                    continue;
                }
                printf("%d %d\n",query1(1,u,t)-1,query2(1,u,t)-1);
                update(1,u,t,0);
            }
            else
            {
                u++;v++;
                //printf("sum:%d\n",sum(1,u,v));
                printf("%d\n",v-u+1-sum(1,u,v));
                update(1,u,v,1);
            }
        }
        printf("\n");
    }
    return 0;
}

 

 

 

 

 

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3214805.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞