DS qt to find a section of the most forward-length QT space
NS QT Find a section of the most forward-length QT space, if not found can occupy the DS space as free space, find a section of the most forward space
study!! L R Empty the space of L, R
With two segment trees, one handling DS, one handling NS
Segment tree maintenance, ma_l, Ma_r, MA interval [l,r] to the left of the free interval, the right side, and the largest idle interval
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace Std;
const int MAXN = 1e5 + 10;
#define LEFT V<<1
#define RIGHT V<<1|1
struct node
{
int L, R;
int ma_l;
int ma_r;
int Ma;
int lazy;
}TREE[2][MAXN<<2];
void build (int l, int r, int v)
{
for (int i = 0;i < 2;i++)
{
TREE[I][V].L = l;
TREE[I][V].R = R;
Tree[i][v].lazy =-1;
tree[i][v].ma_l = Tree[i][v].ma = Tree[i][v].ma_r = (r-l + 1);
}
if (L = = r) return;
int mid = (L + r) >>1;
Build (L, Mid, left);
Build (Mid+1, R, right);
}
void Push_down (int v)
{
for (int i = 0; i < 2;i++)
if (tree[i][v].lazy! =-1)
{
tree[i][left].ma_l = Tree[i][left].ma_r = Tree[i][left].ma = (tree[i][left].r-tree[i][left].l + 1) *tree[i][v].lazy;
tree[i][right].ma_l = Tree[i][right].ma_r = Tree[i][right].ma = (tree[i][right].r-tree[i][right].l + 1) *tree[i][v]. Lazy;
Tree[i][left].lazy = Tree[i][right].lazy = Tree[i][v].lazy;
Tree[i][v].lazy =-1;
}
}
void push_up (int v)
{
for (int i = 0; i < 2;i++)
{
tree[i][v].ma_l = tree[i][left].ma_l;
Tree[i][v].ma_r = Tree[i][right].ma_r;
tree[i][v].ma = Max (max (tree[i][left].ma, tree[i][right].ma), tree[i][left].ma_r+tree[i][right].ma_l);
if (tree[i][v].ma_l = = (TREE[I][LEFT].R-TREE[I][LEFT].L + 1))
tree[i][v].ma_l + = tree[i][right].ma_l;
if (Tree[i][v].ma_r = = (TREE[I][RIGHT].R-TREE[I][RIGHT].L + 1))
Tree[i][v].ma_r + = Tree[i][left].ma_r;
}
}
int query (int v, const int flag, const int num)
{
if (tree[flag][v].l = = TREE[FLAG][V].R)
return TREE[FLAG][V].L;
Push_down (v);
if (tree[flag][left].ma >= num)
Return query (left, flag, num);
else if (tree[flag][left].ma_r + tree[flag][right].ma_l >= num)
return tree[flag][left].r-tree[flag][left].ma_r + 1;
else return query (right, flag, num);
}
void Update (int a, int b, int v, const int flag, const int tag)
{
if (tree[flag][v].l >= a && tree[flag][v].r <= b)
{
Tree[flag][v].lazy = tag;
tree[flag][v].ma_l = Tree[flag][v].ma = Tree[flag][v].ma_r = (tree[flag][v].r-tree[flag][v].l + 1) *tag;
return;
}
Push_down (v);
int mid = (tree[flag][v].l + tree[flag][v].r) >> 1;
if (a <= mid) update (A, B, left, flag, tag);
if (b > Mid) update (A, B, right, flag, tag);
Push_up (v);
}
int main ()
{
Freopen ("In.txt", "R", stdin);
int T;
scanf ("%d", &t);
int cas = 0;
while (t--)
{
int N, Q;
scanf ("%d%d", &n, &q);
Build (1, N, 1);
Char ch[10];
int L, R, QT;
printf ("Case%d:\n", ++cas);
while (q--)
{
scanf ("%s", ch);
if (ch[0] = = ' D ')
{
scanf ("%d", &QT);
if (Tree[1][1].ma < QT)
{
Puts ("Fly with Yourself");
Continue;
}
int pos = query (1, 1, QT);
Update (POS, pos + qt-1, 1, 1, 0);
printf ("%d,let ' s fly\n", POS);
}
else if (ch[0] = = ' N ')
{
scanf ("%d", &QT);
int POS;
if (Tree[1][1].ma < qt && Tree[0][1].ma < QT)
{puts ("Wait for Me"); continue;}
else if (tree[1][1].ma >= qt)
pos = Query (1, 1, QT);
else pos = query (1, 0, QT);
printf ("%d,don ' t put my gezi\n", POS);
Update (POS, pos + qt-1, 1, 0, 0);
Update (POS, pos + qt-1, 1, 1, 0);
}
else if (ch[0] = = ' S ')
{
scanf ("%d%d", &l, &r);
Update (L, R, 1, 0, 1);
Update (L, R, 1, 1, 1);
Puts ("I am the hope of Chinese chengxuyuan!!");
}
}
}
return 0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu4553 Dating Schedule Line tree