【BZOJ3878】[Ahoi2014]奇怪的计算器【线段树】

【题目链接】

先把Xi排序,四种操作都不会改变Xi的相对顺序,这样L和R就是在左边和右边出现了,方便区间赋值。

然后各种标记传一传就行了。

赋值标记可以变为 乘法标记 = 0,加法标记 = 要赋的值,这样就可以少传一个标记。

/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

const int maxn = 100005, inf = 0x3f3f3f3f;

int n, m, L, R;

struct _data {
	int id, num;
} opt[maxn], num[maxn];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int cread() {
	char ch = getchar(); for(; ch != '+' && ch != '-' && ch != '*' && ch != '@'; ch = getchar());
	if(ch == '+') return 1;
	if(ch == '-') return 2;
	if(ch == '*') return 3;
	if(ch == '@') return 4;
}

inline bool cmp(_data x, _data y) {
	return x.num != y.num ? x.num < y.num : x.id < y.id;
}

LL mi[maxn << 2], mx[maxn << 2], addv[maxn << 2], mulv[maxn << 2], catv[maxn << 2];

inline void pushup(int p) {
	mi[p] = min(mi[p << 1], mi[p << 1 | 1]);
	mx[p] = max(mx[p << 1], mx[p << 1 | 1]);
}

inline void pushdown(int p, int l, int mid, int r) {
	int ls = p << 1, rs = p << 1 | 1;
	mi[ls] = mi[ls] * mulv[p] + addv[p] + num[l].num * catv[p];
	mx[ls] = mx[ls] * mulv[p] + addv[p] + num[mid].num * catv[p];
	mulv[ls] *= mulv[p]; addv[ls] *= mulv[p]; catv[ls] *= mulv[p];
	catv[ls] += catv[p]; addv[ls] += addv[p];
	mi[rs] = mi[rs] * mulv[p] + addv[p] + num[mid + 1].num * catv[p];
	mx[rs] = mx[rs] * mulv[p] + addv[p] + num[r].num * catv[p];
	mulv[rs] *= mulv[p]; addv[rs] *= mulv[p]; catv[rs] *= mulv[p];
	catv[rs] += catv[p]; addv[rs] += addv[p];
	addv[p] = 0; mulv[p] = 1; catv[p] = 0;
}

inline void build(int p, int l, int r) {
	mi[p] = inf; mx[p] = -inf; addv[p] = 0; mulv[p] = 1; catv[p] = 0;
	if(l == r) {
		mi[p] = mx[p] = num[l].num;
		return;
	}
	int mid = l + r >> 1;
	build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r);
	pushup(p);
}

inline void add(int p, int l, int r, int x, int y, int c) {
	if(x <= l && r <= y) {
		mi[p] += c; mx[p] += c;
		addv[p] += c;
		return;
	}
	int mid = l + r >> 1;
	pushdown(p, l, mid, r);
	if(x <= mid) add(p << 1, l, mid, x, y, c);
	if(y > mid) add(p << 1 | 1, mid + 1, r, x, y, c);
	pushup(p);
}

inline void mul(int p, int l, int r, int x, int y, int c) {
	if(x <= l && r <= y) {
		mi[p] *= c; mx[p] *= c;
		addv[p] *= c; mulv[p] *= c;
		return;
	}
	int mid = l + r >> 1;
	pushdown(p, l, mid, r);
	if(x <= mid) mul(p << 1, l, mid, x, y, c);
	if(y > mid) mul(p << 1 | 1, mid + 1, r, x, y, c);
	pushup(p);
}

inline void cat(int p, int l, int r, int x, int y, int c) {
	if(x <= l && r <= y) {
		mi[p] += (LL)c * num[l].num;
		mx[p] += (LL)c * num[r].num;
		catv[p] += c;
		return;
	}
	int mid = l + r >> 1;
	pushdown(p, l, mid, r);
	if(x <= mid) cat(p << 1, l, mid, x, y, c);
	if(y > mid) cat(p << 1 | 1, mid + 1, r, x, y, c);
	pushup(p);
}

inline void update(int p, int l, int r) {
	if(mi[p] >= L && mx[p] <= R) return;
	if(mi[p] < L && mx[p] < L) {
		mi[p] = mx[p] = L;
		addv[p] = L; mulv[p] = 0; catv[p] = 0;
		return;
	}
	if(mx[p] > R && mi[p] > R) {
		mi[p] = mx[p] = R;
		addv[p] = R; mulv[p] = 0; catv[p] = 0;
		return;
	}
	int mid = l + r >> 1;
	pushdown(p, l, mid, r);
	update(p << 1, l, mid); update(p << 1 | 1, mid + 1, r);
	pushup(p);
}

int __ans[maxn];

inline void go(int p, int l, int r) {
	if(l == r) {
		__ans[num[l].id] = mi[p];
		return;
	}
	int mid = l + r >> 1;
	pushdown(p, l, mid, r);
	go(p << 1, l, mid); go(p << 1 | 1, mid + 1, r);
}

int main() {
	n = iread(); L = iread(); R = iread();
	for(int i = 1; i <= n; i++) opt[i].id = cread(), opt[i].num = iread();
	m = iread();
	for(int i = 1; i <= m; i++) num[i].num = iread(), num[i].id = i;

	sort(num + 1, num + 1 + m, cmp);
	build(1, 1, m);

	for(int i = 1; i <= n; i++) {
		if(opt[i].id == 1) add(1, 1, m, 1, m, opt[i].num);
		else if(opt[i].id == 2) add(1, 1, m, 1, m, -opt[i].num);
		else if(opt[i].id == 3) mul(1, 1, m, 1, m, opt[i].num);
		else cat(1, 1, m, 1, m, opt[i].num);
		update(1, 1, m);
	}

	go(1, 1, m);
	for(int i = 1; i <= m; i++) printf("%d\n", __ans[i]);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值