zcmimi's blog
查看原题

点击跳转

一看就是树剖

先设所有情报员开始搜索情报的时间(我们假设给这个情报员赋值)是q,即询问个数

对于第i个询问如果是查询,则查找值比i-c小的情报员有多少个(树剖)

参与传递的人数显然是d_x+d_y-2 \times d_{LCA(x,y)} + 1

如果是让某个情报员开始搜集,则把这个情报员赋值为i

解法一:

套个主席树应该就可以了(请参见楼下)

解法二:

离线按照权值排序添加,然后用线段树或树状数组统计就可以了)

#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define il __inline__ __attribute__ ((always_inline))
#define Fur(i,x,y) for(int i=x;i<=y;++i)
#define fl(i,x) for(int i=head[x],to;to=e[i].to,i;i=e[i].nxt)
il void SWAP(int &x,int &y){int t=x;x=y;y=t;}
namespace IO{const char* ln="\n";const int str=1<<20;struct IN{char buf[str],*s,*t;bool _;IN():s(buf),t(buf),_(0){}il char gc(){return s==t&&((t=(s=buf)+fread(buf,1,str,stdin))==s)?EOF:(*s++);}IN&operator>>(char&ch){if(_)return *this;char c;while((c=gc())!=EOF&&isspace(c));if(c==EOF)_=1;else ch=c;return *this;}template<typename T>IN&operator>>(T&x){if(_)return *this;char c=gc();bool ff=0;while(c!=EOF&&(c<'0'||c>'9'))ff^=(c=='-'),c=gc();if(c==EOF){_=1;return *this;}x=0;while(c!=EOF&&'0'<=c&&c<='9')x=(x<<3)+(x<<1)+c-48,c=gc();if(c==EOF)_=1;if(ff)x=-x;return *this;}}in;struct OUT{char buf[str],*s,*t;OUT():s(buf),t(buf+str){}~OUT(){fwrite(buf,1,s-buf,stdout);}void pt(char c){(s==t)?(fwrite(s=buf,1,str,stdout),*s++=c):(*s++=c);}OUT&operator<<(const char*s){while(*s)pt(*s++);return *this;}OUT&operator<<(char*s){while(*s)pt(*s++);return *this;}template<typename T>OUT&operator<<(T x){if(!x)return pt('0'),*this;if(x<0)pt('-'),x=-x;char a[30],t=0;while(x)a[t++]=x%10,x/=10;while(t--)pt(a[t]+'0');return *this;}}out;}using namespace IO;
#define N 200011
int n,cnt=0,head[N],RT;
struct edge{
    int to,nxt;
}e[N];
il void add(int x,int y){
    e[++cnt].to=y;e[cnt].nxt=head[x];head[x]=cnt;
}
int top[N],f[N],siz[N],d[N],id[N],sz=0;
void dfs(int x){
    siz[x]=1;
    fl(i,x){
        f[to]=x;d[to]=d[x]+1;
        dfs(to);siz[x]+=siz[to];
    }
}
void bt(int x,int tp){
    id[x]=++sz;top[x]=tp;int k=0;
    fl(i,x)if(siz[to]>siz[k])k=to;
    if(!k)return;bt(k,tp);
    fl(i,x)if(to!=k)bt(to,to);
}
int s[N];
il void upd(int x){
    while(x<=n)++s[x],x+=(x&-x);
}
il int ask(int x){
    int ans=0;
    while(x)ans+=s[x],x^=(x&-x);
    return ans;
}
int a1[N],a2[N];
struct que{
    int x,y,c,id;
    il bool operator<(que a){
        return c<a.c;
    }
}Q[N];
void work(int x,int y,int ID){
    int ox=x,oy=y,lca,ans=0;
    while(top[x]!=top[y]){
        if(d[top[x]]<d[top[y]])SWAP(x,y);
        ans+=ask(id[x])-ask(id[top[x]]-1);x=f[top[x]];
    }
    if(id[x]>id[y])SWAP(x,y);
    lca=x;
    ans+=ask(id[y])-ask(id[x]-1);
    a1[ID]=d[ox]+d[oy]-d[lca]*2+1;
    a2[ID]=ans;
}
struct node{
    int x,i;
}b[N];
int main(){
    int opt,x,y,c,q;
    in>>n;
    Fur(i,1,n){
        in>>x;
        if(!x)RT=i;
        else add(x,i);
    }
    dfs(RT);bt(RT,RT);
    in>>q;
    int tot=0,TOT=0;
    Fur(i,1,q){
        in>>opt>>x;
        if(opt==1){
            in>>y>>c;
            c=i-c;
            Q[++TOT]=que{x,y,c,TOT};
        }
        else b[++tot]=node{x,i};
    }
    std::sort(Q+1,Q+TOT+1);
    int d=1;
    Fur(i,1,TOT){
        while(d<=tot&&b[d].i<Q[i].c)upd(id[b[d++].x]);
        work(Q[i].x,Q[i].y,Q[i].id);
    }
    Fur(i,1,TOT)out<<a1[i]<<" "<<a2[i]<<ln;
}

### 解法三:

在每个重链的顶端搞个pbds,平衡树

TLE

