hdu 4614 Vases and Flowers 线段树

线段树这么精细的东西太不适合我了。。真难敲

//http://blog.csdn.net/zz_1215/
#pragma comment(linker, "/STACK:102400000,102400000")
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long i64;
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define pb              push_back
#define pf              push_front
#define X               first
#define Y               second
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define MC(a,b)		memcpy(a,b,sizeof(b))
#define MAX(a,b)        ((a)>(b)?(a):(b))
#define MIN(a,b)        ((a)<(b)?(a):(b))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 51234*4;

struct segtree{
	int sum;
	int l;
	int r;
	int sign;
	void gao(int value){
		if (value == 1){
			sign = 1;
			sum = len();
		}
		else if (value == -1){
			sign = -1;
			sum = 0;
		}
	}
	int len(){
		return r - l + 1;
	}
	int mid(){
		return (l + r) / 2;
	}
	int empty(){
		return len() - sum;
	}
}t[maxn];

int n, m;

void build(int l, int r, int node=1){
	t[node].l = l;
	t[node].r = r;
	t[node].sign = 0;
	t[node].sum = 0;
	if (l != r){
		int mid = (l + r) / 2;
		build(l, mid, LL(node));
		build(mid + 1, r,RR(node));
	}
}

void push_down(int node){
	if (t[node].l != t[node].r){
		t[LL(node)].gao(t[node].sign);
		t[RR(node)].gao(t[node].sign);
		t[node].sign = 0;
	}
}

void push_up(int node){
	t[node].sum = t[LL(node)].sum + t[RR(node)].sum;
}

void update(int l, int r,int node,int value){
	if (rt[node].r) return;
	if (l <= t[node].l && t[node].r <= r){
		t[node].gao(value);
		push_down(node);
		return;
	}
	push_down(node);
	update(l, r, LL(node), value);
	update(l, r, RR(node), value);
	push_up(node);
}

int query(int l, int r, int node=1){
	if (rt[node].r) return 0;
	if (l <= t[node].l && t[node].r <= r){
		return t[node].sum;
	}
	push_down(node);
	return query(l, r, LL(node)) + query(l, r, RR(node));
}

int find_pos(int node, int y){
	if (t[node].l == t[node].r && t[node].sum == 0){
		return t[node].l;
	}
	else{
		push_down(node);
		if (t[LL(node)].empty() >= y || t[RR(node)].empty() == 0){
			return find_pos(LL(node), y);
		}
		else{
			return find_pos(RR(node), y - t[LL(node)].empty());
		}
	}
}

int main()
{
	int T;
	cin >> T;
	while (T--){
		cin >> n >> m;
		build(1, n);
		int k, x, y;
		for (int i = 1; i <= m; i++){
			//cin >> k >> x >> y;
			SS(k); SS(x); SS(y);
			if (k == 1){
				x++;
				int temp = x - 1 - query(1, x - 1, 1);
				if (t[1].empty() == temp) {
					puts("Can not put any one.");
				} else{
					int lpos = find_pos(1, temp + 1);
					int rpos = find_pos(1, temp + y);
					update(lpos, rpos, 1, 1);
					lpos--; rpos--;
					printf("%d %d\n", lpos, rpos);
				}
			}
			else{
				x++; y++;
				int temp = query(x, y);
				update(x, y, 1, -1);
				printf("%d\n", temp);
			}
		}
		puts("");
	}
	return 0;
}


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