您的位置:首页 > 其它

HDU4614-Vases and Flowers

2017-02-26 17:09 435 查看


Vases and Flowers

                                                                        Time Limit: 4000/2000 MS (Java/Others)    Memory Limit:
65535/32768 K (Java/Others)

                                                                                                   Total Submission(s): 3268    Accepted Submission(s): 1304


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

 

Author

SYSU

 

Source

2013 Multi-University Training Contest 2

 

Recommend

zhuyuanchen520

 
题意:给n个花瓶插花,花瓶编号为0~n-1,每个花瓶最多只能插一朵,m个操作,1 a b表示从a号花瓶开始插花,插b朵,多余的则扔掉,若一朵插不了则输出“Can not put any one.”,否则就输出放花的起始位置,0 a b表示清除a号到b号花瓶的花,并输出清除了几朵

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

struct node
{
int r,l;
int lazy,sum;
}x[4*50009];

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

void down(int k)
{
if(x[k].lazy!=-1)
{
x[k<<1].lazy=x[k].lazy;
x[k<<1|1].lazy=x[k].lazy;
x[k<<1].sum=(x[k<<1].r-x[k<<1].l+1)*x[k<<1].lazy;
x[k<<1|1].sum=(x[k<<1|1].r-x[k<<1|1].l+1)*x[k<<1|1].lazy;
x[k].lazy=-1;
}
}

void update(int l,int r,int k,int val)
{
if(l<=x[k].l&&r>=x[k].r)
{
x[k].lazy=val;
x[k].sum=(x[k].r-x[k].l+1)*val;
return ;
}
down(k);
int mid=(x[k].l+x[k].r)>>1;
if(r>mid) update(l,r,k<<1|1,val);
if(l<=mid) update(l,r,k<<1,val);
x[k].sum=x[k<<1].sum+x[k<<1|1].sum;
}

int query(int l,int r,int k)
{
if(l<=x[k].l&&r>=x[k].r) return x[k].sum;
down(k);
int mid=(x[k].l+x[k].r)>>1;
int ans=0;
if(r>mid) ans+=query(l,r,k<<1|1);
if(l<=mid) ans+=query(l,r,k<<1);
return ans;
}

int main()
{
int n,m,t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
int a,b,c;
build(1,n,1);
while(m--)
{
scanf("%d %d %d",&a,&b,&c);
if(a==1)
{
int l=b+1,r=n,mid,mi=n,ma=INF;
if(n-l+1-query(l,n,1)==0)
{
printf("Can not put any one.\n");
continue;
}
while(l<=r)
{
mid=(l+r)>>1;
if(mid-(b+1)+1-query(b+1,mid,1)>=1)
{
mi=min(mi,mid);
r=mid-1;
}
else l=mid+1;
}
c=min(c,n-mi+1-query(mi,n,1));
l=mi,r=n;
while(l<=r)
{
mid=(l+r)>>1;
int k=mid-mi+1-query(mi,mid,1) ;
if(k==c)
{
ma=min(ma,mid);
r=mid-1;
}
else if(k>c) r=mid-1;
else l=mid+1;
}
printf("%d %d\n",mi-1,ma-1);
update(mi,ma,1,1);
}
else
{
printf("%d\n",query(b+1,c+1,1));
update(b+1,c+1,1,0);
}
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: