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

57 篇文章 0 订阅
26 篇文章 0 订阅

题目链接:

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<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{
    int ret=0, flag=0;
    char ch;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='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<y)//花瓶数不够
                    y=num;
                int l=x,r=n-1;
                while(l<=r)
                {
                    int mid=(l+r)>>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;
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值