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

    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
    C 1 3
    SUM 0 2
    MAX 0 2

    Sample Output

    3
    2
    1
    -1
    5
    3

    HINT

    一共有10 个数据,对于第i (1 <= i <= 10) 个数据, N = M = i * 2000。

     
    题解:
      LCT或树剖模板题···
    code:
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 using namespace std;
     7 char ch; bool ok;
     8 void read(int &x){
     9     for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
    10     for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
    11     if (ok) x=-x;
    12 }
    13 const int maxn=200005;
    14 const int inf=0x3f3f3f3f;
    15 int n,a,b,c,q;
    16 char op[10];
    17 struct LCT{
    18     #define ls son[x][0]
    19     #define rs son[x][1]
    20     int fa[maxn],son[maxn][2],neg[maxn],rev[maxn],val[maxn],sum[maxn],res[maxn][2];//0:min 1:max
    21     void init(){res[0][0]=inf,res[0][1]=-inf;}
    22     int which(int x){return son[fa[x]][1]==x;}
    23     bool isroot(int x){return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;}
    24     void addtag_rev(int x){if (x) rev[x]^=1,swap(ls,rs);}
    25     void addtag_neg(int x){
    26         if (x){
    27             neg[x]^=1,val[x]=-val[x],sum[x]=-sum[x];
    28             swap(res[x][0],res[x][1]),res[x][0]=-res[x][0],res[x][1]=-res[x][1];
    29         }
    30     }
    31     void pushdown(int x){
    32         if (rev[x]) addtag_rev(ls),addtag_rev(rs),rev[x]=0;
    33         if (neg[x]) addtag_neg(ls),addtag_neg(rs),neg[x]=0;
    34     }
    35     void relax(int x){
    36         if (!isroot(x)) relax(fa[x]);
    37         pushdown(x);    
    38     }
    39     void updata(int x){
    40         sum[x]=sum[ls]+val[x]+sum[rs];
    41         res[x][0]=min(res[ls][0],res[rs][0]);
    42         res[x][1]=max(res[ls][1],res[rs][1]);
    43         if (x>n) res[x][0]=min(res[x][0],val[x]),res[x][1]=max(res[x][1],val[x]);
    44     }
    45     void rotate(int x){
    46         int y=fa[x],z=fa[y],d=which(x),dd=which(y);
    47         fa[son[x][d^1]]=y,son[y][d]=son[x][d^1],fa[x]=fa[y];
    48         if (!isroot(y)) son[z][dd]=x;
    49         son[x][d^1]=y,fa[y]=x,updata(y);    
    50     }
    51     void splay(int x){
    52         relax(x);
    53         while (!isroot(x)){
    54             if (isroot(fa[x])) rotate(x);
    55             else if (which(fa[x])==which(x)) rotate(fa[x]),rotate(x);
    56             else rotate(x),rotate(x);    
    57         }
    58         updata(x);    
    59     }
    60     void access(int x){for (int p=0;x;x=fa[x]) splay(x),son[x][1]=p,p=x;}
    61     void make_root(int x){access(x),splay(x),addtag_rev(x);}
    62     void init(int id,int v){int x=n+id; val[x]=sum[x]=res[x][0]=res[x][1]=v;}
    63     void connect(int u,int v,int id,int w){make_root(u),fa[u]=n+id,fa[n+id]=v,init(id,w);}
    64     int query_sum(int u,int v){make_root(u),access(v),splay(v);return sum[v];}
    65     int query_min(int u,int v){make_root(u),access(v),splay(v);return res[v][0];}
    66     int query_max(int u,int v){make_root(u),access(v),splay(v);return res[v][1];}
    67     void change(int x,int v){splay(x),val[x]=v,updata(x);}
    68     void _neg(int u,int v){make_root(u),access(v),splay(v),addtag_neg(v);}
    69 }T;
    70 int main(){
    71     read(n),T.init();
    72     for (int i=1;i<n;i++) read(a),read(b),read(c),T.connect(++a,++b,i,c);
    73     for (read(q);q;q--){
    74         scanf("%s",op+1),read(a),read(b);
    75         if (op[1]=='C') T.change(n+a,b);
    76         else if (op[1]=='N') T._neg(++a,++b);
    77         else if (op[1]=='S') printf("%d
    ",T.query_sum(++a,++b));
    78         else if (op[2]=='I') printf("%d
    ",T.query_min(++a,++b));
    79         else if (op[2]=='A') printf("%d
    ",T.query_max(++a,++b));
    80     }
    81     return 0;
    82 }
  • 相关阅读:
    关于32位操作系统和64位操作系统对InstallShield打包的影响
    NEWS: Symantec宣布Wise Package Studio将终止
    InstallShield 2012新功能试用(2) 调用MsiGetProperty等MSI API发生变化
    Basic INFO 在命令行Build InstallShield安装包工程获得压缩安装包
    NEWS InstallShield 2012 Service Pack 1发布
    Basic INFO InstallShield Basic MSI工程中如何在SetupCompleteSuccess界面中启动Readme
    Basic INFO InstallShield的脚本编辑器中如何显示代码行号
    Basic INFO 关于在InstallShield制作的安装包界面中删除InstallShield文字的厂商回复
    Basic INFO InstallShield工程中如何让产品的快捷方式名称始终与产品名保持一致
    Basic INFO: 创建隐藏文件夹
  • 原文地址:https://www.cnblogs.com/chenyushuo/p/7004460.html
Copyright © 2011-2022 走看看