HDU 4614 Vases and Flowers(线段树区间更新+二分)

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

3 7
2
1 9
4
Can not put any one.

2 6
2
0 9
4
4 5
2 3

线段树区间更新题目:

操作一:(A,F),从A花瓶开始插花,可以不连续,将F朵花插到花瓶中。给出初末位置。

操作二:(L,R),将区间中的花清除。计算区间中的花的朵数。

操作一,由于是不连续,所以我们考虑两次二分找出初末位置来做。
<pre name="code" class="cpp">#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int maxn=50001+100;
int col[maxn<<2],sum[maxn<<2];//lazy标记,col[i]=1表示空位,col[i]=0表示无空位,col[i]==-1表示全是空位
int t,n,m;
void pushup(int rs)
{
    sum[rs]=sum[rs<<1]+sum[rs<<1|1];
}
void pushdown(int rs,int s)
{
    if(col[rs]==0)
    {
        col[rs<<1]=col[rs<<1|1]=0;
        sum[rs<<1]=sum[rs<<1|1]=0;
        col[rs]=-1;
    }
    else if(col[rs]==1)
    {
        col[rs<<1]=col[rs<<1|1]=1;
        sum[rs<<1]=(s-(s>>1));
        sum[rs<<1|1]=(s>>1);
        col[rs]=-1;
    }
}
void build(int l,int r,int rs)
{
    col[rs]=-1;
    sum[rs]=1;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    build(l,mid,rs<<1);
    build(mid+1,r,rs<<1|1);
    pushup(rs);
}
void update(int x,int y,int c,int l,int r,int rs)
{
    if(l>=x&&r<=y)
    {
        col[rs]=c;
        if(c) sum[rs]=r-l+1;//为1表示空
        else  sum[rs]=0;
        return ;
    }
    int mid=(l+r)>>1;
    pushdown(rs,r-l+1);
    if(x<=mid)  update(x,y,c,l,mid,rs<<1);
    if(y>mid)  update(x,y,c,mid+1,r,rs<<1|1);
    pushup(rs);
}
int query(int x,int y,int l,int r,int rs)
{
    if(l>=x&&r<=y)
       return sum[rs];
    int mid=(l+r)>>1;
    pushdown(rs,r-l+1);
    int res=0;
    if(x<=mid)  res+=query(x,y,l,mid,rs<<1);
    if(y>mid)  res+=query(x,y,mid+1,r,rs<<1|1);
    pushup(rs);
    return res;
}
int main()
{
    int op,l,r;
    scanf("%d",&t);
    while(t--)
    {
       scanf("%d%d",&n,&m);
       build(0,n-1,1);
       while(m--)
       {
           scanf("%d%d%d",&op,&l,&r);//A--F
           if(op==1)
           {
               int ans=query(l,n-1,0,n-1,1);
               if(ans==0)//若没有位置了,输出。。
                 puts("Can not put any one.");
               else
               {
                   if(ans<r)  r=ans;
                   int ll=l,rr=n-1;//二分初末位置
                   while(ll<=rr)
                   {
                       int mid=(ll+rr)>>1;
                       if(query(l,mid,0,n-1,1)>=1) rr=mid-1;
                       else  ll=mid+1;
                   }
                   int a=ll;
                   ll=l;rr=n-1;
                   while(ll<=rr)
                   {
                       int mid=(ll+rr)>>1;
                       if(query(l,mid,0,n-1,1)>=r) rr=mid-1;
                       else  ll=mid+1;
                   }
                   int b=ll;
                   update(a,b,0,0,n-1,1);
                   printf("%d %d\n",a,b);
               }
           }
           else
           {
               printf("%d\n",r-l+1-query(l,r,0,n-1,1));
               update(l,r,1,0,n-1,1);
           }
       }
       puts("");
    }
    return 0;
}
				
时间: 2024-10-21 01:03:14

HDU 4614 Vases and Flowers(线段树区间更新+二分)的相关文章

HDU 4614 Vases and Flowers 线段树+二分

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意:N个花瓶,两种操作. 操作1:从第a个花瓶开始放花,放最多f个,如果花瓶已经有花就跳过,直到放完,或者无更多花瓶,要求输出这次放花的第一个位置和最后一个位置,如果没放就输出Cannot... 操作2:将花瓶a到b区间内的花都扔了,然后输出扔掉的花的数目. 解题思路:花瓶有花为0,无花为1,那么实际上这是一个区间更新以及求和,求左右端点的问题.线段树节点维护一个 sum--区间和,lid-

HDU 4902 Nice boat(线段树 区间更新)

Nice boat 大意:给你一个区间,每次可以进行两种操作,1:把区间中的数全都变成x  2:把区间中大于x的数变成gcd(a[i], x),最后输出序列. 思路:线段树成段更行,用num数组的叶子存储数据,节点当作lazy来使用. 1 #include <stdio.h> 2 const int maxn = 100005; 3 4 int num[maxn<<2]; 5 6 int gcd(int a, int b){ 7 return b?gcd(b, a%b):a; 8

HDU 1698 Just a Hook (线段树,区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17214    Accepted Submission(s): 8600 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f

hdu 6444 Neko&#39;s loop 线段树区间更新

题目连接:Neko's loop 题意:给一个长度为n的环,下标从0~n-1,环上每个点有个值表示到这个点会得到的快乐值.,然后每次可以花费1能量往后跳k步.你可以选择任意点开始跳,可以任意点结束,最多跳m次问得到至少s的快乐值最初要拥有多少. 题解:先把循环节挑出来,,然后在循环节上找最大字段和.循环节长度为cnt,然后就是枚举起点用线段树维护前缀和,然后取最大值. #include<bits/stdc++.h> #define ls l,m,rt<<1 #define rs m

HDU 1698 Just a Hook (线段树 区间更新基础)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21856    Accepted Submission(s): 10963 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing

HDU 1689 Just a Hook 线段树区间更新求和

点击打开链接 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18894    Accepted Submission(s): 9483 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible

(简单) HDU 1698 Just a Hook , 线段树+区间更新。

Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. Now Pudge wants to do some operations on the hook

hdu 1698 Just a Hook 线段树区间更新

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks

hdu - 1689 Just a Hook (线段树区间更新)

http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<=z<=3),问更新完后的总价值. 线段树的区间更新,需要用到延迟标记,简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新或者询问的时候. 这题只需要输出总区间的信息,即直接输出1结点的信息. 1 #include <iostream> 2 #include <