PKU 3667 - Hotel (线段树 + 区间合并 + 区间覆盖)
思路
学习了区间修改的线段树。。还要好好消化
代码
#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
#include <cmath>
#define LL long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const int MAXN = 51000 + 10;
const int MOD = 20071027;
typedef pair<int, int> pii;
typedef vector<int>::iterator viti;
typedef vector<pii>::iterator vitii;
struct SEGTREE
{
int lmax, rmax, max, cover;
}segt[MAXN << 2];
void PushUp(int rt, int m)
{
segt[rt].lmax = segt[LRT].lmax; segt[rt].rmax = segt[RRT].rmax;
if (segt[rt].lmax == (m - (m >> 1))) segt[rt].lmax += segt[RRT].lmax;
if (segt[rt].rmax == (m >> 1)) segt[rt].rmax += segt[LRT].rmax;
segt[rt].max = max(segt[LRT].rmax + segt[RRT].lmax, max(segt[LRT].max, segt[RRT].max));
}
void PushDown(int rt, int m)
{
segt[LRT].lmax = segt[LRT].rmax = segt[LRT].max = (segt[rt].cover ? 0 : m - (m >> 1));
segt[RRT].lmax = segt[RRT].rmax = segt[RRT].max = (segt[rt].cover ? 0 : (m >> 1));
segt[LRT].cover = segt[RRT].cover = segt[rt].cover;
segt[rt].cover = -1;
}
void Update(int rt, int l, int r, int L, int R, int type)
{
if (L <= l && r <= R)
{
segt[rt].lmax = segt[rt].rmax = segt[rt].max = (type ? 0 : r - l + 1);
segt[rt].cover = type;
return;
}
if (segt[rt].cover != -1) PushDown(rt, r - l + 1);
int mid = MID(l, r);
if (L <= mid) Update(LC, L, R, type);
if (R > mid) Update(RC, L, R, type);
PushUp(rt, r - l + 1);
}
int Query(int rt, int l, int r, int val)
{
if (l == r) return l;
if (segt[rt].cover != -1) PushDown(rt, r - l + 1);
int mid = MID(l, r);
if (segt[LRT].max >= val) return Query(LC, val);
else if (segt[LRT].rmax + segt[RRT].lmax >= val) return mid - segt[LRT].rmax + 1;
else Query(RC, val);
}
void Build(int rt, int l, int r)
{
segt[rt].lmax = segt[rt].rmax = segt[rt].max = r - l + 1;
segt[rt].cover = -1;
if (l == r) return;
int mid = MID(l, r);
Build(LC); Build(RC);
}
int main()
{
//ROP;
int ntot, n, i, j;
scanf("%d%d", &ntot, &n);
Build(1, 1, ntot);
while (n--)
{
int k, a, b;
scanf("%d", &k);
if (k == 1)
{
scanf("%d", &a);
if (segt[1].max < a) puts("0");
else
{
int p = Query(1, 1, ntot, a);
printf("%d\n", p);
Update(1, 1, ntot, p, p + a - 1, 1);
}
}
else
{
scanf("%d%d", &a, &b);
Update(1, 1, ntot, a, a + b - 1, 0);
}
}
return 0;
}