hdu4614 线段树区间覆盖和区间查询

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4614

Vases and Flowers

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


 

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]

 

 

Author

SYSU

 

 

Source

2013 Multi-University Training Contest 2

 

 

Recommend

zhuyuanchen520

 

 

Statistic | Submit | Discuss | Note

题目大意:

n个花瓶,m个操作,花瓶里面有的有花,有的是空的。初始全部是空的

1操作是从a开始往右放b朵花,花瓶有了的不放,跳过,直到a右边都放满了花,多余的扔了。输出本次放花的起始位置,如果一朵不能放,输出一句话。

2操作是清除区间[a,b]的花。并输出清除了多少花。

思路:

线段树,1表示没有花,0表示有花,区间覆盖更新,和区间求和

二分查找起始插花的地点和最终插花的地点

This is the code

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define PI acos(-1.0)
#define pppp cout<='0'&&ch<='9')
        ret = ch - '0';
    while((ch=getchar())>='0'&&ch<='9')
        ret=ret*10+(ch-'0');
    return flag ? -ret : ret;
}
const int MAXN=50005;
int sum[MAXN<<2];
int up[MAXN<<2];

void push_up(int i)
{
    sum[i]=sum[i<<1]+sum[i<<1|1];
}

void push_down(int i,int l,int r)
{
    if(up[i]!=-1)
    {
        int mid=(l+r)>>1;
        sum[i<<1]=up[i]*(mid-l+1);
        sum[i<<1|1]=up[i]*(r-mid);
        up[i<<1]=up[i];
        up[i<<1|1]=up[i];
        up[i]=-1;
    }
}

void build(int i,int l,int r)
{
    up[i]=-1;
    if(l==r)
    {
        sum[i]=1;
        return ;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    push_up(i);
}

void update(int i,int l,int r,int x,int y,int val)
{
    if(x<=l&&r<=y)
    {
        sum[i]=val*(r-l+1);
        up[i]=val;
        return ;
    }
    push_down(i,l,r);
    int mid=(l+r)>>1;
    if(x<=mid)
        update(i<<1,l,mid,x,y,val);
    if(y>mid)
        update(i<<1|1,mid+1,r,x,y,val);
    push_up(i);
}

int query(int i,int l,int r,int x,int y)//本题中不需要使用这个函数
{
    if(x<=l&&r<=y)
        return sum[i];
    push_down(i,l,r);
    int mid=(l+r)>>1;
    int sum=0;
    if(x<=mid)
        sum+=query(i<<1,l,mid,x,y);
    if(y>mid)
        sum+=query(i<<1|1,mid+1,r,x,y);
    return sum;
}
int main()
{
    //freopen("D:\\chnegxubianji\\inORout\\in.txt", "r", stdin);
    //freopen("D:\\chnegxubianji\\inORout\\out.txt", "w", stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        build(1,0,n-1);//建树
        while(m--)
        {
            int tem,x,y;
            scanf("%d%d%d",&tem,&x,&y);
            if(tem==1)
            {
                int num=query(1,0,n-1,x,n-1);
                if(num==0)
                {
                    printf("Can not put any one.\n");
                    continue;
                }
                if(num>1;
                    if(query(1,0,n-1,x,mid)>=1)
                        r=mid-1;
                    else
                        l=mid+1;
                }
                int ansl=l;
                l=x,r=n-1;
                while(l<=r)
                {
                    int mid=(l+r)>>1;
                    if(query(1,0,n-1,x,mid)>=y)
                        r=mid-1;
                    else
                        l=mid+1;
                }
                int ansr=l;
                printf("%d %d\n",ansl,ansr);
                update(1,0,n-1,ansl,ansr,0);
            }
            else
            {
                int num=query(1,0,n-1,x,y);
                printf("%d\n",y-x+1-num);
                update(1,0,n-1,x,y,1);
            }
        }
        printf("\n");
    }
    return 0;
}

 

 

你可能感兴趣的:(数据结构,寒假集训,模板)