跳到正文

题解

[THUPC 2026 初赛] 庭中有奇树

_H17_约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年2月5日
收录
2026年2月5日
更新
2026年11月11日
篇幅
约 5 分钟
题目
P14834

版本

  1. v1当前

前言

卡 gp_hash_table 的出题人是【数据删除】。

题目分析

发现树的高度很小,考虑暴力修改。

对于一个点维护子树内不考虑祖先的操作的值和一个懒标记。由于对一个子树异或了 xx 会对根链都异或 xx,考虑直接暴力更新。查询直接暴力或祖先的标记就行。

所以现在问题是需要知道某次操作对这个子树的异或的影响。如果这个子树原先值是 xx,现在或上了 yy。如果 yy 的某一位是 11,那么这一位的异或会变成子树大小的最后一位;否则这一位不变。注意:要根链异或的数是变化量。

还有一个小问题,直接从根往上异或这个变化量不太对,因为我们维护的时候只考虑了子树内的操作,所以此时子树内的值不是真正正确的。所以对于一个操作数 xx,如果 xx 的某一位是 11 并且它祖先的某次操作之中把这一位已经赋为 11 了,那就不再考虑了。这样就避免了和祖先操作产生错误贡献。

注意存的时候手写哈希,不然会被卡。时间复杂度 O(qlog⁡log⁡n)O(q\log \log n)。

代码

C++
#include<bits/stdc++.h>
#include<bits/extc++.h>
#define int long long
#define ALL(x) (x).begin(),(x).end()
using namespace std;
using namespace __gnu_pbds;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
struct Hsh{
	unsigned int operator()(unsigned int x)const{
		static const unsigned int s1=rnd(),s2=rnd(),s3=rnd();
		x+=s1;
		x=(x^(x>>33))*s2;
		x=(x^(x>>30))*s3;
		return x;
	}
};
int n,q;
vector<tuple<int,int,int> >queries;
__inline int getfa(int x){
    if(x<=1)
        return 0;
    int fath=sqrt(2*x-2);
    if(fath*(fath-1)/2+2<=x&&x<=fath*(fath+1)/2+1)
        return fath;
    else
        return fath+1;
}
__inline int qsiz(int id){
    int ret(1);
    __int128 l(id),r(id);
    while(r<=n){
        l=l*(l-1)/2+2,r=r*(r+1)/2+1;
        ret+=max(min(((__int128)(n)),r)-l+1,((__int128)(0)));
    }
    return ret;
}
struct Tree{
    int fa,val,tag,siz;
    Tree(){}
    Tree(int id):fa(getfa(id)),val(0),tag(0),siz(qsiz(id)){}
};
gp_hash_table<int,Tree,Hsh>tr;
__inline int qtag(int x){
    int ret(0);
    while(x)
        ret|=tr[x].tag,x=tr[x].fa;
    return ret;
}
__inline void modify(int x,int val){
    val&=(~qtag(x));
    int v(((tr[x].val&(~val))+(val*(tr[x].siz&1)))^tr[x].val);
    tr[x].tag|=val;
    while((x=tr[x].fa))
        tr[x].val^=v;
    return;
}
__inline int query(int x){
    int tag(qtag(x));
    return(tr[x].val&(~tag))+(tag*(tr[x].siz&1));
}
string program(){
    cin.tie(nullptr)->ios::sync_with_stdio(0);
    cin>>n>>q;
    for(int i(1),o,x,c;i<=q;i++){
        cin>>o>>x;
        if(o==1)
            cin>>c;
        queries.push_back(make_tuple(o,x,c));
    }
    for(auto[H17,p,_H17_]:queries)
        while(p&&(!tr[p].fa)){
            tr[p]=Tree(p);
            p=tr[p].fa;
        }
    for(auto[o,x,c]:queries){
        if(o==1)
            modify(x,c);
        else
            println("{}",query(x));
    }
    return"H17";
}
string H17(program());
signed main(){
    return 0;
}
原文