zoukankan      html  css  js  c++  java
  • 【bzoj2157】旅游

    题目描述

    Ray 乐忠于旅游,这次他来到了T 城。T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接。为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径。换句话说, T 城中只有N − 1 座桥。

    Ray 发现,有些桥上可以看到美丽的景色,让人心情愉悦,但有些桥狭窄泥泞,令人烦躁。于是,他给每座桥定义一个愉悦度w,也就是说,Ray 经过这座桥会增加w 的愉悦度,这或许是正的也可能是负的。有时,Ray 看待同一座桥的心情也会发生改变。

    现在,Ray 想让你帮他计算从u 景点到v 景点能获得的总愉悦度。有时,他还想知道某段路上最美丽的桥所提供的最大愉悦度,或是某段路上最糟糕的一座桥提供的最低愉悦度。


    输入

    输入的第一行包含一个整数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。


    输出

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


    样例输入

    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


    样例输出

    3
    2
    1
    -1
    5
    3


    题解

    近似于模板,细节有点多,代码挺长。

    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define ll long long
    
    const int maxn=20000+50;
    const int maxm=40000+50;
    const int inf=2e9;
    
    int n,fa[maxn],m,cnt,a[maxn],val[maxn];
    int fir[maxn],nex[maxm],to[maxm],from[maxm],wi[maxm],ecnt;
    int top[maxn],son[maxn],dep[maxn],sz[maxn],id[maxn],wt[maxn];
    
    struct SegmentTree{int l,r,v,mul,mn,ma;}st[maxn<<2];
    
    void add(int u,int v,int w){
        nex[++ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v;from[ecnt]=u;wi[ecnt]=w;
    }
    
    void dfs1(int x,int f,int deep){
        fa[x]=f;dep[x]=deep;
        sz[x]=1;
        for(int e=fir[x];e;e=nex[e]){
            int v=to[e];
            if(v==f) continue;
            val[v]=wi[e];
            dfs1(v,x,deep+1);
            sz[x]+=sz[v];
            if(sz[v]>sz[son[x]]) son[x]=v;
        }
    }
    
    void dfs2(int x,int topf){
        top[x]=topf;
        id[x]=++cnt;
        wt[cnt]=val[x];
        if(!son[x]) return ;
        dfs2(son[x],topf);
        for(int e=fir[x];e;e=nex[e]){
            int v=to[e];
            if(v==fa[x]||v==son[x]) continue;
            dfs2(v,v);
        }
    }
    
    void pushup(int root){
        st[root].v=st[root<<1].v+st[root<<1|1].v;
        st[root].ma=max(st[root<<1].ma,st[root<<1|1].ma);
        st[root].mn=min(st[root<<1].mn,st[root<<1|1].mn);
    }
    
    void build(int root,int l,int r){
        st[root].l=l;st[root].r=r;st[root].mul=1;
        if(l==r){
            if(l==1){
                st[root].v=wt[l];st[root].ma=-inf;st[root].mn=inf;
            }
            else st[root].v=st[root].ma=st[root].mn=wt[l];
        } 
        else{
            int m=l+r>>1;
            build(root<<1,l,m);build(root<<1|1,m+1,r);
            pushup(root);
        }
    }
    
    void pushdown(int root){
        if(st[root].mul!=1){
            st[root<<1].v*=-1;
            st[root<<1|1].v*=-1;
            st[root<<1].mul*=-1;
            st[root<<1|1].mul*=-1;
            st[root<<1].ma*=-1;
            st[root<<1].mn*=-1;
            st[root<<1|1].ma*=-1;
            st[root<<1|1].mn*=-1;
            swap(st[root<<1].ma,st[root<<1].mn);
            swap(st[root<<1|1].ma,st[root<<1|1].mn);
            st[root].mul=1;
        }
    }
    
    void change(int root,int l,int r,int val){
        if(st[root].l>r||st[root].r<l) return ;
        if(st[root].l>=l&&st[root].r<=r){
            st[root].v=val;
            st[root].ma=st[root].mn=val;
        }
        else{
            pushdown(root);
            change(root<<1,l,r,val);change(root<<1|1,l,r,val);
            pushup(root);
        }
    }
    
    void update(int root,int l,int r){
        if(st[root].l>r||st[root].r<l) return ;
        if(st[root].l>=l&&st[root].r<=r){
            st[root].v*=-1;
            st[root].ma*=-1;
            st[root].mn*=-1;
            st[root].mul*=-1;
            swap(st[root].ma,st[root].mn);
        }
        else{
            pushdown(root);
            update(root<<1,l,r);update(root<<1|1,l,r);
            pushup(root);
        }
    }
    
    int query(int root,int l,int r){
        if(st[root].l>r||st[root].r<l) return 0;
        if(st[root].l>=l&&st[root].r<=r) return st[root].v;
        pushdown(root);
        return query(root<<1,l,r)+query(root<<1|1,l,r);
    }
    
    int query_max(int root,int l,int r){
        if(st[root].l>r||st[root].r<l) return -inf;
        if(st[root].l>=l&&st[root].r<=r) return st[root].ma;
        pushdown(root);
        return max(query_max(root<<1,l,r),query_max(root<<1|1,l,r));
    }
    
    int query_min(int root,int l,int r){
        if(st[root].l>r||st[root].r<l) return inf;
        if(st[root].l>=l&&st[root].r<=r) return st[root].mn;
        pushdown(root);
        return min(query_min(root<<1,l,r),query_min(root<<1|1,l,r));
    }
    
    void T_up(int x,int y){
        int f1=top[x],f2=top[y];
        while(f1!=f2){
            if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
            update(1,id[f1],id[x]);
            x=fa[f1];f1=top[x];
        }
        if(dep[x]>dep[y]) swap(x,y);
        update(1,id[x]+1,id[y]);
    }
    
    int T_sum(int x,int y){
        int f1=top[x],f2=top[y],ans=0;
        while(f1!=f2){
            if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
            ans+=query(1,id[f1],id[x]);
            x=fa[f1];f1=top[x];
        }
        if(dep[x]>dep[y]) swap(x,y);
        ans+=query(1,id[x]+1,id[y]);
        return ans;
    }
    
    int T_max(int x,int y){
        int f1=top[x],f2=top[y],ans=-inf;
        while(f1!=f2){
            if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
            ans=max(ans,query_max(1,id[f1],id[x]));
            x=fa[f1];f1=top[x];
        }
        if(dep[x]>dep[y]) swap(x,y);
        ans=max(ans,query_max(1,id[x]+1,id[y]));
        return ans;
    }
    
    int T_min(int x,int y){
        int f1=top[x],f2=top[y],ans=inf;
        while(f1!=f2){
            if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
            ans=min(ans,query_min(1,id[f1],id[x]));
            x=fa[f1];f1=top[x];
        }
        if(dep[x]>dep[y]) swap(x,y);
        ans=min(ans,query_min(1,id[x]+1,id[y]));
        return ans;
    }
    
    template<typename T>void read(T& aa){
        char cc; ll ff;aa=0;cc=getchar();ff=1;
        while((cc<'0'||cc>'9')&&cc!='-') cc=getchar();
        if(cc=='-') ff=-1,cc=getchar();
        while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
        aa*=ff;
    }
    
    int main(){
        read(n);
        for(int i=1;i<n;i++){
            int x,y,z;
            read(x),read(y),read(z);
            x++,y++;
            add(x,y,z);add(y,x,z);
        }
        dfs1(1,0,1);dfs2(1,1);build(1,1,n);
        read(m);char c[10];int x,y;
        while(m--){
            scanf("%s",c);
            if(c[0]=='C'){
                read(x);read(y);
                x=x*2-1;
                int u=from[x],v=to[x];
                if(dep[u]>dep[v]) swap(u,v);
                change(1,id[v],id[v],y);
            }
            else if(c[0]=='N'){
                read(x),read(y);x++,y++;
                T_up(x,y);
            }
            else if(c[0]=='S'){
                read(x),read(y);x++,y++;
                printf("%d
    ",T_sum(x,y));
            }
            else if(c[0]=='M'&&c[1]=='A'){
                read(x),read(y);x++,y++;
                printf("%d
    ",T_max(x,y));
            }
            else{
                read(x),read(y);x++,y++;
                printf("%d
    ",T_min(x,y));
            }
        }
        return 0;
    }
  • 相关阅读:
    动态tab页
    SQL Server附加数据库拒绝访问
    SQL window身份登陆 SQL server不能登陆
    SQL UPDATE 经典
    Char Varchar Nvarchar区别
    【OpenSSL】创建证书
    makecert 制作数字证书
    cer, pfx 创建,并且读取公钥/密钥,加解密 (C#程序实现)
    CompareValidator ASP控件
    XMLHelper 类
  • 原文地址:https://www.cnblogs.com/rlddd/p/9807476.html
Copyright © 2011-2022 走看看