求助LCT
查看原帖
求助LCT
701221
Chr0n1CleC楼主2022/10/4 14:26
#include<stdio.h>
#define N 500009

static int val[N], ch[N][2], fa[N];

static bool lazy[N];

int n;

static bool lazy1[N];

static long long sum[N], lastans;

inline void swap(register int& a, register int& b)
{
	a ^= b, b ^= a, a ^= b;
}

inline void push(register int x)
{
	if (x)
		swap(ch[x][0], ch[x][1]), lazy[x] ^= 1;
}

inline void pushdown(register int x)
{
	if (lazy[x])
	{
		push(ch[x][0]);
		push(ch[x][1]);
		lazy[x] = 0;
	}
	if (lazy1[x])
	{
		if (ch[x][0])
			lazy1[ch[x][0]] = 1, val[ch[x][0]] = 0, sum[ch[x][0]] = 0;
		if (ch[x][1])
			lazy1[ch[x][1]] = 1, val[ch[x][1]] = 0, sum[ch[x][1]] = 0;
		lazy1[x] = 0;
	}
}

inline void updata(register int x)
{
	sum[x] = sum[ch[x][0]] + sum[ch[x][1]] + val[x];
}

inline bool isRoot(register int x)
{
	return x != ch[fa[x]][1] && x != ch[fa[x]][0];
}

inline bool get(register int x)
{
	return x == ch[fa[x]][1];
}

inline void rorate(register int x)
{
	int y = fa[x], z = fa[y], f = get(x);
	if (!isRoot(y))
		ch[z][get(y)] = x;
	ch[y][f] = ch[x][!f], fa[ch[x][!f]] = y;
	ch[x][!f] = y, fa[y] = x, fa[x] = z;
	updata(y), updata(x);
}

inline void splay(register int x)
{
	register int y = x, top = 0;
	static int s[N];
	while (!isRoot(y))
		s[++ top] = y, y = fa[y];
	while (top)
		pushdown(s[top --]);
	while (!isRoot(x))
	{
		y = fa[x];
		if (!isRoot(y))
			rorate(get(y) == get(x) ? x : y);
		rorate(x);
	}
	updata(x);
}

inline void access(register int x)
{
	for (register int y = 0;x;y = x, x = fa[x])
		splay(x), ch[x][1] = y, updata(x);
}

inline void makeroot(register int x)
{
	access(x), splay(x), push(x);
}

inline int findroot(register int x)
{
	access(x), splay(x);
	while (ch[x][0])
		pushdown(x), x = ch[x][0];
	splay(x);
	return x;
}

inline void link(register int x, register int y)
{
	if (x != y)
		makeroot(x), fa[x] = y;
}

inline void split(register int x, register int y)
{
	if (findroot(x) == findroot(y))
		makeroot(x), access(y), splay(y), lastans = sum[x], lazy1[x] = 1, sum[x] = 0, val[x] = 0;
	else
		lastans = 0;
}

inline void decode(register int& x)
{
	x ^= lastans % n;
	if (x > n)
		x %= n;
	if (!x)
		x = 1;
}

int main()
{
	register int m;
	scanf("%d%d", &n, &m);
	register int opt, x, y;
	while (m --)
	{
		scanf("%d%d%d", &opt, &x, &y);
		decode(x), decode(y);
		if (opt == 1)
			link(x, y);
		if (opt == 2)
			splay(x), val[x] += y;
		if (opt == 3)
			split(x, y), printf("%lld\n", lastans);
	}
	
	return 0;
}
2022/10/4 14:26
加载中...