zoukankan      html  css  js  c++  java
  • BZOJ2157: 旅游

    题解:

    裸链剖/LCT

    刚开始想把边转到点上,结果各种蛋疼,后来发现lct的话,好像在边上不难处理。。。

    我个逗比忘了把v[x]取反了。。。

    代码:

      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<cmath>
      4 #include<cstring>
      5 #include<algorithm>
      6 #include<iostream>
      7 #include<vector>
      8 #include<map>
      9 #include<set>
     10 #include<queue>
     11 #include<string>
     12 #define inf 2000000000
     13 #define maxn 400000+5
     14 #define maxm 100000+5
     15 #define eps 1e-10
     16 #define ll long long
     17 #define pa pair<int,int>
     18 #define for0(i,n) for(int i=0;i<=(n);i++)
     19 #define for1(i,n) for(int i=1;i<=(n);i++)
     20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
     21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
     22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
     23 #define mod 1000000007
     24 using namespace std;
     25 inline int read()
     26 {
     27     int x=0,f=1;char ch=getchar();
     28     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     29     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
     30     return x*f;
     31 }
     32 int n,m,tot,tmp,ans,top,id[maxn],head[maxn],sta[maxn],v[maxn],sum[maxn],mi[maxn],mx[maxn],c[maxn][2],fa[maxn];
     33 bool tag[maxn];
     34 struct edge{int go,next,w;}e[maxn];
     35 inline bool isroot(int x)
     36 {
     37     return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
     38 }
     39 inline void pushup(int x)
     40 {
     41     int l=c[x][0],r=c[x][1];
     42     sum[x]=sum[l]+sum[r]+v[x];
     43     mi[x]=min(v[x],min(mi[l],mi[r]));
     44     mx[x]=max(v[x],max(mx[l],mx[r]));
     45 }
     46 inline void update(int x)
     47 {
     48     tag[x]^=1;
     49     v[x]=-v[x];
     50     sum[x]=-sum[x];
     51     int t=mi[x];
     52     mi[x]=-mx[x];
     53     mx[x]=-t;
     54 }
     55 inline void pushdown(int x)
     56 {
     57     int l=c[x][0],r=c[x][1];
     58     if(tag[x])
     59     {
     60         update(l);
     61         update(r);
     62         tag[x]=0;
     63     }
     64 }
     65 inline void rotate(int x)
     66 {
     67     int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
     68     if(!isroot(y))c[z][c[z][1]==y]=x;
     69     fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
     70     c[y][l]=c[x][r];c[x][r]=y;
     71     pushup(y);pushup(x);
     72 }
     73 inline void splay(int x)
     74 {
     75     sta[++top]=x;
     76     for(int y=x;!isroot(y);y=fa[y])sta[++top]=fa[y];
     77     while(top)pushdown(sta[top--]);
     78     while(!isroot(x))
     79     {
     80         int y=fa[x],z=fa[y];
     81         if(!isroot(y))
     82         {
     83             if(c[z][1]==y^c[y][1]==x)rotate(x);
     84             else rotate(y);
     85         }
     86         rotate(x);
     87     }
     88 }
     89 inline void access(int x)
     90 {
     91     for(int y=0;x;x=fa[x])
     92     {
     93       splay(x);c[x][1]=y;pushup(x);y=x;
     94     }
     95 }
     96 inline void ask(int x,int y)
     97 {
     98     access(y);
     99     for(y=0;x;x=fa[x])
    100     {
    101         splay(x);
    102         if(!fa[x])
    103         {
    104             if(tmp==1)update(c[x][1]),update(y);
    105             if(tmp==2)ans=sum[c[x][1]]+sum[y];
    106             if(tmp==3)ans=max(mx[c[x][1]],mx[y]);
    107             if(tmp==4)ans=min(mi[c[x][1]],mi[y]);
    108         }
    109         c[x][1]=y;pushup(x);y=x;
    110     }
    111 }
    112 inline void add(int x,int y,int w)
    113 {
    114     e[++tot]=(edge){y,head[x],w};head[x]=tot;
    115     e[++tot]=(edge){x,head[y],w};head[y]=tot;
    116 }
    117 inline void dfs(int x)
    118 {
    119     for4(i,x)if(y!=fa[x])
    120     {
    121         id[i>>1]=y;fa[y]=x;
    122         sum[y]=mi[y]=mx[y]=v[y]=e[i].w;
    123         dfs(y);
    124     }
    125 }
    126 int main()
    127 {
    128     freopen("input.txt","r",stdin);
    129     freopen("output.txt","w",stdout);
    130     n=read();
    131     tot=1;
    132     for1(i,n-1)
    133     {
    134         int x=read()+1,y=read()+1,w=read();
    135         add(x,y,w);
    136     }
    137     dfs(1);
    138     mi[0]=inf;mx[0]=-inf;
    139     m=read();char ch[10];
    140     while(m--)
    141     {
    142         scanf("%s",ch);int x=read()+1,y=read()+1;
    143         if(ch[0]=='C')
    144         {
    145             x=id[x-1];y--;
    146             splay(x);v[x]=y;pushup(x);
    147         }else
    148         {
    149             if(ch[0]=='N')tmp=1;
    150             else if(ch[0]=='S')tmp=2;
    151             else if(ch[1]=='A')tmp=3;
    152             else tmp=4;
    153             ans=inf;
    154             ask(x,y);
    155             if(ans!=inf)printf("%d
    ",ans);
    156         }
    157     }
    158     return 0;
    159 }
    View Code

    2157: 旅游

    Time Limit: 10 Sec  Memory Limit: 259 MB
    Submit: 123  Solved: 84
    [Submit][Status]

    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),输出答案。

  • 相关阅读:
    Python模块之logging
    Python模块之configparser
    python序列化模块 json&&pickle&&shelve
    Python模块之hashlib
    最短路之Floyd(多源)HDU 1874
    Python类知识学习时的部分问题
    第9章 Python文件操作目录
    第8章 Python类中常用的特殊变量和方法目录
    第7章 Python类型、类、协议目录
    第6章 Python中的动态可执行方法目录
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4198627.html
Copyright © 2011-2022 走看看