POJ 3667 Hotel(線分樹:区間カバー+最大連結子区間長)


ホテル
Time Limit: 3000 MS
 
メモリLimit: 65536 K
Total Submissions: 21673
 
Acceepted: 9442
Description The cows are Journeying north to Thunder Bay in Candar to gain cultural enrichment and enjoy a vacation on the sunny shore Superior.Bessie、ever the comptent travegent、has med the Bulmonce Honerse Honerse Honerse N (1≦ N ≤50,000)rooms all located on the same side of an extremely long hallway(all the better to see the lake,of course).The cows and other visitors arrive in groups of size Di (1≦ Di ≦N)and appech the front desk to check in.Each group i requests a set of Di contigous room from Canumu,the moose staffing the counter.He assigns the m some set of consecutive room numbers r.r+Di-1 if they are available or,ifのcontigous set of rooms is available,polity suggame alternate lodging.Canmu always choss the value of r to be the smalest possible.Visitors also depart the hotel from groups of contigous rooms.Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1(1≦ Xi ≦ N-Di+1).Some(or all)of those rooms might be empy before the checout.Your job is to assist Canmu by processing M (1≦ M < 50,000)checkin/checout requests.The hotel is initially unoccupied.Input*Line 1:Two space-separated integers: N and M*Lines 2.. M+1:Line i+1 contains request expresed as one of two possible formas:(a)Two space separated integers representing a check-n request:1 and Di (b)Three space-separated integers representing a check-out:2、 Xi,and Di Output*Liness 1...:For each check-in request,output a single line with a single integer r,the first room in the contigous sequence of rooms to be occupied.If the request cannot be satis fied,output 0.Sample Input
10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
Sample Output
1
4
7
0
5
Source    USACO 2008 Febrasury Gold
 
