维护两个线段树,一个男生的树, 一个女生的树
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
struct Node {
int lmax, rmax, ans;// lmax代表从左端点开始的最大连续区间和 rmax代表从右端点开始向左的最大连续区间和
//ans是最大的连续区间和
} b[N << 2], g[N << 2];
int t, n, m, x, y;
char s[10];
void pushup(int id, int l, int r) {
//st >=0 更新b st>= 1更新g
int mid = (l + r) >> 1;
Node &root = b[id], < = b[id << 1], &rt = b[id << 1 | 1];
root.lmax = lt.lmax + (lt.lmax == (mid - l + 1) ? rt.lmax : 0); //若左边 最大和是整个区间那么就再加上右区间的lmax
root.rmax = rt.rmax + (rt.rmax == (r - mid) ? lt.rmax : 0);
root.ans = max(max(lt.ans, rt.ans), lt.rmax + rt.lmax);
Node &groot = g[id], &glt = g[id << 1], &grt = g[id << 1 | 1];
groot.lmax = glt.lmax + (glt.lmax == (mid - l + 1) ? grt.lmax : 0);
groot.rmax = grt.rmax + (grt.rmax == (r - mid) ? glt.rmax : 0);
groot.ans = max(max(glt.ans, grt.ans), glt.rmax + grt.lmax);
}
void pushdown(int id, int l, int r) {
//b g数组都进行下放
int mid = (l + r) >> 1;
Node &root = b[id], < = b[id << 1], &rt = b[id << 1 | 1];
if (root.ans == r - l + 1) { //说明是学习导致的下放
lt.lmax = lt.rmax = lt.ans = mid - l + 1;
rt.lmax = rt.rmax = rt.ans = r - mid;
}
if (root.ans == 0) { //约会导致的下放
lt.lmax = lt.rmax = lt.ans = 0;
rt.lmax = rt.rmax = rt.ans = 0;
}
//引用只能赋值一次
Node &groot = g[id], &glt = g[id << 1], &grt = g[id << 1 | 1];
if (groot.ans == r - l + 1) { //说明是学习导致的下放
glt.lmax = glt.rmax = glt.ans = mid - l + 1;
grt.lmax = grt.rmax = grt.ans = r - mid;
}
if (groot.ans == 0) { //约会导致的下放
glt.lmax = glt.rmax = glt.ans = 0;
grt.lmax = grt.rmax = grt.ans = 0;
}
}
void build(int id, int l, int r) {
if (l == r) {
b[id].ans = b[id].lmax = b[id].rmax = g[id].ans = g[id].lmax = g[id].rmax = 1;
return ;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
pushup(id, l, r);
}
void update(int id, int l, int r, int x, int y, int st) {
if (x <= l && r <= y) {
if (st == 0) { //代表男生修改为0
b[id].lmax = b[id].rmax = b[id].ans = 0;
} else { //男女生都修改为0 st==2代表男女生都修改为1
b[id].lmax = b[id].rmax = b[id].ans = (st == 1 ? 0 : r - l + 1);
g[id].lmax = g[id].rmax = g[id].ans = (st == 1 ? 0 : r - l + 1);
}
return ;
}
pushdown(id, l, r);
int mid = (l + r) >> 1;
if (x <= mid) update(id << 1, l, mid, x, y, st);
if (y > mid) update(id << 1 | 1, mid + 1, r, x, y, st);
pushup(id, l, r);
}
int query(int id, int l, int r, int x, int st) {
if (l == r) return l; //查询到单点了 只能返回了 由于我们查询之前判断过,代表一定能查询到结果才进行查询
pushdown(id, l, r);
int mid = (l + r) >> 1;
//3种情况 要么是在左边区间, 如果不在左边区间那么再判断是否是左边区间和右边区间的并,若还不是只能在右区间了
if (st == 0) { //对b线段树进行查询
Node &root = b[id], < = b[id << 1], &rt = b[id << 1 | 1];
if (lt.ans >= x) {
return query(id << 1, l, mid, x, st);
} else if ((lt.rmax + rt.lmax) >= x) {
return mid - lt.rmax + 1;
} else {
return query(id << 1 | 1, mid + 1, r, x, st);
}
}
if (st == 1) {
Node &groot = g[id], &glt = g[id << 1], &grt = g[id << 1 | 1];
if (glt.ans >= x) {
return query(id << 1, l, mid, x, st);
} else if ((glt.rmax + grt.lmax) >= x) {
return mid - glt.rmax + 1;
} else {
return query(id << 1 | 1, mid + 1, r, x, st);
}
}
}
int main() {
scanf("%d", &t);
for (int ci = 1; ci <= t; ci++) {
scanf("%d%d", &n, &m);
build(1, 1, n);
printf("Case %d:\n", ci);
for (int i = 1; i <= m; i++) {
scanf("%s%d", s, &x);
if (s[0] == 'D') {
if (b[1].ans >= x) {
int t = query(1, 1, n, x, 0);
update(1, 1, n, t, t + x - 1, 0);
printf("%d,let's fly\n", t);
} else {
printf("fly with yourself\n");
}
} else if (s[0] == 'N') {
if (b[1].ans >= x) {
int t = query(1, 1, n, x, 0); // 0代表在b数组进行查询
update(1, 1, n, t, t + x - 1, 1); //1代表b、g数组的该区间都更新为0 2代表更新为1
printf("%d,don't put my gezi\n", t);
} else if (g[1].ans >= x) {
int t = query(1, 1, n, x, 1);
update(1, 1, n, t, t + x - 1, 1);
printf("%d,don't put my gezi\n", t);
} else {
printf("wait for me\n");
}
} else {
scanf("%d", &y);
update(1, 1, n, x, y, 2);
printf("I am the hope of chinese chengxuyuan!!\n");
}
}
}
return 0;
}