题目大意:有N个花瓶,刚开始每个花瓶都是空的,每个花瓶只能放一束花,现在有两种操作
1 x y:收到了y束花,要求从花瓶x开始放过去,如果花瓶不够,多的花直接丢弃,输出放的花瓶的起始和终点,如果一个花瓶也没有,另外输出
2 x y:[x,y]内的花瓶清空,输出清空了几个花瓶
解题思路:维护区间的空的花瓶的数量,第二个操作就比较简单了,现在讨论第一个操作
首先判断一下,区间是否有花瓶(特殊情况),如果没有,直接另外输出
接着判断一下空的花瓶的数量和收到的花的数量,取最小值
最后,二分查找第一个空的花瓶所在的位置和最后一个花瓶所在的位置,找到这两个位置后,Modify这两个位置内的所有花瓶即可
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 50010 << 2;
int setv[N], sum[N];
int n, m;
void PushUp(int u) {
sum[u] = sum[u << 1] + sum[u << 1 | 1];
}
void PushDown(int u, int l, int r) {
setv[u << 1] = setv[u << 1 | 1] = setv[u];
int mid = (l + r) >> 1;
sum[u << 1] = (mid - l + 1) * setv[u];
sum[u << 1 | 1] = (r - mid) * setv[u];
setv[u] = -1;
}
void build(int u, int l, int r) {
setv[u] = -1;
if (l == r) {
sum[u] = 1;
return ;
}
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
PushUp(u);
}
void Modify(int u, int l, int r, int L, int R, int a) {
if (l == L && r == R) {
sum[u] = (r - l + 1) * a;
setv[u] = a;
return ;
}
int mid = (l + r) >> 1;
if (~setv[u]) PushDown(u, l, r);
if (R <= mid) Modify(u << 1, l, mid, L, R, a);
else if (L > mid) Modify(u << 1 | 1, mid + 1, r, L, R, a);
else {
Modify(u << 1, l, mid, L, mid, a);
Modify(u << 1 | 1, mid + 1, r, mid + 1, R, a);
}
PushUp(u);
}
int Query(int u, int l, int r, int L, int R) {
if (l == L && r == R) return sum[u];
int mid = (l + r) >> 1;
if (~setv[u]) PushDown(u, l, r);
if (R <= mid) return Query(u << 1, l, mid, L, R);
else if (L > mid) return Query(u << 1 | 1, mid + 1, r, L, R);
else return Query(u << 1, l, mid, L, mid) + Query(u << 1 | 1, mid + 1, r, mid + 1, R);
}
int findLeft(int l) {
int r = n;
while (l < r) {
int mid = (l + r) >> 1;
if (Query(1, 1, n, l, mid) >= 1) r = mid;
else l = mid + 1;
}
return r;
}
int findRight(int l, int num) {
int t = l, r = n;
while (t < r) {
int mid = (r + t) >> 1;
if (Query(1, 1, n, l, mid) >= num) r = mid;
else t = mid + 1;
}
return r;
}
void solve() {
scanf("%d%d", &n, &m);
build(1, 1, n);
int op, a, b;
while (m--) {
scanf("%d%d%d", &op, &a, &b);
if (op == 1) {
a++;
int left = Query(1, 1, n, a, n);
if (left == 0) {
printf("Can not put any one.\n");
continue;
}
int l = findLeft(a);
int r = findRight(l, min(left, b));
Modify(1, 1, n, l, r, 0);
printf("%d %d\n", l - 1, r - 1);
}
else {
a++, b++;
printf("%d\n", (b - a + 1) - Query(1, 1, n, a, b));
Modify(1, 1, n, a, b, 1);
}
}
printf("\n");
}
int main() {
int test;
scanf("%d", &test);
while (test--) solve();
return 0;
}