QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#92827 | #4387. Static Query on Tree | zbceyond | AC ✓ | 357ms | 72288kb | C++20 | 2.8kb | 2023-03-31 00:25:59 | 2023-03-31 00:26:01 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define per(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
const int N=2e5+10;
const int mod=998244353;
#define int long long
int qmi(int a,int b,int mod){//mod别弄错了
int res=1;
for(;b;b>>=1,a=a*a%mod)
if(b&1)res=res*a%mod;
return res;
}
int dfn[N],dep[N],tot,par[N][22],p[N],colA[N],colB[N],colC[N],ans;
vector<int>e[N];
void dfs(int u,int fa){
dfn[u]=++tot,par[u][0]=fa,dep[u]=dep[fa]+1;
for(auto v:e[u])if(v!=fa){
dfs(v,u);
}
}
int lca(int x,int y){
if(dep[x]>dep[y])swap(x,y);
int tmp=dep[y]-dep[x];
for(int i=20;i>=0;i--){
if(tmp>>i&1)y=par[y][i];
}
if(x==y)return x;
for(int i=20;i>=0;i--){
if(par[x][i]!=par[y][i]) {
x = par[x][i], y =par[y][i];
}
}
return par[x][0];
}
vector<int>E[N];
void dfs1(int u,int fa,int ok){
ok+=colC[u];
for(auto v:E[u])if(v!=fa){
dfs1(v,u,ok);
colA[u]|=colA[v];
colB[u]|=colB[v];
}
//cout<<u<<" "<<colA[u]<<" "<<colB[u]<<" "<<ok<<"\n";
if(colA[u] and colB[u] and ok)ans++;
if(colC[u])ok--;
if(colA[u] and colB[u] and ok)ans+=dep[u]-dep[fa]-1;
}
void solve() {
int n, q;
cin >> n >> q;
rep(i, 2, n)cin >> p[i], e[p[i]].push_back(i);
dfs(1,0);
rep(j,1,20)rep(i,1,n)
par[i][j]=par[par[i][j-1]][j-1];
rep(_, 1, q) {
int a, b, c;
cin >> a >> b >> c;
vector<int> A(a), B(b), C(c), vx;
for (auto &i: A)cin >> i, vx.push_back(i),colA[i]=1;
for (auto &i: B)cin >> i, vx.push_back(i),colB[i]=1;
for (auto &i: C)cin >> i, vx.push_back(i),colC[i]=1;
sort(vx.begin(), vx.end(), [&](int &x, int &y) {
return dfn[x] < dfn[y];
});
vector<int> vy;
rep(i, 0, (int) vx.size() - 2) {
vy.push_back(vx[i]);
vy.push_back(lca(vx[i], vx[i + 1]));
}
vy.push_back(vx.back());
sort(vy.begin(), vy.end(), [&](int &x, int &y) {
return dfn[x] < dfn[y];
});
vy.erase(unique(vy.begin(),vy.end()),vy.end());
rep(i,0,(int)vy.size()-2){
int lc=lca(vy[i],vy[i+1]);
E[lc].push_back(vy[i+1]);
E[vy[i+1]].push_back(lc);
}
int rt=vy[0];
dfs1(rt,rt,0);
cout<<ans<<"\n";
for(auto x:vy){
E[x].clear();
colA[x]=colB[x]=colC[x]=0;
}
ans=0;
}
rep(i,1,n)e[i].clear();
tot=0;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int tc = 1;
cin >> tc;
for (int i = 1; i <= tc; i++) {
solve();
}
}
//1
//7 2
//1 1 2 2 3 3
//4 4 3
//4 5 6 7
//4 5 6 7
//2 4 6
//2 1 1
//4 5
//6
//1
详细
Test #1:
score: 100
Accepted
time: 357ms
memory: 72288kb
input:
1 200000 18309 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:
102147 62590 87270 88880 7654 61542 62953 85022 55135 54125 70500 64356 25824 88300 42278 15336 18132 28734 90282 42889 28099 31311 96842 19959 34366 60205 78358 91142 56048 74688 86091 51979 94750 11989 89544 86860 56720 29534 52343 90031 79002 90293 94554 48340 65015 9181 15016 19884 49445 14181 6...
result:
ok 18309 numbers