跳到正文

题解

题解:P9867 [POI 2021/2022 R2] kon

dream10约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年4月21日
收录
2025年12月12日
更新
2026年11月3日
篇幅
约 7 分钟
题目
P9867

版本

  1. v1当前

Hint

考虑操作树。

Solution

Step 1

我们建立操作树,新加点就作为儿子加入,记录边是 WW 还是 ZZ。

Step 2

原图两个点 x,yx,y 在图上联通,在树上的充要条件是什么?肯定有一条初始的 (u,v)(u,v) 边,然后不断对 u,vu,v 进行 ZZ 操作,在树上就是一条路径,中间是 WW,两边是 ZZ,而且 ZZ 的时间比 WW 晚。

Step 3

如果不调整模型,那么时间的限制就需要再加数据结构。所以考虑再次调整模型,使得充要条件更为简单。

将 uu 点按照 WW 边加入的时间进行拆点,拆成 u1,u2,⋯ ,uku_1,u_2,\cdots,u_k,构成祖先后代关系,然后挂 ZZ 边就在一个地方插入,使得它的祖先下方的 WW 边都是原来的问题能复制的 WW 边。也就是代码里的

代码
G[pos[x]].emplace_back(++tot);
G[pos[x]].emplace_back(++tot);
pos[++n]=tot-1,pos[x]=tot;

此时只有叶子是真实的。

Step 4

计算答案,就是一段 ZZ 向上走,然后经过 WW 边,然后向下走,走到一个叶子(代表原图中的点),如果已经加入了就统计答案。

维护 ZZ 联通块,其他视为虚边。

发现有一部分是到根链,然后向下走一段 WW。 另外一部分是跳到根,然后跳虚边,再向下。

两部分分开统计,使用数据结构维护。

Analysis

复杂度 O(qlog⁡q)O(q \log q)。

C++
#include<bits/stdc++.h>
using namespace std;
template<typename T>
void read(T &a){
    #define gc getchar()
    char c;a=0;int f=1;
    while(!isdigit(c=gc))if(c=='-')f=-1;
    do a=a*10+c-'0';
    while(isdigit(c=gc));
    a*=f;
}
template<typename T>
void write(T a){
    if(a<0)putchar('-'),a=-a;
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
char GC(){
    char c=getchar();
    while(c<=32)c=getchar();
    return c;
}
template<typename T>
void chmin(T &x,T y){if(x>y)x=y;}
template<typename T>
void chmax(T &x,T y){if(x<y)x=y;}
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int> PII;
typedef pair<ll,int> PLI;
typedef __int128 lll;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
namespace GENSOKYO{
int Q;
const int MAXN=2000010;
struct QRY{
    char ch;int x;
}a[MAXN];
vector<int> rts;
int n,tot;
int vfa[MAXN];//virtual father 在新图下标
int pos[MAXN];//原图下标到新图下标
int dfn[MAXN],dfc,siz[MAXN];
int bel[MAXN];
vector<int> G[MAXN];
void dfs(int x,int RT){
    bel[x]=RT;dfn[x]=++dfc;
    siz[x]=1;
    for(int y:G[x]){
        dfs(y,RT);
        siz[x]+=siz[y];
    }
}
struct FWK{
int a[MAXN];
int lowbit(int x){return x&-x;}
void upd(int x,int v){
    for(;x<=tot;x+=lowbit(x))
        a[x]+=v;
}
int qry(int x){
    int res=0;
    for(;x;x-=lowbit(x))
        res+=a[x];
    return res;
}
}t1,t2;
//t1记录子树贡献,t2记录链上连出去的贡献
void activate(int x){
    t1.upd(dfn[pos[x]],1);
    int rt=bel[pos[x]];
    if(vfa[rt]!=0){
        int y=vfa[rt];
        t2.upd(dfn[y],1);
        t2.upd(dfn[y]+siz[y],-1);
    }
}
void main(){
    cin>>Q;
    rts.emplace_back(1);
    rts.emplace_back(2);
    vfa[1]=0;vfa[2]=1;
    pos[1]=1,pos[2]=2;
    tot=n=2;
    for(int iq=1;iq<=Q;++iq){
        cin>>a[iq].ch>>a[iq].x;
        char op=a[iq].ch;int x=a[iq].x;
        if(op=='W'){
            pos[++n]=++tot;
            rts.emplace_back(tot);
            vfa[tot]=pos[x];
        }else if(op=='Z'){
            G[pos[x]].emplace_back(++tot);
            G[pos[x]].emplace_back(++tot);
            pos[++n]=tot-1,pos[x]=tot;
        }
    }
    dfc=0;
    for(auto rt:rts)
        dfs(rt,rt);
    activate(1);
    activate(2);
    int it=2;
    for(int iq=1;iq<=Q;++iq){
        char op=a[iq].ch;int x=a[iq].x;
        if(op=='W'||op=='Z'){
            activate(++it);
        }else{
            int ans=0;
            ans+=t2.qry(dfn[pos[x]]);
            if(vfa[bel[pos[x]]]!=0){
                int y=vfa[bel[pos[x]]];
                ans+=t1.qry(dfn[y]+siz[y]-1)-t1.qry(dfn[y]-1);
            }
            printf("%d\n",ans);
        }
    }
}
}
signed main(){
    int T=1;//cin>>T;
    while(T--)GENSOKYO::main();
    return 0;
}

借鉴 lmh 的思路,代码。

原文