HDU 4614

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair  PII;
typedef long long LL;
const int maxn = 144444;
LL sum[maxn << 2];
int cover[maxn << 2];
int empty[maxn << 2];
int lhs[maxn << 2];
int rhs[maxn << 2];
void pushup(int rt) {
	sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
	empty[rt] = empty[rt << 1] + empty[rt << 1 | 1];
	lhs[rt] = min(lhs[rt << 1], lhs[rt << 1 | 1]);
	rhs[rt] = max(rhs[rt << 1], rhs[rt << 1 | 1]);
}
void pushdown(int l, int r, int rt) {
	
	if(cover[rt] > 0) {
		sum[rt << 1] += empty[rt << 1];
		sum[rt << 1 | 1] += empty[rt << 1 | 1];

		empty[rt << 1] = empty[rt << 1 | 1] = 0;

		lhs[rt << 1] = lhs[rt << 1 | 1] = maxn;
		rhs[rt << 1] = rhs[rt << 1 | 1] = -1;
		cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];
	}

	if(!cover[rt]) {
		sum[rt << 1] = sum[rt << 1 | 1] = 0;
		int m = (l + r) >> 1;
		empty[rt << 1] = m - l + 1;
		empty[rt << 1 | 1] = r - m;
		lhs[rt << 1] = l;
		rhs[rt << 1] = m;

		lhs[rt << 1 | 1] = m + 1;
		rhs[rt << 1 | 1] = r;
		cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];
	}

	cover[rt] = -1;
}
void build(int l, int r, int rt) {
	empty[rt] = r - l + 1;
	cover[rt] = -1;
	sum[rt] = 0;
	lhs[rt] = l;
	rhs[rt] = r;
	if(l == r)
		return;
	int m = (l + r) >> 1;
	build(lson);
	build(rson);
}
void update(int L, int R, int c, int l, int r, int rt) {
	if(L <= l && r <= R) {
		if(c) {
			sum[rt] += empty[rt];
			empty[rt] = 0;
			lhs[rt] = maxn;
			rhs[rt] = -1;
			cover[rt] = true;
		}
		else {
			sum[rt] = cover[rt] = 0;
			empty[rt] = r - l + 1;
			lhs[rt] = l;
			rhs[rt] = r;
		}

		cover[rt] = c;
		return;
	}
	pushdown(l, r, rt);
	int m = (l + r) >> 1;
	if(L <= m)
		update(L, R, c, lson);
	if(m < R)
		update(L, R, c, rson);
	pushup(rt);
}
PII query(int L, int R, LL &s, int l, int r, int rt) {
	if(L <= l && r <= R) {
		s += sum[rt];
		return PII(lhs[rt], rhs[rt]);
	}
	pushdown(l, r, rt);
	int m = (l + r) >> 1;
	int x = maxn, y = -1;
	PII tmp;
	if(L <= m) {
		tmp = query(L, R, s, lson);
		x = min(tmp.first, x);
		y = max(tmp.second, y);
	}
	if(m < R) {
		tmp = query(L, R, s, rson);
		x = min(tmp.first, x);
		y = max(tmp.second, y);
	}
	return PII(x, y);
}
int query(int L, int R, int l, int r, int rt) {
	if(L <= l && r <= R) {

		return empty[rt];
	}
	pushdown(l, r, rt);
	int m = (l + r) >> 1;
	int t = 0;
	if(L <= m)
		t += query(L, R, lson);
	if(m < R)
		t += query(L, R, rson);
	return t;
}
int main() {
	int n, m, i, K, A, F, B;
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d%d", &n, &m);
		build(1, n, 1);
		LL s;
		for(i = 0; i < m; ++i) {
			scanf("%d", &K);
			if(K & 1) {
				scanf("%d%d", &A, &F);
				A++;
				s = 0;
				if(query(A, n, 1, n, 1) == 0) {
					puts("Can not put any one.");
					continue;
				}

				int mid, l = A, r = n, pos = n;
				while(l <= r) {
					mid = (l + r) >> 1;
					if(query(A, mid, 1, n, 1) <= F)
						pos = mid, l = mid + 1;
					else
						r = mid - 1;
				}
				s = 0;
				PII tmp = query(A, pos, s, 1, n, 1);
				printf("%d %d\n", tmp.first - 1, tmp.second - 1);
				update(A, pos, 1, 1, n, 1);
			}
			else {
				scanf("%d%d", &A, &B);
				s = 0;
				A++, B++;
				query(A, B, s, 1, n, 1);
				printf("%I64d\n", s);
				update(A, B, 0, 1, n, 1);
			}
		}
		puts("");
	}
	return 0;
}


你可能感兴趣的:(区间树/线段树,Fenwick,acm)