Vases and Flowers
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
题意:已知有 n 个花瓶,每个花瓶只能装一枝花,现在有两种操作,第一种,给定 A 和 F ,表示从第A个花瓶开始依次往后放 F朵花,放不完的花扔掉,输出放的第一朵花的位置和最后一朵花的位置,如果一朵花都没能放输出 Can not put any one. 。第二种,给定A 和 B,清空第 A 个花瓶到第 B 个花瓶中的所有话,输出清除的数量。
思路:对于清空操作很简单,就是查询区间和。
那么对于插花的操作,我们可以这样解决:插一朵花标记为1,那么当区间和 sum[i] = 区间长度时,就表示全部插了花 直接 return (剪枝), sum[i] = 0 时表示一段花瓶都没插花,由于插的花按顺序的,所以我们还需要一个参数,当前未插的朵数来控制一下,当未插完的数量 >= 区间长度时,令 sum[i] = 区间长度,然后更新一下未插的朵数和答案。否则就继续往下递归即可。。具体update可看代码
AC代码:
#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)
using namespace std;
const int maxn = 5e4 + 10;
int sum[maxn << 2],lazy[maxn << 2];
void push_down(int l,int r,int i){
if(lazy[i] != -1){
int mid = (l + r) >> 1;
sum[i << 1] = lazy[i] * (mid - l + 1);
sum[i << 1 | 1] = lazy[i] * (r - mid);
lazy[i << 1] = lazy[i << 1 | 1] = lazy[i];
lazy[i] = -1;
}
}
int minn = maxn,maxx = -1;
void update(int l,int r,int ul,int ur,int i,int &f){
if(!f) return ;
// printf("%d %d\n",l,r);
if(ul <= l && r <= ur){
// printf("%d %d f:%d\n",l,r,f);
// debug(f >= (r - l + 1));
// debug(sum[i]);
if(sum[i] == 0 && f >= (r - l + 1)){///当未插的话的数量大于这一段区间长度,直接插完就可
sum[i] = (r - l + 1);
minn = min(minn,l);
maxx = max(maxx,r);
f -= r - l + 1;
lazy[i] = 1;
return ;
}
}
if(sum[i] == r - l + 1) return ; ///这个区间段全都插了花,剪枝
if(l == r) return ; ///极端时的递归出口
push_down(l,r,i);
int mid = (l + r) >> 1;
if(ul <= mid) update(l,mid,ul,ur,i << 1,f);
if(ur > mid) update(mid + 1,r,ul,ur,i << 1 | 1,f);
sum[i] = sum[i << 1] + sum[i << 1 | 1];
}
int query(int l,int r,int ql,int qr,int i){
if(ql <= l && r <= qr){
return sum[i];
}
push_down(l,r,i);
int ans = 0,mid = (l + r) >> 1;
if(ql <= mid) ans += query(l,mid,ql,qr,i << 1);
if(qr > mid) ans += query(mid + 1,r,ql,qr,i << 1 | 1);
return ans;
}
void update2(int l,int r,int ul,int ur,int i){
if(ul <= l && r <= ur){
sum[i] = 0;
lazy[i] = 0;
return ;
}
push_down(l,r,i);
int mid = (l + r) >> 1;
if(ul <= mid) update2(l,mid,ul,ur,i << 1);
if(ur > mid) update2(mid + 1,r,ul,ur,i << 1 | 1);
sum[i] = sum[i << 1] + sum[i << 1 | 1];
}
int main() {
int t; scanf("%d",&t);
while(t --){
int n,m; scanf("%d%d",&n,&m);
clr(sum,0); clr(lazy,-1);
while(m --){
int k,a,b; scanf("%d%d%d",&k,&a,&b);
if(k == 1){
++ a;
minn = maxn;
maxx = -1;
update(1,n,a,n,1,b);
if(minn != maxn) printf("%d %d\n",minn - 1,maxx - 1);
else printf("Can not put any one.\n");
}
else {
++ a; ++ b;
printf("%d\n",query(1,n,a,b,1));
update2(1,n,a,b,1);
}
}
printf("\n");
}
return 0;
}