ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

bzoj 4573 大森林

2019-02-21 19:50:17  阅读:218  来源: 互联网

标签:ch const int pos getchar 4573 虚点 bzoj 森林


bzoj 4573 大森林

  • 由于树上路径是唯一的,查询合法的两个点间路径长度显然与其他加点操作无关,所以可以离线处理,将所有的查询放在加点后.
  • 这样我们可以对每棵树都在上颗树的基础上处理好形态后,处理这颗树上的询问.
  • 考虑若没有操作 \(1\) ,则所有树都一模一样.那么两棵树的形态不同,一定是某一颗执行了操作 \(1\) ,另一个却没有.只需要在每个关键位置(修改开始或结束时)将原来生长点所有子树挂到新的生长点下.
  • 若暴力移动子树,当子树数目较大时就会很劣.可以对每个操作 \(1\) 建立一个虚点,将这个操作内挂上去的点都挂在虚点上,于是只需要移动虚点就可以了.注意将虚点 \(siz\) 设置为 \(0\) .
  • 查询答案时,使用 \(dep[x]+dep[y]-2*dep[lca]\) 计算,避免虚点造成的影响 (若 \(lca\) 是虚点,直接用 \(split(x,y)\) 会出问题哦).
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
inline int read()
{
    int x=0;
    bool pos=1;
    char ch=getchar();
    for(;!isdigit(ch);ch=getchar())
        if(ch=='-')
            pos=0;
    for(;isdigit(ch);ch=getchar())
        x=x*10+ch-'0';
    return pos?x:-x;
}
const int MAXN=3e5+10;
struct query{
    int pos,op,x,y;
    bool operator < (const query &rhs) const
        {
            return pos==rhs.pos?op<rhs.op:pos<rhs.pos;
        }
}q[MAXN];
namespace LCT{
    int stk[MAXN],tp;
    int tot;
    struct node{
        int ch[2],fa;
        int rev;
        int siz,val;
        node()
            {
                fa=ch[0]=ch[1]=0;
                rev=0;
                siz=val=0;
            }
    }tree[MAXN];
    #define root tree[x]
    #define lson tree[root.ch[0]]
    #define rson tree[root.ch[1]]
    inline void pushup(int x)
        {
            root.siz=lson.siz+root.val+rson.siz;
        }
    void inverse(int x)
        {
            swap(root.ch[0],root.ch[1]);
            root.rev^=1;
        }
    void pushdown(int x)
        {
            if(root.rev)
                {
                    if(root.ch[0])
                        inverse(root.ch[0]);
                    if(root.ch[1])
                        inverse(root.ch[1]);
                    root.rev=0;
                }
        }
    bool isroot(int x)
        {
            int y=root.fa;
            return tree[y].ch[0]!=x && tree[y].ch[1]!=x;
        }
    void rotate(int x)
        {
            int y=tree[x].fa,z=tree[y].fa;
            int k=tree[y].ch[1]==x;
            if(!isroot(y))
                tree[z].ch[tree[z].ch[1]==y]=x;
            tree[x].fa=z;
            tree[tree[x].ch[k^1]].fa=y;
            tree[y].ch[k]=tree[x].ch[k^1];
            tree[x].ch[k^1]=y;
            tree[y].fa=x;
            pushup(y);
            pushup(x);
        }
    void splay(int x)
        {
            tp=0;
            stk[++tp]=x;
            for(int pos=x;!isroot(pos);pos=tree[pos].fa)
                stk[++tp]=tree[pos].fa;
            while(tp)
                pushdown(stk[tp--]);
            while(!isroot(x))
                {
                    int y=tree[x].fa,z=tree[y].fa;
                    if(!isroot(y))
                        (tree[y].ch[0]==x)^(tree[z].ch[0]==y)?rotate(x):rotate(y);
                    rotate(x);
                }
            pushup(x);
        }
    int Access(int x)
        {
            int y;
            for(y=0;x;y=x,x=tree[x].fa)
                {
                    splay(x);
                    tree[x].ch[1]=y;
                    pushup(x);
                }
            return y;
        }
    void makeroot(int x)
        {
            Access(x);
            splay(x);
            inverse(x);
        }
    int findroot(int x)
        {
            Access(x);
            splay(x);
            while(tree[x].ch[0])
                x=tree[x].ch[0];
            return x;
        }
    void split(int x,int y)
        {
            makeroot(x);
            Access(y);
            splay(y);
        }
    void Link(int x,int y)
        {
            makeroot(x);
            tree[x].fa=y;
        }
    void Cut(int x)
        {
            Access(x);
            splay(x);
            tree[tree[x].ch[0]].fa=0;
            tree[x].ch[0]=0;
            pushup(x);
        }
}
using namespace LCT;
int solve(int x,int y)
{
    int ans=0;
    Access(x);
    splay(x);
    ans+=tree[x].siz;
    int lca=Access(y);
    splay(y);
    ans+=tree[y].siz;
    Access(lca);
    splay(lca);
    ans-=2*tree[lca].siz;
    return ans;
}
void newnode(int v)
{
    int x=++tot;
    root.val=root.siz=v;
}
int n,m;
int last,id[MAXN],idd,cnt,L[MAXN],R[MAXN],qs;
inline void insq(int pos,int op,int x,int y)
{
    ++cnt;
    q[cnt].pos=pos,q[cnt].op=op;
    q[cnt].x=x,q[cnt].y=y;
}
int ans[MAXN];
int main()
{
    tot=0;
    n=read(),m=read();
    newnode(1),idd=1,L[1]=1,R[1]=n,id[1]=1;
    newnode(0),last=2,Link(2,1);
    for(int i=1;i<=m;++i)
        {
            int type=read();
            if(type==0)
                {
                    int l=read(),r=read();
                    newnode(1);
                    L[++idd]=l,R[idd]=r,id[idd]=tot;
                    insq(1,i-m,tot,last);
                }
            else if(type==1)
                {
                    int l=read(),r=read(),x=read();
                    l=max(l,L[x]),r=min(r,R[x]);
                    if(l<=r)
                        {
                            newnode(0);
                            Link(tot,last);
                            insq(l,i-m,tot,id[x]);
                            insq(r+1,i-m,tot,last);
                            last=tot;
                        }
                }
            else
                {
                    int x=read(),u=read(),v=read();
                    insq(x,++qs,id[u],id[v]);
                }
        }
    sort(q+1,q+cnt+1);
    for(int i=1,j=1;i<=n;++i)
        {
            for(;j<=cnt && q[i].pos==i;++j)
                {
                    if(q[j].op<=0)
                        {
                            Cut(q[j].x);
                            Link(q[j].x,q[j].y);
                        }
                    else
                        ans[q[j].op]=solve(q[j].x,q[j].y);
                }
        }
    for(int i=1;i<=qs;++i)
        printf("%d\n",ans[i]);
    return 0;
}

标签:ch,const,int,pos,getchar,4573,虚点,bzoj,森林
来源: https://www.cnblogs.com/jklover/p/10414862.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有