跳到正文

题解

题解 P5537 【【XR-3】系统设计】

z7z_Eta约 8 分钟
原文
文章信息与版本

文章信息

发布
2019年9月24日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 8 分钟
题目
P5537

版本

  1. v2当前
  2. v1

题面:

一棵 nn 个点的有根树,

一个长度为 mm 的序列 aa,

接下来需要实现 qq 个操作。

  1. 1 x l r 从树的节点 xx,依次遍历序列 l∼rl∼r。每个数字 aia_i 代表树上一次跳转,即从当前节点走向它的编号第 aia_i 小的儿子。如果某一时刻无法跳转,输出这个点的编号,停止遍历。
  2. 2 t k 单点修改,ata_t 修改为 kk。

打开标签,发现里面惊奇的出现了

字符串HASH

HASH真是个神奇的算法啊,,

这道题是用到了哈希的诸多性质

一个简单的想法,我们如果可以直接匹配序列上的hash值和树上跳转的hash值,直到无法匹配就好了。

这样可以二分跳转用到的序列的长度,如果可以在以xx为根的子树中找到一条链和此时序列hash值相同,就表示从xx跳转到了这条链的另一端。

怎么快速查询树上的hash值呢?先考虑所有的x=1x=1的情况,因为树的结构是没有修改的,所以我们可以在dfs过程中排序子节点的编号,预处理出从父节点到子节点的数字,合并得到从1号节点到所有节点的hash值。

再来,hash是有可减性质的,其实x≠1x\neq1的情况就是判断节点的hash值是否等于x的hash值+序列的hash值。

序列的hash值就很好维护了,因为hash值可以快速合并,所以序列上的hash值用线段树来维护单点修改和区间查询就好了。

时间复杂度O(nlog⁡2n)O(n\log^2n),看到巨佬还有树链剖分的的题解有兴趣的自行搜索吧

code:

C++
// luogu-judger-enable-o2
// SeptEtavioxy
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<cmath>
#define re register
#define ll long long
#define il inline
#define rep(i,s,t) for(re int i=(s);i<=(t);i++)
#define rev_rep(i,s,t) for(re int i=(s);i>=(t);i--)
#define each(i,u) for(int i=head[u];i;i=bow[i].nxt)
#define pt(ch) putchar(ch)
#define pti(x) printf("%d",x)
#define ptll(x) printf("%lld",x)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
// c_ints
il int ci(){
	re char ch;int f=1;
	while(!isdigit(ch=getchar()))f= ch=='-'?-1:1 ;
	re int x= ch^'0';
	while(isdigit(ch=getchar()))x=(x*10)+(ch^'0');
	return f*x;
}
il char cc(){
	re char ch;
	while(isspace(ch=getchar()));
	return ch;
}

enum{N=500024};

#define Ull unsigned ll
class Edge{
public:
	int nxt,to;
	il void operator()(int a,int b){
		nxt=a, to=b;
	}
}bow[N];
int head[N],depth[N];

const int base = 1e9+7; //base开大到n以上
Ull num[N],powb[N];

void dfs(int u){
	static int d[N];
	int tot= 0;
	each(i,u) d[++tot]= bow[i].to;
	sort(d+1,d+tot+1);
	rep(i,1,tot) num[d[i]]= num[u]*base+i;
	each(i,u){
		int v= bow[i].to;
		depth[v]= depth[u]+1;
		dfs(v);
	}
}

struct cmp1{
	bool operator()(const int a,const int b){
		return depth[a]==depth[b] ? num[a]<num[b]:depth[a]<depth[b] ;
	}
};
Ull tbl[N],*op[N];
//这里是内存池和动态数组
int c[N],mxdep;
il void item(int n){
	rep(i,1,n) c[i]=i;//排序标号
	sort(c+1,c+n+1,cmp1());
	rep(i,1,n){
		if( depth[c[i]]!=mxdep ){
			op[++mxdep]=tbl+i;
		}
		tbl[i]= num[c[i]];
	}
	op[mxdep+1]= tbl+n+1;
}

//zkw线段树
class node{
public: int len; Ull num;
};
il node operator+(const node&a,const node&b){
	return (node){
		a.len+b.len,
		a.num*powb[b.len]+b.num
	};
}
class SEG{
public:
	int n,a[N];
	#define lc(x) ((x)<<1)
	#define rc(x) ((x)<<1|1)
	il void init(){
		m= (1<<int(log2(n+1)+1));
		rep(i,0,m-1) tr[m+i].len= 1;
		rep(i,1,n) tr[m+i].num= a[i];
		rev_rep(i,m,1) tr[i]= tr[lc(i)]+tr[rc(i)];
	}
	il node query(int l,int r){
		node L= (node){0,0}, R= (node){0,0};
		for(int s=m+l-1,t=m+r+1;s^t^1;s>>=1,t>>=1){
			if( (s&1)==0 ) L= L+tr[s^1];
			if( (t&1)==1 ) R= tr[t^1]+R;
		}
		return L+R;
	}
	il void modify(int x,int k){
		re int i= m+x;
		tr[i].num= k;
		while( i>>=1 ) update(i);
	}
private:
	int m;
	node tr[1<<20];
	il void update(int x){
		tr[x]= tr[lc(x)]+tr[rc(x)];
	}
}seg;

int main(){
	int n=ci(), m=ci(), q=ci();
	{//预处理幂
		powb[0]= 1;
		int M=max(n,m);
		rep(i,1,M) powb[i]=powb[i-1]*base;
	}
	rep(i,1,n){//邻接表存边
		int x=ci();
		bow[i](head[x],i);
		head[x]= i;
	}

	dfs(0);
	item(n);

	seg.n= m;
	rep(i,1,m) seg.a[i]=ci();
	seg.init();

	while( q-- ){
		if( cc()=='1' )
		{//查询
			int x=ci(), l=ci()-1, r=ci();
			int s=l+1;
			node e;
			Ull numx;
			int dep,k;
			while( l<r ){//二分
				int w= (l+r+1)>>1;
				e= seg.query(s,w);
				numx= num[x]*powb[e.len]+e.num;
				dep= depth[x]+w-s+1;
				if( dep<=mxdep && tbl[
						k=lower_bound(op[dep],op[dep+1],numx)-tbl
					]==numx ) l=w;
				else r=w-1;
			}
			e= seg.query(s,l);
			numx= num[x]*powb[e.len]+e.num;
			dep= depth[x]+l-s+1;
			k= lower_bound(op[dep],op[dep+1],numx)-tbl;
			pti(c[k]),pt('\n');
		}
		else
		{//修改
			int x=ci(), k=ci();
			seg.modify(x,k);
		}
	}
	return 0;
}

noip rp++noip\ rp++

原文
存档评论14 条抓取于