写了4个小时,人都给我弄傻了,很有参考意义的一道题
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>
#define INF 0x3f3f3f3f
#define IMAX 2147483646
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int
using namespace std;
struct T {
int l, r;
ll add, dat,ls, rs;
#define add(p) (t[p].add)
#define sum(p) (t[p].r - t[p].l + 1)
#define dat(p) (t[p].dat)
#define ls(p) (t[p].ls)
#define rs(p) (t[p].rs)
#define l(p) (t[p].l)
#define r(p) (t[p].r)
}t[51111 * 4];
//void spread(int p) {
// add(p * 2) = add(p * 2 + 1) = add(p);
// if (add(p) != -1) {
// dat(p * 2) = ls(p * 2) = rs(p * 2) = add(p)*sum(p * 2);
// dat(p * 2 + 1) = ls(p * 2 + 1) = rs(p * 2 + 1) = add(p)*sum(p * 2 + 1);
// }
// add(p) = -1;
//}
void spread(int p) {
if (add(p) == -1) return;
add(p * 2) = add(p * 2 + 1) = add(p);
dat(p * 2) = ls(p * 2) = rs(p * 2) = add(p)*sum(p * 2);
dat(p * 2 + 1) = ls(p * 2 + 1) = rs(p * 2 + 1) = add(p)*sum(p * 2 + 1);
add(p) = -1;
}
void change(int p) {
if (sum(p * 2) == dat(p * 2))ls(p) = sum(p * 2) + ls(p * 2 + 1);
else ls(p) = ls(p * 2);
if (sum(p * 2 + 1) == dat(p * 2 + 1))rs(p) = sum(p * 2 + 1) + rs(p * 2);
else rs(p) = rs(p * 2 + 1);
dat(p) = max(max(dat(p * 2), dat(p * 2 + 1)), rs(p * 2) + ls(p * 2 + 1));
}
void build(int p,int l,int r) {
r(p) = r, l(p) = l;
add(p) = -1;
if (l == r) {
dat(p) = ls(p) = rs(p) = 1;
return;
}
int mid = (l + r) / 2;
build(p * 2, l, mid);
build(p * 2 + 1, mid + 1, r);
change(p);
}
void change(int p, int l, int r, int d) {
if (l(p) >= l && r(p) <= r) {
dat(p) = ls(p) = rs(p) = d * (r(p) - l(p) + 1);
add(p) = d;
return;
}
spread(p);
//int mid = (l + r) / 2;
/*if (mid >= l) change(p * 2, l, mid, d);
if (mid + 1 <= r)change(p * 2 + 1, mid + 1, r, d);*/
int mid = (l(p) + r(p)) / 2;
if (mid >= l) change(p * 2, l, r, d);
if (mid + 1 <= r)change(p * 2 + 1, l, r, d);
change(p);
}
int ask(int p, int x) {
if (l(p) == r(p))return l(p);
spread(p);
if (dat(p * 2) >= x)return ask(p * 2, x);
if (rs(p * 2) + ls(p * 2 + 1) >= x)
return (l(p) + r(p)) / 2 - rs(p * 2) + 1;
if (dat(p * 2 + 1) >= x)return ask(p * 2 + 1, x);
return 0;
}
int n, m, op, x, d, ans;
int main() {
scanf("%d%d", &n, &m); build(1, 1, n);
while (m--) {
scanf("%d", &op);
if (op == 1) {
scanf("%d", &d);
if (t[1].dat < d)printf("0\n");
else {
ans = ask(1, d);
printf("%d\n", ans);
change(1, ans, ans + d - 1, 0);
}
}
else {
scanf("%d%d", &x, &d);
change(1, x, x + d - 1, 1);
}
}
return 0;
}