考虑以日期为关键字,把预约用Splay进行维护。
先考虑加入新的预约。设新的预约是从
l
日到
很显然,一个从
l0
日到
r0
日的预约与这个新的预约不冲突的充分必要条件为:
r0<l
或
l0>r
。
首先找出满足
r0<l
的情况下
r0
最大的节点
x
,以及满足
下面使用类似于Splay维护序列时提取区间的操作,把节点
第
2
个操作就是询问根节点的
代码:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read() {
int res = 0; bool bo = 0; char c;
while (((c = getchar()) < '0' || c > '9') && c != '-');
if (c == '-') bo = 1; else res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
return bo ? ~res + 1 : res;
}
inline char get() {
char c; while ((c = getchar()) != 'A' && c != 'B'); return c;
}
const int N = 2e5 + 5;
int n, T, Rt, cnt, fa[N], lc[N], rc[N], sze[N], L[N], R[N];
int which(int x) {return rc[fa[x]] == x;}
void upt(int x) {
sze[x] = 1;
if (lc[x]) sze[x] += sze[lc[x]];
if (rc[x]) sze[x] += sze[rc[x]];
}
void rotate(int x) {
int y = fa[x], z = fa[y], b = lc[y] == x ? rc[x] : lc[x];
if (z) (lc[z] == y ? lc[z] : rc[z]) = x;
fa[x] = z; fa[y] = x; if (b) fa[b] = y;
if (lc[y] == x) rc[x] = y, lc[y] = b;
else lc[x] = y, rc[y] = b; upt(y); upt(x);
}
void splay(int x, int tar) {
while (fa[x] != tar) {
if (fa[fa[x]] != tar) {
if (which(x) == which(fa[x])) rotate(fa[x]);
else rotate(x);
}
rotate(x);
}
if (!tar) Rt = x;
upt(x);
}
int PreR(int v) {
int x = Rt, res = 1;
while (x)
if (R[x] < v) res = x, x = rc[x];
else x = lc[x];
return res;
}
int SufL(int v) {
int x = Rt, res = 2;
while (x)
if (L[x] > v) res = x, x = lc[x];
else x = rc[x];
return res;
}
void ins(int l, int r) {
int x = PreR(l), y = SufL(r);
splay(x, 0); splay(y, Rt);
printf("%d\n", (lc[rc[Rt]] ? sze[lc[rc[Rt]]] : 0));
if (lc[rc[Rt]]) {
cnt -= sze[lc[rc[Rt]]];
fa[lc[rc[Rt]]] = 0; lc[rc[Rt]] = 0;
upt(rc[Rt]); upt(Rt);
}
L[lc[rc[Rt]] = ++T] = l; R[T] = r; fa[T] = rc[Rt];
sze[T] = 1; upt(rc[Rt]); upt(Rt); cnt++;
}
void init() {
fa[T = rc[Rt = 1] = 2] = sze[2] = 1;
L[sze[1] = 2] = R[2] = 1e5 + 3;
}
int main() {
int i, x, y; n = read(); char c; init();
while (n--) {
c = get(); if (c == 'A') {
x = read(); y = read();
ins(x, y);
}
else printf("%d\n", cnt);
}
return 0;
}