Vases and Flowers
[Link](Problem - 4614 (dingbacode.com))
题意
情人节到了!众所周知,黄队喜欢花花,所以他当然有很多的花瓶,据不完全统计,黄队有N个花瓶(标号为0~ N-1)。当然他也是个魅力四射的男孩子,所以他也自然会在这条收到很多的花花,当他在情人节这天收到花花时时,他会随机的选择一个瓶子A,从它开始遍历A,A+1, A+2, …, N-1号瓶子,遇到空瓶子的话就放一朵花进去,直到花放完或没有瓶子,剩下的花将被残忍的丢弃。有时,他也会清理标号从a到b的花瓶(a <= b).花瓶里的花会被丢弃。任性又帅气的黄队!
题解
考虑用线段树维护,空的位置权值的1,非空的权值为零,就可以维护当前区间有多少空花瓶。然后对于操作一,对线段树的query加一个二分即可每次查当前左端点到中点的空花瓶,因此具有二段性所以可以二分,注意特判没有一个花瓶存在这种特例,操作完修改一下区间就可以。对于操作二,直接区间修改即可。
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
struct Node {
int l, r;
int sum, lazy;
}tr[N << 2];
void pushup(int u) {
tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
}
void down(Node &t, int lazy) {
t.sum = (t.r - t.l + 1) * lazy;
t.lazy = lazy;
}
void down(int u) {
if (tr[u].lazy != -1) {
down(tr[u << 1], tr[u].lazy), down(tr[u << 1 | 1], tr[u].lazy);
tr[u].lazy = -1;
}
}
void build(int u, int l, int r) {
if (l == r) {
tr[u] = {l, r, 1, -1};
return ;
}
tr[u] = {l, r}, tr[u].lazy = -1;
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 x) {
if (l <= tr[u].l && tr[u].r <= r) {
down(tr[u], x);
return ;
}
down(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) modify(u << 1, l, r, x);
if (r > mid) modify(u << 1 | 1, l, r, x);
pushup(u);
}
int query(int u, int l, int r) {
if (l <= tr[u].l && tr[u].r <= r) return tr[u].sum;
down(u);
int mid = tr[u].l + tr[u].r >> 1;
int res = 0;
if (l <= mid) res = query(u << 1, l, r);
if (r > mid) res += query(u << 1 | 1, l, r);
return res;
}
int check(int x, int pos) {
int l = x, r = n, res = 0;
while (l <= r) {
int mid = l + r >> 1;
if (query(1, x, mid) >= pos) res = mid, r = mid - 1;
else l = mid + 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
while (T -- ) {
cin >> n >> m;
build(1, 1, n);
while (m --) {
int op, L, R;
cin >> op >> L >> R;
if (op == 1) {
L ++;
int mx = query(1, L, n);
if (!mx) cout << "Can not put any one." << endl;
else {
int l = check(L, 1), r = check(L, min(mx, R));
cout << l - 1 << ' ' << r - 1 << endl;
modify(1, l, r, 0);
}
}
else {
L ++, R ++;
cout << R - L + 1 - query(1, L, R) << endl;
modify(1, L, R, 1);
}
}
cout << endl;
}
return 0;
}