Descrption
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个花瓶,开始都为空,有两种操作,第一种操作从pos位置开始放k朵花,只放空花瓶,直到放完k朵花或到了第n-1个花瓶,输出第一个空瓶和最后一个空花瓶的位置,第二种操作是清空qL到qR花瓶的花,并输出qL到qR有多少花。
当然,这题也是用线段树来做,放花和清空,可以看作是区间赋值1和区间赋值0,查询qL到qR有多少花,即区间查询sum,这题关键是如何找到第一个空花瓶和最后一个空花瓶的位置,我们注意到,空花瓶的个数是一个单调序列,所以我们可以二分找到位置,好吧都是套路。。
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define CLR(a) memset(a, 0, sizeof(a))
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, a, b) for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1000000009;
const int N= 5e4 +10;
int T[N*4], sum[N*4];
int t, n, m;
int k, a, b;
int qL, qR, x, ans, first, last;
void Update(int o, int L, int R) {
sum[o]=sum[o*2]+sum[o*2+1];
}
void Down(int o, int L, int R) {
T[o*2]=T[o*2+1]=T[o];
T[o]=-1;
int M=L+(R-L)/2;
sum[o*2]=(M-L+1)*T[o*2];
sum[o*2+1]=(R-M)*T[o*2+1];
}
void Set(int o, int L, int R) {
if (qL<=L&&R<=qR) {
T[o]=x;
sum[o]=(R-L+1)*x;
return;
}
if (T[o]>=0) Down(o, L, R);
int M=L+(R-L)/2;
if (qL<=M) Set(o*2, L, M);
if (qR>M) Set(o*2+1, M+1, R);
Update(o, L, R);
}
void Query(int o, int L, int R) {
if (qL<=L&&R<=qR) {
ans+=sum[o];
return;
}
if (T[o]>=0) Down(o, L, R);
int M=L+(R-L)/2;
if (qL<=M) Query(o*2, L, M);
if (qR>M) Query(o*2+1, M+1, R);
}
int find(int pos, int x) {
int L=pos, R=n;
//注意全局变量的值
qL=pos;
while(L<R) {
qR=L+(R-L)/2;
ans=0;
Query(1, 0, n-1);
ans=(qR-qL+1)-ans;
if (ans<x) {
L=qR+1;
}
else if (ans>=x) {
R=qR;
}
}
return L;
}
int main() {
cin>>t;
while(t--) {
cin>>n>>m;
memset(sum, 0, sizeof(sum));
memset(T, -1, sizeof(T));
REP(i, 1, m) {
scanf("%d %d %d", &k, &a, &b);
if (k==1) {
first=find(a, 1);
if (first==n) {
printf("Can not put any one.\n");
}
else {
//末尾0的个数
qL=first;
qR=n-1;
ans=0;
Query(1, 0, n-1);
ans=(qR-qL+1)-ans;
//最右边空花瓶位置
last=find(a, min(ans, b));
/* if (ans>=b) {
last=find(qL, b);
}
else last=find(qL, ans);
*/
qL=first;
qR=last;
x=1;
Set(1, 0, n-1);
printf("%d %d\n", qL, qR);
}
}
else if (k==2) {
qL=a; qR=b;
x=0;
ans=0;
Query(1, 0, n-1);
printf("%d\n", ans);
Set(1, 0, n-1);
}
}
cout<<endl;
}
//cout<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";
return 0;
}
教练,我想打acm。。