#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace __gnu_pbds;
namespace ZDY{
    #pragma GCC optimize(3)
    #define il __inline__ __attribute__ ((always_inline))
    #define rg register
    #define ll long long
    #define ull unsigned long long
    #define db double
    #define sht short
    #define MB template <class T>il
    #define Fur(i,x,y) for(int i=x;i<=y;++i)
    #define Fdr(i,x,y) for(int i=x;i>=y;--i)
    #define fl(i,x) for(int i=head[x],to;to=e[i].to,i;i=e[i].nxt)
    #define clr(x,y) memset(x,y,sizeof(x))
    #define cpy(x,y) memcpy(x,y,sizeof(x))
    #define fin(s) freopen(s".in","r",stdin)
    #define fout(s) freopen(s".out","w",stdout)
    #define fcin ios::sync_with_stdio(false)
    #define l2(n) ((int)(log2(n)))
    #define inf 0x3f3f3f3f
    MB T ABS(T x){return x>0?x:-x;}
    MB T MAX(T x,T y){return x>y?x:y;}
    MB T MIN(T x,T y){return x<y?x:y;}
    MB T GCD(T x,T y){return y?GCD(y,x%y):x;}
    MB void SWAP(T &x,T &y){T t=x;x=y;y=t;}
}using namespace ZDY;using namespace std;
namespace IO{const char* ln="\n";const int str=1<<20;struct IN{char buf[str],*s,*t;bool _;IN():s(buf),t(buf),_(0){}il char gc(){return s==t&&((t=(s=buf)+fread(buf,1,str,stdin))==s)?EOF:(*s++);}IN&operator>>(char&ch){if(_)return *this;char c;while((c=gc())!=EOF&&isspace(c));if(c==EOF)_=1;else ch=c;return *this;}IN& operator>>(char* ch){clr(ch,0);if(_)return *this;char c;while((c=gc())!=EOF&&isspace(c));if(c==EOF)return _=1,*this;*ch=c;ch++;while((c=gc())!=EOF&&!isspace(c))*ch=c,ch++;if(c==EOF)_=1;return *this;}IN& operator>>(string& ch){if(_)return *this;char c;while((c=gc())!=EOF&&isspace(c));if(c==EOF)return _=1,*this;ch+=c;while((c=gc())!=EOF&&!isspace(c))ch+=c;if(c==EOF)_=1;return *this;}template<typename T>IN&operator>>(T&x){if(_)return *this;char c=gc();bool ff=0;while(c!=EOF&&(c<'0'||c>'9'))ff^=(c=='-'),c=gc();if(c==EOF){_=1;return *this;}x=0;while(c!=EOF&&'0'<=c&&c<='9')x=(x<<3)+(x<<1)+c-48,c=gc();if(c==EOF)_=1;if(ff)x=-x;return *this;}}in;struct OUT{char buf[str],*s,*t;OUT():s(buf),t(buf+str){}~OUT(){fwrite(buf,1,s-buf,stdout);}void pt(char c){(s==t)?(fwrite(s=buf,1,str,stdout),*s++=c):(*s++=c);}OUT&operator<<(const char*s){while(*s)pt(*s++);return *this;}OUT&operator<<(char*s){while(*s)pt(*s++);return *this;}OUT&operator<<(string s){for(int i=0;s[i];i++)pt(s[i]);return *this;}template<typename T>OUT&operator<<(T x){if(!x)return pt('0'),*this;if(x<0)pt('-'),x=-x;char a[30],t=0;while(x)a[t++]=x%10,x/=10;while(t--)pt(a[t]+'0');return *this;}}out;}using namespace IO;
#define N 200011
int n,cnt=0,head[N],RT;
struct edge{
    int to,nxt;
}e[N];
void add(int x,int y){
    e[++cnt].to=y;e[cnt].nxt=head[x];head[x]=cnt;
}
int top[N],f[N],siz[N],d[N],sz=0;
void dfs(int x){
    siz[x]=1;
    fl(i,x){
        f[to]=x;d[to]=d[x]+1;
        dfs(to);siz[x]+=siz[to];
    }
}
void bt(int x,int tp){
    top[x]=tp;int k=0;
    fl(i,x)if(siz[to]>siz[k])k=to;
    if(!k)return;bt(k,tp);
    fl(i,x)if(to!=k)bt(to,to);
}
int a[N];
tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>T[N];
void upd(int x,int i){
    a[x]=i;T[top[x]].insert(i);
}
int ask(int x,int y,int c){
    int ans=0;
    while(x!=y){
        if(a[x]<c)++ans;
        x=f[x];
    }
    ans+=(a[y]<c);
    return ans;
}
void work(int x,int y,int c){
    int ox=x,oy=y,lca,ans=0;
    ans+=ask(x,top[x],c);x=f[top[x]];
    ans+=ask(y,top[y],c);y=f[top[y]];
    while(top[x]!=top[y]){
        if(d[top[x]]<d[top[y]])SWAP(x,y);
        ans+=T[top[x]].order_of_key(c)-1;
        x=f[top[x]];
    }
    if(d[x]>d[y])SWAP(x,y);
    lca=x;
    ans+=ask(x,y,c);
    out<<d[ox]+d[oy]-d[lca]*2+1<<" "<<ans<<ln;
}
int main(){
    int opt,x,y,c,q;
    in>>n;
    Fur(i,1,n){
        in>>x;
        if(!x)RT=i;
        else add(x,i);
    }
    dfs(RT);bt(RT,RT);
    in>>q;
    Fur(i,1,q)a[i]=q;
    Fur(i,1,q){
        in>>opt>>x;
        if(opt==1){
            in>>y>>c;
            work(x,y,i-c);
        }
        else upd(x,i);
    }
}
LG 4216 [SCOI2015]情报传递
comment评论
Search
search