QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#447586#8630. 字符树zyz070 133ms256320kbC++174.2kb2024-06-18 16:52:392024-06-18 16:52:40

Judging History

你现在查看的是最新测评结果

  • [2024-06-18 16:52:40]
  • 评测
  • 测评结果:0
  • 用时:133ms
  • 内存:256320kb
  • [2024-06-18 16:52:39]
  • 提交

answer

#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
#define For(Ti,Ta,Tb) for(auto Ti=(Ta);Ti<=(Tb);++Ti)
#define Dec(Ti,Ta,Tb) for(auto Ti=(Ta);Ti>=(Tb);--Ti)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
using ll=long long;
using ull=unsigned long long;
const int N=5e5+5,Base=13331;
struct HashSeq{
	vector<ull> pw,hsh;
	HashSeq(const vector<int>& a):pw(a.size()+1),hsh(a.size()+1){
		pw[0]=1;
		For(i,1,int(a.size())){
			pw[i]=pw[i-1]*Base;
		}
		For(i,0,int(a.size())-1){
			hsh[i+1]=hsh[i]*Base+a[i]+1;
		}
	}
	ull substr(int l,int r){
		return hsh[r+1]-hsh[l]*pw[r-l+1];
	}
};
int n,m,len,fa[N],faw[N],dep[N],siz[N],hson[N];
vector<int> e[N];
void dfs(int u){
	siz[u]=1;
	for(int v:e[u]){
		dep[v]=dep[u]+1;
		dfs(v);
		siz[u]+=siz[v];
		if(siz[v]>siz[hson[u]]){
			hson[u]=v;
		}
	}
}
int rk[N],dfn[N],dfx,top[N],down[N];
void dfs2(int u,int t){
	rk[dfn[u]=++dfx]=u;
	top[u]=t;
	down[t]=u;
	if(hson[u]){
		dfs2(hson[u],t);
	}
	for(int v:e[u]){
		if(v!=hson[u]){
			dfs2(v,v);
		}
	}
}
int lca(int u,int v){
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]]){
			swap(u,v);
		}
		u=fa[top[u]];
	}
	return dep[u]<dep[v]?u:v;
}
int kth_anc(int u,int k){
	while(1){
		int x=dep[u]-dep[top[u]]+1;
		if(k>=x){
			k-=x;
			u=fa[top[u]];
		}else{
			return rk[dfn[u]-k];
		}
	}
}
int match(const vector<int>& vec,ull x){
	HashSeq hsh(vec);
	int ans=0;
	For(i,0,int(vec.size())-len){
		ans+=(hsh.substr(i,i+len-1)==x);
	}
	return ans;
}
ull get_hash(const string& s){
	ull x=0;
	For(i,0,len-1){
		x=x*Base+s[i]-'0'+1;
	}
	return x;
}
struct BIT{
	__gnu_pbds::gp_hash_table<ull,int> t[N];
	void add(int p,ull x,int w){
		for(;p<=n;p+=p&-p){
			t[p][x]+=w;
		}
	}
	int query(int p,ull x){
		int res=0;
		for(;p;p&=p-1){
			auto it=t[p].find(x);
			res+=(it!=t[p].end()?it->second:0);
		}
		return res;
	}
}bit;
int solve(int u,int lca,const string& s){
	if(dep[u]-dep[lca]<len){
		return 0;
	}
	// debug("solve %d %d %s",u,lca,s.c_str());
	int ans=0;
	ull x=get_hash(s);
	vector<pair<int,int>> vec;
	{
		int x=u;
		while(top[x]!=top[lca]){
			vec.emplace_back(dfn[top[x]],dfn[x]);
			x=fa[top[x]];
		}
		if(x!=lca){
			vec.emplace_back(dfn[lca]+1,dfn[x]);
		}
	}
	For(i,0,int(vec.size())-2){
		auto [l1,r1]=vec[i];
		auto [l2,r2]=vec[i+1];
		vector<int> a;
		Dec(j,min(r1,l1+len-2),l1){
			a.push_back(faw[rk[j]]);
		}
		Dec(j,r2,max(l2,r2-len+2)){
			a.push_back(faw[rk[j]]);
		}
		ans+=match(a,x);
	}
	ans+=bit.query(dfn[u],x)-bit.query(dfn[kth_anc(u,dep[u]-dep[lca]-len+1)],x);
	// debug("=%d\n",ans);
	return ans;
}
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	// assert(freopen("data.in","r",stdin));
	// assert(freopen("data.out","w",stdout));
	cin>>n>>m>>len;
	For(i,2,n){
		cin>>fa[i];
		e[fa[i]].push_back(i);
	}
	{
		string s;
		cin>>s;
		For(i,2,n){
			faw[i]=s[i-2]-'0';
			// debug("%d %d %d\n",i,fa[i],faw[i]);
		}
	}
	dfs(1);
	dfs2(1,1);
	For(u,1,n){
		if(down[top[u]]==u){
			// debug("down=%d,top=%d\n",u,top[u]);
			vector<int> a;
			for(int v=0;v!=top[u];){
				v=(v?fa[v]:u);
				a.push_back(faw[v]);
			}
			HashSeq hsh(a);
			for(int v=u;v&&dep[v]-dep[top[u]]+1>=len;v=fa[v]){
				ull val=hsh.substr(dep[u]-dep[v],dep[u]-dep[v]+len-1);
				// debug("%d %llu\n",v,val);
				bit.add(dfn[v],val,1);
				bit.add(dfn[v]+siz[v],val,-1);
			}
		}
	}
	// debug("done\n");
	For($,1,m){
		int op,u,v;
		string s;
		cin>>op>>u>>v>>s;
		if(op==1){
			assert(0);
		}else{
			if(dep[u]<dep[v]){
				reverse(range(s));
				swap(u,v);
			}
			int lca=::lca(u,v);
			if(dep[u]+dep[v]-2*dep[lca]<len){
				cout<<"0\n";
				continue;
			}
			int ans=solve(u,lca,s);
			if(lca!=v){
				ans+=solve(v,lca,string(s.rbegin(),s.rend()));
				int u_=kth_anc(u,max(0,dep[u]-dep[lca]-(len-1)));
				int v_=kth_anc(v,max(0,dep[v]-dep[lca]-(len-1)));
				vector<int> vec,vec_;
				for(;u_!=lca;u_=fa[u_]){
					vec.push_back(faw[u_]);
				}
				for(;v_!=lca;v_=fa[v_]){
					vec_.push_back(faw[v_]);
				}
				reverse(range(vec_));
				vec.insert(vec.end(),range(vec_));
				ans+=match(vec,get_hash(s));
			}
			cout<<ans<<'\n';
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 0
Runtime Error

input:

250 250 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 30 67 68 8 70 16 72 3 74 75 32 77 75 31 80 81 65 83 30 19 49 4 1 89 57 91 92 43 94 95 96 85 51 32 100 8...

output:


result:


Subtask #2:

score: 0
Wrong Answer

Test #11:

score: 0
Wrong Answer
time: 133ms
memory: 256320kb

input:

500000 650 769
1 2 3 4 5 6 7 8 9 10 11 12 7 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9...

output:

1
0
1
1
1
0
0
0
1
1
1
1
1
0
1
0
1
1
1
1
0
1
1
1
1
1
0
1
1
1
1
1
1
0
1
1
0
1
0
0
1
1
0
0
0
0
1
1
0
0
0
0
0
1
1
1
0
0
0
1
1
0
1
1
1
0
1
1
0
1
1
0
1
1
1
1
1
0
0
0
0
0
1
1
0
0
1
0
0
1
0
0
1
1
0
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 2nd numbers differ - expected: '1', found: '0'

Subtask #3:

score: 0
Runtime Error

Test #21:

score: 0
Runtime Error

input:

500000 499998 1
1 2 3 4 5 6 7 8 1 1 11 12 13 14 15 16 1 1 19 20 21 22 23 24 1 26 1 28 29 30 1 32 33 34 35 1 37 38 39 40 41 42 43 1 45 46 47 48 49 50 1 52 53 54 55 56 57 1 1 60 61 62 63 64 65 66 67 68 69 1 71 1 73 1 75 76 77 78 79 80 81 82 83 1 1 86 87 88 89 1 91 92 93 94 95 96 97 98 99 100 101 102 1...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Runtime Error

Test #41:

score: 0
Runtime Error

input:

499997 9 40060
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 ...

output:


result:


Subtask #6:

score: 0
Runtime Error

Test #51:

score: 0
Runtime Error

input:

500000 62500 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 ...

output:


result:


Subtask #7:

score: 0
Runtime Error

Test #61:

score: 0
Runtime Error

input:

500000 100000 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98...

output:


result:


Subtask #8:

score: 0
Runtime Error

Test #71:

score: 0
Runtime Error

input:

25000 2500 10
1 2 3 4 5 6 7 8 9 10 1 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99...

output:


result:


Subtask #9:

score: 0
Skipped

Dependency #8:

0%

Subtask #10:

score: 0
Skipped

Dependency #1:

0%