洛谷 P5105 不强制在线的动态快速排序 (线段树区间合并)

题目链接:https://www.luogu.org/problemnew/show/P5105

题目大意:中文题,就不解释了-,-

题目思路:首先,对于求解sort(S),我们通过打表可以知道,求解[1,n]区间的(a_{i}^{2}-a_{i-1}^{2})异或和sum(n),有如下的规律:

当n%4 == 0 时,sum(n)等于1;

当n%4 == 1 时,sum(n)等于2×(n-1);

当n%4 == 2 时,sum(n)等于3;

当n%4 == 3时,sum(n)等于2×n。

那么区间 [l,r] 的异或和就是sum(l)\oplus sum(r)\oplus代表异或。

这样就是转化成了只需要求若干个连续区间的异或和异或后再与中间相隔的值异或的结果就行。

这样只需要将各区间端点异或之后,借助线段树的区间更新和区间合并即可。

由于线段树内维护的区间是离散后的区间,所以再求结果的时候还需要将其还原回去,记录一下离散后的两点是否是连续的,如果是连续的,还得将这段区间异或的结果也计算进去。

具体实现看代码:

#include <bits/stdc++.h>
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pb push_back
#define MP make_pair
#define all(v) v.begin(),v.end()
#define IOS ios::sync_with_stdio(false)
#define FIN freopen("in.txt","r",stdin)
#define clr(a) memset(a,0,sizeof(a))
#define fuck(x) cout<<"["<<#x<<" "<<x<<"]\n"
using namespace std;
typedef long long ll;
typedef pair<int, int>pii;
typedef pair<ll, ll>pll;
const int MX = 6e5 + 5;
const int inf = 0x3f3f3f3f;

ll cal(ll x) {
	if (x % 4 == 0) return 1;
	if (x % 4 == 1) return 2ll * (x - 1);
	if (x % 4 == 2) return 3;
	return 2ll * x;
}
ll cal_sum(ll l, ll r) {
	return cal(l)  ^ cal(r);
}
vector<int>has;
int get_id(int x) {
	return lower_bound(all(has), x) - has.begin() + 1;
}
int q;
struct Que {
	int l, r, op;
} Q[MX];
struct Seg_Tree {
	int l, r;
	bool lazy, tag, vis;
	ll left, right;
	ll sum;

	Seg_Tree operator + (const Seg_Tree &A)const {
		Seg_Tree res;
		res.l = l; res.r = A.r;
		res.left = min(left, A.left);
		res.right = max(right, A.right);

		if (right == -inf || A.left == inf)
			res.sum = sum ^ A.sum;
		else
			res.sum = sum ^ A.sum ^ (A.left * A.left - right * right);

		return res;
	}

} T[MX << 2];
void push_up(int rt) {
	bool tag = T[rt].tag, lazy = T[rt].lazy, vis = T[rt].vis;
	T[rt] = T[rt << 1] + T[rt << 1 | 1];
	if (tag)
		T[rt].sum = T[rt << 1].sum ^ T[rt << 1 | 1].sum ^ cal_sum(T[rt << 1].right, T[rt << 1 | 1].left);
	T[rt].tag = tag; T[rt].lazy = lazy; T[rt].vis = vis;
}
void upd(int rt) {
	T[rt].sum = cal_sum(has[T[rt].l - 1], has[T[rt].r - 1]);
	T[rt].left = has[T[rt].l - 1];
	T[rt].right = has[T[rt].r - 1];
}
void push_down(int rt) {
	if (T[rt].lazy) {
		upd(rt << 1); upd(rt << 1 | 1);
		T[rt << 1].lazy = T[rt << 1 | 1].lazy = 1;
		T[rt].lazy = 1;
		T[rt].vis = 1;
	}
}
void build(int l, int r, int rt) {
	T[rt].l = l; T[rt].r = r;
	T[rt].left = inf; T[rt].right = -inf;
	T[rt].lazy = 0;
	T[rt].sum = 0;
	if (l == r) return;
	int m = (l + r) >> 1;
	build(lson); build(rson);
}
void update(int L, int R, int l, int r, int rt) {
	if (L <= l && r <= R) {
		upd(rt);
		T[rt].lazy = 1;
		T[rt].vis = 1;
		return;
	}
	push_down(rt);
	if (T[rt].vis) return;
	int m = (l + r) >> 1;
	if (L > m) update(L, R, rson);
	else if (R <= m) update(L, R, lson);
	else {
		T[rt].tag = 1;
		update(L, R, lson);
		update(L, R, rson);
	}
	push_up(rt);
}

int main() {
	// FIN;
	scanf("%d", &q);
	for (int i = 1; i <= q; i++) {
		scanf("%d", &Q[i].op);
		if (Q[i].op == 1) {
			scanf("%d%d", &Q[i].l, &Q[i].r);
			has.pb(Q[i].l); has.pb(Q[i].r);
		}
	}
	sort(all(has));
	has.erase(unique(all(has)), has.end());
	int n = has.size();
	build(1, n, 1);
	for (int i = 1; i <= q; i++) {
		if (Q[i].op == 2) printf("%lld\n", T[1].sum);
		else update(get_id(Q[i].l), get_id(Q[i].r), 1, n, 1);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值