跳到正文

题解

题解:P14834 [THUPC 2026 初赛] 庭中有奇树

LittleAcbg约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年12月23日
收录
2026年4月23日
更新
2026年11月14日
篇幅
约 7 分钟
题目
P14834

版本

  1. v2当前
  2. v1

考场上和队友 @Synthesize 一起场切了。

树形态分析

先看一张示意图。

节点 nn 的最小儿子编号应为 2+1+2+3+⋯+(n−1)=n(n−1)2+22+1+2+3+\dots+(n-1)=\dfrac{n(n-1)}2+2,最大儿子编号是 1+1+2+3+⋯+n=n(n+1)2+11+1+2+3+\dots+n=\dfrac{n(n+1)}2+1。分别记为 lsn,rsnls_n,rs_n。

接着观察一个性质:树的层数非常小。

深度最大节点编号
1111
22rs1=2rs_1=2
33rs2=4rs_2=4
44rs4=11rs_4=11
55rs11=67rs_{11}=67
66rs67=2279rs_{67}=2279

以此类推,得到下表:

深度最大节点编号
7725980612598061
88≈3.374×1012\approx3.374\times10^{12}
99>1024>10^{24}

因此题目中的奇树最多 99 层。

算法

考虑把一个节点的子树节点编号拆分成若干连续区间。注意到子树每一层都是一个连续区间,所以每个子树可以拆成至多 99 个区间,例如 33 的子树可以拆成 [3,3],[5,7],[12,29],…[3,3],[5,7],[12,29],\dots,直到超过 nn。

现在只需要做如下问题:

给定序列 aa,维护区间按位或和区间异或和查询。

这个不需要拆位,可以直接用线段树维护。操作时直接在需要操作的位使用 bitmask,按区间长度奇偶性修改异或和即可。

其实是队友写的线段树我也不知道细节。

为了减少空间损耗,我离散化了,需要注意离散化细节,不离散化不知道空间够不够。

代码实现

考场上写了一段时间,调了几下还卡了会空间,封榜后一发过了。

C++
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 9e6+9;
const int Q = 1e6+9;
ll n,q,p,u,c,nOp,pos[Q],nQ,po[N],nPo;
struct Operation {ll l,r,c;} op[N];
namespace Seg_Tree {
	#define int long long
	int tag[18000005], val[18000005];
	void pushdown(int p, int l, int r) {
		if (!tag[p]) return;
		if((po[(l + r) / 2 + 1] - po[l]) % 2 == 0) {
			val[p << 1] = (val[p << 1] ^ (val[p << 1] & tag[p]));
		}
		else {
			val[p << 1] = ((val[p << 1] ^ (val[p << 1] & tag[p])) | tag[p]);
		}
		if((po[r + 1] - po[(l + r) / 2 + 1]) % 2 == 0) {
			val[p << 1 | 1] = (val[p << 1 | 1] ^ (val[p << 1 | 1] & tag[p]));
		}
		else {
			val[p << 1 | 1] = ((val[p << 1 | 1] ^ (val[p << 1 | 1] & tag[p])) | tag[p]);
		}
		tag[p << 1] |= tag[p];
		tag[p << 1 | 1] |= tag[p];
		tag[p] = 0;
	}
	void modify(int p, int l, int r, int cl, int cr, int c) {
		if(l >= cl && r <= cr) {
			if((po[r + 1] - po[l]) % 2 == 0) {
				val[p] = (val[p] ^ (val[p] & c));
			}
			else {
				val[p] = ((val[p] ^ (val[p] & c)) | c);
			}
			tag[p] |= c;
			return;
		}
		pushdown(p, l, r);
		int mid = (l + r) / 2;
		if(mid >= cl) {
			modify(p << 1, l, mid, cl, cr, c);
		}
		if(mid + 1 <= cr) {
			modify(p << 1 | 1, mid + 1, r, cl, cr, c);
		}
		val[p] = val[p << 1] ^ val[p << 1 | 1];
	}
	int query(int p, int l, int r, int cl, int cr) {
		if(l >= cl && r <= cr) {
			return val[p];
		}
		pushdown(p, l, r);
		int mid = (l + r) / 2, res = 0;
		if(mid >= cl) {
			res ^= query(p << 1, l, mid, cl, cr);
		}
		if(mid + 1 <= cr) {
			res ^= query(p << 1 | 1, mid + 1, r, cl, cr);
		}
		return res;
	}
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> q;
    while (q--)
    {
    	cin >> p >> u;
    	if (p == 2) c = 0;
    	else cin >> c;
    	__int128_t l = u,r = u;
		while (l <= n)
		{
			op[++nOp] = (Operation){l, r + 1, c};
			po[++nPo] = l;
			po[++nPo] = r + 1;
			l = l * (l - 1) / 2 + 2;
			r = r * (r + 1) / 2 + 1;
			r = min(r, (__int128_t)n);
		}
		if (!c) pos[++nQ] = nOp;
	}
	sort(po + 1, po + nPo + 1);
	nPo = unique(po + 1, po + nPo + 1) - po - 1;
	ll ans = 0;
	for (ll i = 1,j = 1; i <= nOp; ++i)
	{
		op[i].l = lower_bound(po + 1, po + nPo + 1, op[i].l) - po;
		op[i].r = lower_bound(po + 1, po + nPo + 1, op[i].r) - po - 1;
		if (op[i].c) Seg_Tree::modify(1, 1, nPo, op[i].l, op[i].r, op[i].c);
		else ans ^= Seg_Tree::query(1, 1, nPo, op[i].l, op[i].r);
		if (j <= nQ && i == pos[j]) cout << ans << endl,ans = 0,++j;
	}
    return 0;
}
原文