fhqtreap 0pts 求助,样例可过qwq 悬关
查看原帖
fhqtreap 0pts 求助,样例可过qwq 悬关
706737
sundyLIUXY楼主2023/7/24 21:33
#include <bits/stdc++.h>
#define int long long
using namespace std;

int n, m, idx, rt;
struct TREAP {
	int pos, size, val, lson, rson, flag;
} t[100010];

void pushup(int u) {
	t[u].size = t[t[u].lson].size+t[t[u].rson].size+1;
}
int build(int x) {
	t[++idx].val = x, t[idx].size = 1, t[idx].pos = rand();
	return idx;
}
void pushdown(int u) {
	swap(t[u].lson, t[u].rson);
	if(t[u].lson) t[t[u].lson].flag ^= 1;
	if(t[u].rson) t[t[u].rson].flag ^= 1;
	t[u].flag = 0;
}
int merge(int u, int v) {
	if(!u || !v) return u+v;
	if(t[u].pos < t[v].pos) {
		if(t[u].flag) pushdown(u);
		t[u].rson = merge(t[u].rson, v);
		pushup(u);
		return u;
	}
	if(t[v].flag) pushdown(v);
	t[v].lson = merge(t[v].lson, u);
	pushup(v);
	return v;
}
void split(int x, int y, int &u, int &v) {
	if(!x) {
		u = v = 0;
		return;
	}
	if(t[x].flag) pushdown(x);
	if(t[t[x].lson].size < y)
		u = x, split(t[x].rson, y-t[t[x].lson].size-1, t[x].rson, v);
	else v = x, split(t[x].lson, y, u, t[x].lson);
	pushup(x);
}
void dfs(int u) {
	if(!u) return;
	if(t[u].flag) pushdown(u);
	dfs(t[u].lson);
	printf("%lld ", t[u].val);
	dfs(t[u].rson);
}

signed main() {
	scanf("%lld%lld", &n, &m);
	for(int i = 1; i <= n; i++) 
		rt = merge(rt, build(i));
	for(int i = 1; i <= m; i++) {
		int l, r, a = 0, b = 0, c = 0;
		scanf("%lld%lld", &l, &r);
		split(rt, l-1, a, b), split(b, r-l+1, b, c);
		t[b].flag ^= 1, rt = merge(a, merge(b, c));
	}
	dfs(rt);
	
	return 0;
}
2023/7/24 21:33
加载中...