fhq(7~12AC)求调(split函数的锅?(可能))
查看原帖
fhq(7~12AC)求调(split函数的锅?(可能))
550957
Anonymely楼主2022/11/28 21:26
#include<bits/stdc++.h>
using namespace std;

#define ull unsigned long long
#define eps 0.00000001
#define ll long long
#define orz puts("-1")
#define pf(x) printf("%s",x);
#define pii pair<int,int>
#define pb push_back
#define mk make_pair
#define newline puts("")
#define newspace putchar(' ')
#define lowbit(x) (x&(-x))
#define md(l,r) ((l+r)>>1)
#define lson(p) (p<<1)
#define rson(p) ((p<<1)|1)
#define fi first
#define se second
#define inf 2147483647
#define mod
#define N 200010

namespace fastIO{
	template<typename T> void read(T &x){
		x=0;
		char ch=getchar();T fl=1;
		while(ch<'0'||ch>'9'){if(ch=='-')fl=-1;ch=getchar();};
		while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();};
		x=x*fl;
	}
	template<typename T, typename ...T1> void read(T &x, T1 &...x1){
		read(x);
		read(x1...);
	}
	template<typename T> void write(T x){
		if(x<0){
			x=-x;putchar('-');
		}
		if(x/10)write(x/10);
		putchar(x%10+'0');
	}
	template<typename T> void writer(T x){
		write(x);
		putchar(' ');
	}
	template<typename T> void writen(T x){
		write(x);
		putchar('\n');
	}
}

using namespace fastIO;

struct tree {
	int ls, rs;//左右儿子 
	int key;//权值 
	ll val;//随机值/优先级 
	int siz;//大小 
	ll sum;//和 
	int tag;//标记 
} t[N << 7];

int root[N], cnt;

int addnew(int x) {//新建节点 
	t[++cnt].key = x;
	t[cnt].ls = t[cnt].rs = t[cnt].tag = 0;
	t[cnt].siz = 1;
	t[cnt].sum = x;
	t[cnt].val = rand();
	return cnt;
} 

int getcpy(int x) {//复制 
	int rt = addnew(0);
	t[rt] = t[x];
	return rt;
}

void pushup(int u) {
	t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1;
	t[u].sum = t[t[u].ls].sum + t[t[u].rs].sum + t[u].key;
} 

void pushdown(int u) {
	if (t[u].tag) {
		if (t[u].ls != 0) t[u].ls = getcpy(t[u].ls);
		if (t[u].rs != 0) t[u].rs = getcpy(t[u].rs);
		swap(t[u].ls, t[u].rs); 
		t[t[u].ls].tag ^= 1, t[t[u].rs].tag ^= 1;
		t[u].tag = 0;
	//	pushup(u);
	}
}

pii split(int u, int k) {//分裂 
	if (u == 0) return {0, 0};
	pushdown(u);
	if (t[t[u].ls].siz + 1 <= k) {
		pii tp = split(t[u].rs, k - (t[t[u].ls].siz + 1));
		if (tp.fi != 0) t[u].rs = getcpy(tp.fi), pushup(t[u].rs); 
		else t[u].rs = 0;
	//	if (t[u].rs != 0) 
		//==pushup(t[u].rs);
		pushup(u);
		return {u, tp.se};
	} else {
		pii tp = split(t[u].ls, k);
		if (tp.se != 0) t[u].ls = getcpy(tp.se), pushup(t[u].ls);
		else t[u].ls = 0;
	//	if (t[u].ls != 0) 
	//==	pushup(t[u].ls);
		pushup(u);
		return {tp.fi, u};
	}
}

int merge(int u, int v) {
	if (!u || !v) return u + v;	
	pushdown(u);pushdown(v);
	if (t[u].val > t[v].val) {

		t[u].rs = merge(t[u].rs, v);
		pushup(u);
		return u;
	} else {
		
		t[v].ls = merge(u, t[v].ls);
		pushup(v);
		return v;
	}
}

void inorder(int u) {//不用管,调试用的(中序遍历) 
	if (u == 0) return ;
//	pushdown(u);
	inorder(t[u].ls);
	if (t[u].key == 0) writen(u);
	writer(t[u].key);
	inorder(t[u].rs);
}

int tot, n;

signed main() {
	srand(time(0));
	read(n);
	ll lst = 0;
	while (n--) {
		int v, op;
		read(v, op);
		if (op == 1) {
			ll x, y;
			read(x, y);
			x ^= lst, y ^= lst;		
			pii tp = split(root[v], x);	
		//	cout << tp.fi << ' ' << tp.se << endl;
			//puts("-1");
			root[++tot] = merge(merge(tp.fi, addnew(y)), tp.se);
		//	cout << root[tot] << endl;
		} else if (op == 2) {
			ll x;
			read(x);
			x ^= lst;
			pii tp = split(root[v], x);
			pii opt = split(tp.fi, x - 1);
			root[++tot] = merge(opt.fi, tp.se);
		} else if (op == 3) {
			ll l, r;
			read(l, r);
			l ^= lst, r ^= lst;
			pii tp = split(root[v], r);
			pii opt = split(tp.fi, l - 1);
			t[opt.se].tag ^= 1;
			root[++tot] = merge(merge(opt.fi, opt.se), tp.se);
		} else {
			ll l, r;
			read(l, r);
			l ^= lst, r ^= lst;
			pii tp = split(root[v], r);
			pii opt = split(tp.fi, l - 1);
			lst = t[opt.se].sum;
			writen(lst);
			root[++tot] = merge(merge(opt.fi, opt.se), tp.se);
		}
		//inorder(root[tot]);
		//puts("");
	}
	return 0;
}

/*
10
0 1 0 1
1 1 1 2
2 4 1 2
3 1 2 0
4 4 2 1
5 3 5 7
6 4 5 6
4 1 7 1
8 3 4 6
9 4 4 1
*/ 

2022/11/28 21:26
加载中...