• bzoj 2157: 旅游


    2157: 旅游

    Time Limit: 10 Sec  Memory Limit: 259 MB
    http://www.lydsy.com/JudgeOnline/problem.php?id=2157

    Description

    Ray 乐忠于旅游,这次他来到了T 城。T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接。为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径。换句话说, T 城中只有N − 1 座桥。Ray 发现,有些桥上可以看到美丽的景色,让人心情愉悦,但有些桥狭窄泥泞,令人烦躁。于是,他给每座桥定义一个愉悦度w,也就是说,Ray 经过这座桥会增加w 的愉悦度,这或许是正的也可能是负的。有时,Ray 看待同一座桥的心情也会发生改变。现在,Ray 想让你帮他计算从u 景点到v 景点能获得的总愉悦度。有时,他还想知道某段路上最美丽的桥所提供的最大愉悦度,或是某段路上最糟糕的一座桥提供的最低愉悦度。

    Input

    输入的第一行包含一个整数N,表示T 城中的景点个数。景点编号为 0...N − 1。接下来N − 1 行,每行三个整数u、v 和w,表示有一条u 到v,使 Ray 愉悦度增加w 的桥。桥的编号为1...N − 1。|w| <= 1000。输入的第N + 1 行包含一个整数M,表示Ray 的操作数目。接下来有M 行,每行描述了一个操作,操作有如下五种形式: C i w,表示Ray 对于经过第i 座桥的愉悦度变成了w。 N u v,表示Ray 对于经过景点u 到v 的路径上的每一座桥的愉悦度都变成原来的相反数。 SUM u v,表示询问从景点u 到v 所获得的总愉悦度。 MAX u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最大愉悦度。 MIN u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最小愉悦度。测试数据保证,任意时刻,Ray 对于经过每一座桥的愉悦度的绝对值小于等于1000。

    Output

    对于每一个询问(操作S、MAX 和MIN),输出答案。

    Sample Input

    3
    0 1 1
    1 2 2
    8
    SUM 0 2
    MAX 0 2
    N 0 1
    SUM 0 2
    MIN 0 2
     
    练LCT
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define N 40101
    using namespace std;
    int fa[N],ch[N][2];
    int st[N],top;
    int maxn[N],minn[N],sum[N],key[N];
    bool opp[N],rev[N];
    int tot,id[N];
    int n;
    struct LINKCUTTREE
    {
        bool isroot(int x)
        {
            return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;
        }
        void up(int x)
        {
            sum[x]=key[x]+sum[ch[x][0]]+sum[ch[x][1]];
            maxn[x]=max(maxn[ch[x][1]],maxn[ch[x][0]]);
            minn[x]=min(minn[ch[x][1]],minn[ch[x][0]]);
            if(x>n) maxn[x]=max(key[x],maxn[x]);
            if(x>n) minn[x]=min(key[x],minn[x]);
        }
        void opposite(int x)
        {
            sum[x]=-sum[x];
            key[x]=-key[x];
            maxn[x]=-maxn[x];
            minn[x]=-minn[x];
            swap(minn[x],maxn[x]);
            opp[x]^=1;
        }
        void down(int x)
        {
            if(opp[x])
            {
                if(ch[x][0]) opposite(ch[x][0]);
                if(ch[x][1]) opposite(ch[x][1]);
                opp[x]=0;
            }
            if(rev[x])
            {
                rev[x]^=1; rev[ch[x][0]]^=1; rev[ch[x][1]]^=1;
                swap(ch[x][0],ch[x][1]);
            }
        }
        void rotate(int x)
        {
            int y=fa[x],z=fa[y],l,r;
            l= ch[y][0]!=x; r=l^1;
            if(!isroot(y)) 
            {
                if(ch[z][1]==y) ch[z][1]=x;   else ch[z][0]=x;
            }
            ch[y][l]=ch[x][r]; ch[x][r]=y;
            fa[y]=x; fa[ch[y][l]]=y;  fa[x]=z;
            up(y);
        }
        bool getson(int x)
        {
            return ch[fa[x]][1]==x;
        }
        void splay(int x)
        {
            st[top=1]=x;
            for(int i=x;!isroot(i);i=fa[i]) st[++top]=fa[i];
            for(int i=top;i;i--) down(st[i]);
            while(!isroot(x))
            {
                int y=fa[x];
                if(!isroot(y)) rotate(getson(x)==getson(y) ? y : x);
                rotate(x);
                up(x);
            }
        }
        void access(int x)
        {
            int t=0;
            while(x)
            {
                splay(x);
                ch[x][1]=t;
                up(x);
                t=x; x=fa[x];
            }
        }
        void make_root(int x)
        {
            access(x);
            splay(x);
            rev[x]^=1;
        }
        void link(int x,int y)
        {
            make_root(x);
            fa[x]=y;
        }
        void cut(int x,int y)
        {
            make_root(x);
            access(y);
            splay(y);
            //ch[y][0]=fa[x]=0;
        }
    }Tree;
    int main()
    {
        int u,v,w,m;
        scanf("%d",&n);
        tot=n;
        memset(maxn,-127,sizeof(maxn));
        memset(minn,127,sizeof(minn));
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            u++; v++;
            id[i]=++tot;
            sum[tot]=key[tot]=maxn[tot]=minn[tot]=w;
            Tree.link(u,tot); Tree.link(v,tot);
        }
        scanf("%d",&m);
        char ch[10]; int x,y;
        while(m--)
        {
            scanf("%s",ch);
            scanf("%d%d",&x,&y);
            if(ch[0]=='C')
            {
                Tree.splay(id[x]);
                key[id[x]]=y;
                Tree.up(id[x]);
            }
            else if(ch[0]=='N')
            {
                x++; y++;
                Tree.cut(x,y);
                Tree.opposite(y);
            }
            else if(ch[0]=='S')
            {
                x++; y++;
                Tree.cut(x,y);
                printf("%d
    ",sum[y]);
            }
            else if(ch[1]=='A')
            {
                x++; y++;
                Tree.cut(x,y);
                printf("%d
    ",maxn[y]);
            }
            else
            {
                x++; y++;
                Tree.cut(x,y);
                printf("%d
    ",minn[y]);
            }
        }
    }

     错误:

    up函数里,即使x没有孩子节点,也要取大、取小,最后再与自身取

    否则,如果一个实际没有权值(LCT里为极大、极小)的点,从上面转下来自后,不能更新掉他原先子节点的最大最小值

  • 相关阅读:
    Some notes in Stanford CS106A(4)
    Some notes in Stanford CS106A(3)
    Some notes in Stanford CS106A(2)
    Some notes in Stanford CS106A(1)
    将前台页面的数据传到后台的方法(不调用ajax,少量数据)
    12、(扩展)获取省份表,填充于下拉列表框的简易js
    iframe刷新问题
    MVC基础
    DataList
    序列化
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6880118.html
Copyright © 2020-2023  润新知