TLE35pts求调
查看原帖
TLE35pts求调
408071
TankYu楼主2022/11/18 17:24
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define D double
#define LD long double
#define LL long long
#define ULL unsigned long long
#define S string
#define fi first
#define se second
#define mp make_pair
using namespace std;

const int maxn = 100010;

char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
const int N = 2E6 + 100;
#define getchar() (p1 == p2 &&(p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)

inline int rd()
{
	int x = 0, f = 1;
	char ch = getchar();
	while (!isdigit(ch))
	{
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch))
		x = x * 10 + (ch ^ 48), ch = getchar();
	return x * f;
}

vector<int> e[maxn];
int n;
int size[maxn];
int son[maxn];
int fa[maxn];
int dep[maxn];
int dfn[maxn];
int mapp[maxn];
int top[maxn];
int tot;
int a[maxn];

struct node
{
	int l;
	int r;
	int sum;
} tree[maxn << 2];
int maxroot;

void pushup(int rt)
{
	tree[rt].sum = tree[rt << 1].sum ^ tree[rt << 1 | 1].sum;
	return;
}

void dfs1(int now, int f, int depth)
{
	fa[now] = f;
	dep[now] = depth;
	size[now] = 1;
	//	dfn[now] = ++tot;
	//	mapp[dfn[now]] = a[now];
	int maxx = -1;
	for (auto i : e[now])
	{
		if (i != f)
		{
			dfs1(i, now, depth + 1);
			if (size[i] > maxx)
			{
				maxx = size[i];
				son[now] = i;
			}
		}
	}
	return;
}

void dfs2(int now, int topp)
{
	dfn[now] = ++tot;
	mapp[dfn[now]] = a[now];
	top[now] = topp;
	if (son[now])
	{
		dfs2(son[now], topp);
	}
	for (auto i : e[now])
	{
		if (i != fa[now] && i != son[now])
		{
			dfs2(i, i);
		}
	}
	return;
}

void build(int rt, int l, int r)
{
	tree[rt].l = l;
	tree[rt].r = r;
	if (l == r)
	{
		tree[rt].sum = mapp[l];
		return;
	}
	int mid = (l + r) / 2;
	build(rt << 1, l, mid);
	build(rt << 1 | 1, mid + 1, r);
	pushup(rt);
}

void update(int rt, int x, int k)
{
	if (tree[rt].l == tree[rt].r)
	{
		tree[rt].sum = k;
		return;
	}
	if ((tree[rt].l + tree[rt].r) / 2 >= x)
	{
		update(rt << 1, x, k);
	}
	else
	{
		update(rt << 1 | 1, x, k);
	}
	pushup(rt);
	return;
}

int query(int rt, int l, int r)
{
	if (tree[rt].l >= l && tree[rt].r <= r)
	{
		return tree[rt].sum;
	}
	int mid = (tree[rt].l + tree[rt].r) / 2;
	int ans = 0;
	if (mid >= l)
	{
		ans ^= query(rt << 1, l, r);
	}
	if (mid < r)
	{
		ans ^= query(rt << 1 | 1, l, r);
	}
	return ans;
}

int path_query(int x, int y)
{
	int ans = 0;
	while (top[x] != top[y])
	{
		if (dep[top[x]] < dep[top[y]])
		{
			swap(x, y);
		}
		//		cerr << x << ' ' << top[x] << '\n';
		ans ^= query(1, dfn[top[x]], dfn[x]);
		x = fa[top[x]];
	}
	//	cout << 1;
	if (dep[x] > dep[y])
	{
		swap(x, y);
		//	cerr << x << ' ' << y << '\n';
	}
	ans ^= query(1, dfn[x], dfn[y]);
	return ans;
}


int main()
{
	freopen("P6098_4.in", "r", stdin);
	freopen("P6098_4.ans", "w", stdout);
	int q;
	n = rd();
	q = rd();
	for (int i = 1; i <= n; i++)
	{
		a[i] = rd();
	}
	for (int i = 1; i < n; i++)
	{
		int a, b;
		a = rd();
		b = rd();
		e[a].push_back(b);
		e[b].push_back(a);
	}
	dfs1(1, 0, 1);
	dfs2(1, 1);
	build(1, 1, n);
	while (q--)
	{
		int op, x, y;
		op = rd();
		x = rd();
		y = rd();
		if (op == 1)
		{
			update(1, dfn[x], y);
		}
		else
		{
			printf("%d\n", path_query(x, y));
			//			cerr << query(1, dfn[3], dfn[3]) << ' ' << query(1, dfn[1], dfn[1]) << ' ' << query(1, dfn[5], dfn[5]) << '\n';
		}
	}
	return 0;
}

2022/11/18 17:24
加载中...