poj 3667

19 篇文章 0 订阅
5 篇文章 0 订阅

写了4个小时,人都给我弄傻了,很有参考意义的一道题

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>

#define INF 0x3f3f3f3f
#define IMAX 2147483646
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int

using namespace std;

struct T {
	int l, r;
	ll add, dat,ls, rs;
#define add(p) (t[p].add)
#define sum(p) (t[p].r - t[p].l + 1)
#define dat(p) (t[p].dat)
#define ls(p) (t[p].ls)
#define rs(p) (t[p].rs)
#define l(p) (t[p].l)
#define r(p) (t[p].r)
}t[51111 * 4];

//void spread(int p) {
//  add(p * 2) = add(p * 2 + 1) = add(p);
//	if (add(p) != -1) {
//		dat(p * 2) = ls(p * 2) = rs(p * 2) = add(p)*sum(p * 2);
//		dat(p * 2 + 1) = ls(p * 2 + 1) = rs(p * 2 + 1) = add(p)*sum(p * 2 + 1);
//	}
//	add(p) = -1;
//}

void spread(int p) {
	if (add(p) == -1) return;
	add(p * 2) = add(p * 2 + 1) = add(p);
	dat(p * 2) = ls(p * 2) = rs(p * 2) = add(p)*sum(p * 2);
	dat(p * 2 + 1) = ls(p * 2 + 1) = rs(p * 2 + 1) = add(p)*sum(p * 2 + 1);
	add(p) = -1;
}

void change(int p) {
	if (sum(p * 2) == dat(p * 2))ls(p) = sum(p * 2) + ls(p * 2 + 1);
	else ls(p) = ls(p * 2);
	if (sum(p * 2 + 1) == dat(p * 2 + 1))rs(p) = sum(p * 2 + 1) + rs(p * 2);
	else rs(p) = rs(p * 2 + 1);
	dat(p) = max(max(dat(p * 2), dat(p * 2 + 1)), rs(p * 2) + ls(p * 2 + 1));
}

void build(int p,int l,int r) {
	r(p) = r, l(p) = l;
	add(p) = -1;
	if (l == r) {
		dat(p) = ls(p) = rs(p) = 1;
		return;
	}
	int mid = (l + r) / 2;
	build(p * 2, l, mid);
	build(p * 2 + 1, mid + 1, r);
	change(p);
}

void change(int p, int l, int r, int d) {
	if (l(p) >= l && r(p) <= r) {
		dat(p) = ls(p) = rs(p) = d * (r(p) - l(p) + 1);
		add(p) = d;
		return;
	}
	spread(p);
	//int mid = (l + r) / 2;
	/*if (mid >= l) change(p * 2, l, mid, d);
	if (mid + 1 <= r)change(p * 2 + 1, mid + 1, r, d);*/
	int mid = (l(p) + r(p)) / 2;
	if (mid >= l) change(p * 2, l, r, d);
	if (mid + 1 <= r)change(p * 2 + 1, l, r, d);
	change(p);
}

int ask(int p, int x) {
	if (l(p) == r(p))return l(p);
	spread(p);
	if (dat(p * 2) >= x)return ask(p * 2, x);
	if (rs(p * 2) + ls(p * 2 + 1) >= x)
		return (l(p) + r(p)) / 2 - rs(p * 2) + 1;
	if (dat(p * 2 + 1) >= x)return ask(p * 2 + 1, x);
	return 0;
}

int n, m, op, x, d, ans;

int main() {
	scanf("%d%d", &n, &m); build(1, 1, n);
	while (m--) {
		scanf("%d", &op);
		if (op == 1) {
			scanf("%d", &d);
			if (t[1].dat < d)printf("0\n");
			else {
				ans = ask(1, d);
				printf("%d\n", ans);
				change(1, ans, ans + d - 1, 0);
			}
		}
		else {
			scanf("%d%d", &x, &d);
			change(1, x, x + d - 1, 1);
		}
	}

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值