QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#447593 | #8630. 字符树 | zyz07 | 10 | 916ms | 822680kb | C++17 | 4.2kb | 2024-06-18 17:03:08 | 2024-06-18 17:03:09 |
Judging History
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 [l,r]=vec[i];
vector<int> a;
Dec(j,min(r,l+len-2),l){
a.push_back(faw[rk[j]]);
}
for(int v=fa[rk[l]];dep[v]>max(dep[lca],dep[rk[l]]-len);v=fa[v]){
a.push_back(faw[v]);
}
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: 10
Accepted
Test #11:
score: 10
Accepted
time: 150ms
memory: 256220kb
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 1 1 1 1 1 0 0 1 1 1 1 1 0 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 0 0 1 0 0 1 1 0 1 1 0 0 1 0 0 1 1 0 1 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 ...
result:
ok 650 numbers
Test #12:
score: 10
Accepted
time: 916ms
memory: 216040kb
input:
500000 499995 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 12 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 55 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:
113 133 1 134 138 143 35 117 79 1 55 26 98 190 53 82 1 190 141 4 161 4 82 328 193 160 189 124 32 293 133 133 41 119 50 110 194 67 107 30 131 111 24 91 168 139 0 3 162 70 108 140 72 174 60 1 184 157 184 13 138 52 212 193 172 132 147 200 143 51 179 33 184 104 129 71 58 59 6 86 139 79 159 94 0 56 36 55...
result:
ok 499995 numbers
Test #13:
score: 10
Accepted
time: 446ms
memory: 352968kb
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 5 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:
648 90 150 309 2 0 23 5 227 190 52 6 0 14 381 1 177 73 11 1 345 191 3 16 246 0 1 828 11 87 9 1 52 260 5 819 3 55 451 138 3 6 94 45 96 8 1 109 692 285 1 328 1 599 6 0 364 190 1 151 132 13 3 19 32 36 0 263 14 3 199 0 487 0 371 0 345 6 111 85 159 17 422 2 343 218 320 801 183 34 3 968 1 93 164 2 32 119 ...
result:
ok 100000 numbers
Test #14:
score: 10
Accepted
time: 113ms
memory: 201268kb
input:
500000 8 56878 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:
1 1 0 0 1 0 1 1
result:
ok 8 numbers
Test #15:
score: 10
Accepted
time: 530ms
memory: 293624kb
input:
499995 62500 8 1 2 3 4 5 6 7 8 2 10 11 12 13 14 15 12 17 18 19 20 21 6 23 24 25 26 27 28 14 30 31 23 33 34 13 36 37 12 39 30 41 15 43 44 20 46 47 48 1 32 51 52 17 54 55 11 57 58 59 60 61 62 63 30 65 66 67 68 69 70 71 72 73 24 75 76 77 1 79 80 81 82 18 73 85 86 87 88 89 90 91 92 93 94 95 54 97 98 99 ...
output:
1 1 0 1 1 0 5 1 1 2 1 0 1 1 1 1 1 1 1 1 2 1 1 1 1 0 1 1 9 1 0 1 1 0 1 1 1 0 0 1 1 0 0 2 1 2 0 0 0 3 7 1 2 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 0 1 4 1 1 1 0 1 0 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 0 1 0 1 1 1 1 1 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 5 0 6 0 0 2 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 4 0 0 0 0 1 0 0 0 ...
result:
ok 62500 numbers
Test #16:
score: 10
Accepted
time: 905ms
memory: 238752kb
input:
500000 499999 1 1 2 3 4 5 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 3 20 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 32 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 27 90 91 92 93 94 95 96 97 98 ...
output:
7 207 7 66 49 163 53 104 45 266 7 17 95 20 112 181 146 1 0 102 157 97 56 76 53 227 124 20 177 11 33 282 208 63 251 198 3 46 91 29 63 78 93 2 69 95 36 95 10 63 245 61 7 5 12 267 0 18 9 10 190 177 135 138 99 10 12 16 14 18 132 13 16 9 3 303 4 9 201 50 184 134 95 11 130 148 111 124 205 103 141 13 252 1...
result:
ok 499999 numbers
Test #17:
score: 10
Accepted
time: 537ms
memory: 513012kb
input:
500000 55552 9 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:
18 1 2 1 3 3 1 50 0 59 1 1 17 1 0 1 0 17 8 2 1 9 0 0 1 2 8 8 6 2 56 0 6 0 2 4 87 2 2 0 0 14 2 1 45 1 2 17 50 58 38 1 3 9 0 0 0 46 1 9 0 3 12 12 0 51 40 1 1 1 0 15 0 4 59 3 0 1 10 0 7 5 1 1 0 57 131 17 56 0 57 1 7 60 11 87 1 4 1 37 19 57 76 3 0 137 62 0 36 22 0 0 69 0 0 0 0 2 26 16 122 69 35 0 4 0 0 ...
result:
ok 55552 numbers
Test #18:
score: 10
Accepted
time: 392ms
memory: 236192kb
input:
500000 250000 2 1 2 3 4 5 6 1 1 1 10 11 1 1 14 15 16 17 1 1 1 1 1 1 24 1 26 27 1 29 30 31 1 1 34 1 36 37 1 1 1 41 1 1 44 45 1 1 1 1 1 1 1 53 54 1 56 1 1 59 60 1 62 63 1 1 1 1 1 69 70 71 1 1 74 75 76 77 78 1 80 81 1 1 84 85 86 87 88 1 1 1 1 1 94 1 96 1 98 1 1 1 1 1 104 105 1 107 108 109 110 111 1 113...
output:
1 1 0 1 1 0 1 0 1 3 2 1 0 0 0 0 0 0 0 1 1 1 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 0 1 0 1 2 0 1 1 4 0 1 1 0 1 1 2 1 1 0 1 0 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 2 1 0 1 0 0 1 1 0 2 1 0 1 1 0 1 1 0 0 0 1 2 1 2 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 ...
result:
ok 250000 numbers
Test #19:
score: 10
Accepted
time: 504ms
memory: 210432kb
input:
499996 500000 1 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:
997 577 24 996 4 1 1786 1286 603 11 240 809 929 968 4184 272 27 819 92 718 395 371 181 1344 301 957 472 518 1712 174 2419 933 88 379 2 215 18 975 363 96 125 1643 2 44 1126 214 849 0 205 1524 1183 1797 755 121 1742 473 160 170 34 568 518 938 794 1189 9 2257 302 182 124 81 781 1355 146 110 3741 1250 1...
result:
ok 500000 numbers
Test #20:
score: 10
Accepted
time: 685ms
memory: 822680kb
input:
499999 101 4859 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:
1 0 0 0 0 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 0 1 0 1 0 1 1 1 1 1 1 1 1 0 1 0 0 0 1 0 0 1 1 0 0 1 0 1 1 1 0 0 1 0 1 1 1 0 0 1 1 1 1 0 1 0 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 0
result:
ok 101 numbers
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%