题解:P14834 [THUPC 2026 初赛] 庭中有奇树
挺秒的。
注意到树高
先考虑祖先的贡献。在子孙的基础上,祖先会将子树的一些位全部赋值为
接下来考虑子孙的贡献。注意到若一次对
具体实现时,我们记
使用 unordered_map 维护。
复杂度
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<long,long> pii;
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3fll
ll n,m,Q;
unordered_map<ll,ll> s1,s2;
inline ll getls(ll t){return m&&t>m?-1:t*(t-1)/2+2;}
inline ll getrs(ll t){return m&&t>m?-1:min(t*(t+1)/2+1,n);}
inline ll getfa(ll x)
{
if(x==1) return 0;
ll t=sqrtl(x*2-2);
while(getls(t)>x) t--;
while(getrs(t)<x) t++;
return t;
}
inline ll getsiz(ll x)
{
ll sum=0,l=x,r=x;
while(l!=-1)
{
sum+=r-l+1;
l=getls(l);
if(r>=m) r=n;
else r=getrs(r);
}
return sum;
}
inline ll ask(ll x)
{
ll sum=0,tmp=x;
while(tmp)
{
sum|=s1[tmp];
tmp=getfa(tmp);
}
if(getsiz(x)&1) return s2[x]|sum;
else return (s2[x]|sum)^sum;
}
inline void change(ll x,ll val)
{
while(x)
{
s2[x]^=val;
x=getfa(x);
}
}
inline void solve()
{
cin >> n >> Q;
m=getfa(n);
while(Q--)
{
int opt;
cin >> opt;
if(opt==1)
{
ll x,y;
cin >> x >> y;
ll t=ask(x);
s1[x]|=y;
t^=ask(x);
change(x,t);
}
else
{
ll x;
cin >> x;
cout << ask(x) << '\n';
}
}
}
int main()
{
// int t=clock();
// freopen("gdx.in","r",stdin);
// freopen("gdx.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// cin >> Type;
// cin >> T;
// while(T--)
solve();
// cout << clock()-t << '\n';
return 0;
}