件名:
長さnの区間、mの操作は最初は0です.
1 xは、長さxの0を求める連続区間の最左端を表し、この区間を1にします.
2 x yは区間[x,y]を0にすることを表します.
分析:
線分樹区間の合併の入門問題は、pusshowとpusupの操作に注意します.
区間の割り当て:怠け者マークが必要で、題意はカバーマークが必要で、直接flagsが働きます.
全体クエリー(すべて)
#include 
#include 
#include 
using namespace std;
const int N = 500006, INF = 0x3f3f3f3f;
int n, m, a[N];
typedef long long ll;
struct T {
	//int l, r, sum, lx, rx, ans;
	//1.    :(l l r) l r        ,(0,x,y):a[x]   y
    //   sum,         ans,
	//            lx,            rx
	int l,r,ls,rs,sum,flag;
	//2.    :(l len)          len      ,     ;(2,x,len):  a[x]~a[x+len-1]
	//    +    flag: 0        , 1       , -1                    .
} tree[N*4];
void  pushup(int p){
	//tree[p].sum = tree[p<<1].sum + tree[p<<1|1].sum;
	//tree[p].lx = max(tree[p<<1].lx, tree[p<<1].sum + tree[p<<1|1].lx);
	//tree[p].rx = max(tree[p<<1|1].rx, tree[p<<1].rx + tree[p<<1|1].sum);
    //tree[p].ans = max(max(tree[p<<1].ans, tree[p<<1|1].ans), tree[p<<1].rx + tree[p<<1|1].lx); 
    tree[p].ls = tree[p<<1].ls;
	tree[p].rs = tree[p<<1|1].rs;
	if (tree[p<<1].ls == tree[p<<1].r - tree[p<<1].l + 1) 
		tree[p].ls += tree[p<<1|1].ls;
	if (tree[p<<1|1].rs == tree[p<<1|1].r - tree[p<<1|1].l + 1)
	    tree[p].rs += tree[p<<1].rs;
	    
	tree[p].sum = max(max(tree[p<<1].sum, tree[p<<1|1].sum), tree[p<<1].rs + tree[p<<1|1].ls);
}
//1             pushdown
void pushdown(int p){

    if (tree[p].flag != -1)
	{
		tree[p<<1].flag = tree[p<<1|1].flag = tree[p].flag;
	    if (tree[p<<1].flag) {
		     tree[p<<1].ls = tree[p<<1].rs = tree[p<<1].sum = tree[p<<1].r - tree[p<<1].l + 1;
		     tree[p<<1|1].ls = tree[p<<1|1].rs = tree[p<<1|1].sum = tree[p<<1|1].r - tree[p<<1|1].l + 1;
	    } else 
	    tree[p<<1].ls = tree[p<<1].rs = tree[p<<1].sum = tree[p<<1|1].ls = tree[p<<1|1].rs = tree[p<<1|1].sum = 0;
	    tree[p].flag = -1;
	}
}
void build(int p, int l, int r) {
	
	tree[p].l = l;
	tree[p].r = r;
	tree[p].sum = tree[p].ls = tree[p].rs = r - l + 1;
	tree[p].flag = -1;
	if (l == r) {
		//tree[p].sum = tree[p].lx = tree[p].rx = tree[p].ans = a[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(p << 1, l, mid);
	build(p << 1 | 1, mid + 1, r);
	pushup(p);
}

//1      ,a[x]=y 
/*void change1(int p, int x, int y) {
	if (tree[p].l == tree[p].r) {
		tree[p].sum = tree[p].lx = tree[p].rx = tree[p].ans = y;
		return;
	}
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (x <= mid) change1(p << 1, x, y);
	else change1(p << 1 | 1, x, y);
	pushup(p);
} */
//2       
void change2(int p, int l, int r,ll val) {
	if (tree[p].l >= l && tree[p].r <= r) {
		if(val==0)
		tree[p].ls = tree[p].rs = tree[p].sum = tree[p].flag = 0;
	
		if(val==1)
		{
			tree[p].ls = tree[p].rs = tree[p].sum = tree[p].r - tree[p].l + 1;
			tree[p].flag=1;
		}
		return;
	}
	pushdown(p);
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (l <= mid) change2(p << 1, l, r,val);
	if (r > mid) change2(p << 1 | 1, l, r,val);
	pushup(p);
}
//2                 
int ask1(int p, int d) {
	if (tree[p].l == tree[p].r) return tree[p].l;
	 pushdown(p);
	if (tree[p<<1].sum >= d) return ask1(p << 1, d);
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (tree[p<<1].rs + tree[p<<1|1].ls >= d) return mid - tree[p<<1].rs + 1;
	if (tree[p<<1|1].sum >= d) return ask1(p << 1 | 1, d);
	return -1;
}
/*
//  1   l r        
T ask(int p, int l, int r) {
	if (l <= tree[p].l && r >= tree[p].r) return tree[p];
	T a, b, ans;
	a.sum = a.lx = a.rx = a.ans = b.sum = b.lx = b.rx = b.ans = -INF;
	ans.sum = 0;
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (l <= mid) {
		a = ask(p << 1, l, r);
		ans.sum += a.sum;
	}
	if (r > mid) {
		b = ask(p << 1 | 1, l, r);
		ans.sum += b.sum;
	}
	ans.ans = max(max(a.ans, b.ans), a.rx + b.lx);
	ans.lx = max(a.lx, a.sum + b.lx);
	ans.rx = max(b.rx, b.sum + a.rx);
	if (l > mid) ans.lx = max(ans.lx, b.lx);
	if (r <= mid) ans.rx = max(ans.rx, a.rx);
	return ans;
}
 */
int main() {
	cin >> n >> m;
	build(1, 1, n);
	while (m--) {
		int op, x, d;
		scanf("%d", &op);
		if (op == 1) {
			scanf("%d", &d);
			if (tree[1].sum < d) puts("0");
			else {
				int ans = ask1(1, d);
				printf("%d
", ans); change2(1, ans, ans + d - 1,0); } } else { scanf("%d %d", &x, &d); change2(1, x, x + d - 1,1); } } return 0; }