FHQtreap样例过了,全WA,悬关
查看原帖
FHQtreap样例过了,全WA,悬关
716260
SegmentTree_楼主2024/1/20 22:06
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5+5;
struct node{
	int ls, rs;
	int key, pri, siz;
	int lazy;
}treap[N];
int tsiz = 0, root = 0;
void update(int rt){
	treap[rt].siz = treap[treap[rt].ls].siz + treap[treap[rt].rs].siz + 1;
}
void pushdown(int x){
	if (treap[x].lazy){
		swap(treap[x].ls, treap[x].rs);
		if (treap[x].ls) treap[treap[x].ls].lazy ^= 1;
		if (treap[x].rs) treap[treap[x].rs].lazy ^= 1;
		treap[x].lazy = 0;	
	}
}
void newnode(int x){
	++tsiz;
	treap[tsiz].siz = 1;
	treap[tsiz].ls = treap[tsiz].rs = 0;
	treap[tsiz].key = x;
	treap[tsiz].pri = rand();
}
void split(int rt, int x, int &l, int &r){
	if (rt == 0){
		l = r = 0;
		return;
	}
	pushdown(rt);
	if (treap[rt].key <= x) l = rt, split(treap[rt].rs, x, treap[rt].rs, r);
	else r = rt, split(treap[rt].ls, x, l, treap[rt].ls);
	update(rt);
}
int merge(int l, int r){
	if (l == 0 || r == 0) return l + r;
	if (treap[l].pri > treap[r].pri){
		pushdown(l);
		treap[l].rs = merge(treap[l].rs, r);
		update(l);
		return l;
	}
	else{
		pushdown(r);
		treap[r].ls = merge(l, treap[r].ls);
		update(r);
		return r;
	}
}
int insert(int x){
	int l, r;
	split(root, x, l, r);
	newnode(x);
	root = merge(merge(l, tsiz), r);
	return tsiz;
}
void print(int x){
	if (!x) return;
	pushdown(x);
	print(treap[x].ls);
	cout << treap[x].key << ' ';
	print(treap[x].rs);
}
int n, q;
int main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	srand(time(NULL));
	cin >> n >> q;
	for (int i = 1;i <= n;i++){
		newnode(i);
		root = merge(root, tsiz);
	}
	while (q--){
		int l, r, a, b, c;
		cin >> l >> r;
		split(root, l - 1, a, b);
		split(b, r - l + 1, b, c);
		treap[b].lazy ^= 1;
		root = merge(a, merge(b, c));
	}
	print(root);
	return 0;
}
2024/1/20 22:06
加载中...