線段樹+二分區間
用線段樹維護某一段內還有多少個花瓶可以用,二分確定插入的左右界.....
Vases and Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1782 Accepted Submission(s): 700
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
#include
#include
#include
#include
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=60050;
int sum[maxn<<2],col[maxn<<2];
int n,m,leftbound,rightbound,F,A,B;
void init()
{
memset(sum,0,sizeof(sum));
memset(col,-1,sizeof(col));
}
void push_up(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=1; return ;
}
int m=(l+r)/2;
build(lson); build(rson);
push_up(rt);
}
void push_down(int len,int rt)
{
if(col[rt]!=-1)
{
col[rt<<1]=col[rt<<1|1]=col[rt];
sum[rt<<1]=(len-len/2)*col[rt];
sum[rt<<1|1]=(len/2)*col[rt];
col[rt]=-1;
}
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
return sum[rt];
push_down(r-l+1,rt);
int m=(l+r)/2;
int ret=0;
if(L<=m) ret+=query(L,R,lson);
if(R>m) ret+=query(L,R,rson);
return ret;
}
void Update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[rt]=c;
sum[rt]=(r-l+1)*c;
return ;
}
push_down(r-l+1,rt);
int m=(l+r)/2;
if(L<=m) Update(L,R,c,lson);
if(R>m) Update(L,R,c,rson);
push_up(rt);
}
void showit(int l,int r,int rt)
{
cout<<"rt: "<