hdu 4614 Vases and Flowers 线段树

这两天在搞基础的图论。。。一直没去搞这个,今天一搞原来这么简单,怪不得别人说线段树模板题,只不过函数多了点,成段更新,单点询问,懒惰标记。


#include <stdio.h>
#include <string.h>
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r

const int maxn = 50005;
struct PP{
	int sum, flag;
}node[maxn<<2];
void create(int rt, int l, int r) {
	node[rt].sum = r-l+1;
	node[rt].flag = 0;
	if(l == r)	return ;
	int mid = (l+r)/2;
	create(lson);create(rson);
}

void push_up(int rt) {
	node[rt].sum = node[rt<<1].sum + node[rt<<1|1].sum;
}

void push_down(int rt, int l, int r) {
	int mid = (l+r)/2;
	if(node[rt].flag == 1) {
		node[rt<<1].flag = node[rt<<1|1].flag = 1;
		node[rt<<1].sum = mid-l+1;
		node[rt<<1|1].sum = r-mid;
		node[rt].flag = 0;
	}
	else if(node[rt].flag == 2) {
		node[rt<<1].flag = node[rt<<1|1].flag = 2;
		node[rt<<1].sum = node[rt<<1|1].sum = 0;
		node[rt].flag = 0;
	}
}

int get_sum(int rt, int l, int r, int x) {
	if(l == x)	{
		return node[rt].sum;
	}
	push_down(rt, l, r);
	int mid = (l+r)/2;
	if(x <= mid)
		return get_sum(lson, x) + node[rt<<1|1].sum;
	if(x > mid)
		return get_sum(rson, x);
	push_up(rt);
}

int get_pos(int rt, int l, int r, int num) {
	if(l == r)	return l;
	push_down(rt, l, r);
	int mid = (l+r)/2;
	int lsum = node[rt<<1].sum;
	if(lsum >= num)
		return get_pos(lson, num);
	return get_pos(rson, num-lsum);
	push_up(rt);
}

void update1(int rt, int l, int r, int L, int R) {
	if(L <= l && R >= r) {
		node[rt].flag = 2;
		node[rt].sum = 0;
		return ;
	}
	push_down(rt, l, r);
	int mid = (l+r)/2;
	if(L <= mid)	update1(lson, L, R);
	if(R > mid)	update1(rson, L, R);
	push_up(rt);
}

int update2(int rt, int l, int r, int L, int R) {
	if(L <= l && R >= r) {
		node[rt].flag = 1;
		int ret = r-l+1-node[rt].sum;
		node[rt].sum = r-l+1;
		return ret;
	}
	push_down(rt, l, r);
	int mid = (l+r)/2;
	int ans1 = 0, ans2 = 0;
	if(L <= mid)	ans1 = update2(lson, L, R);
	if(R > mid)	ans2 = update2(rson, L, R);
	push_up(rt);
	return ans1 + ans2;
}

int main() {
	int t, n, m, x, l, r;
	scanf("%d", &t);
	while(t--) {
		scanf("%d%d", &n, &m);
		create(1, 1, n);
		while(m--) {
			scanf("%d%d%d", &x, &l ,&r);
			if(x == 1) {
				l++;
				int cur = get_sum(1, 1, n, l);
				if(cur == 0) {
					puts("Can not put any one.");
					continue;
				}
				int ll = (node[1].sum-cur);
				int ans1 = get_pos(1, 1, n, ll+1);
				if(cur > r)	cur = r;
				int ans2 = get_pos(1, 1, n, ll+cur);
				printf("%d %d\n", ans1-1, ans2-1);
				update1(1, 1, n, ans1, ans2);
			}
			else {
				l++, r++;
				int ans = update2(1, 1, n, l, r);
				printf("%d\n", ans);
			}
		}
		puts("");
	}
	return 0;
}




你可能感兴趣的:(hdu 4614 Vases and Flowers 线段树)