bzoj2157旅游(代码片段)

rlddd rlddd     2023-01-08     371

关键词:

题目描述

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 SegmentTreeint 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;

 






bzoj2157:旅游树链剖分+线段树(代码片段)

裸的树链剖分+线段树但是要注意一个地方……我WA了好几次才发现取完相反数之后max值和min值是要交换的……#include<iostream>#include<cstdio>usingnamespacestd;constintN=200005;intn,m,h[N],cnt,de[N],va[N],fa[N],si[N],hs[N],fr[N],id[N],tot,rl[ 查看详情

bzoj2157「国家集训队」旅游(树链剖分,线段树,边权转点权)bzoj计划(代码片段)

整理的算法模板合集:ACM模板点我看算法全家桶系列!!!实际上是一个全新的精炼模板整合计划题目链接https://hydro.ac/d/bzoj/p/2157是hydro的BZOJ修复工程!Problem给定一棵nnn个节点的树,边带权,编号0∼n... 查看详情

bzoj2157「国家集训队」旅游(树链剖分,线段树,边权转点权)bzoj计划(代码片段)

整理的算法模板合集:ACM模板点我看算法全家桶系列!!!实际上是一个全新的精炼模板整合计划题目链接https://hydro.ac/d/bzoj/p/2157是hydro的BZOJ修复工程!Problem给定一棵nnn个节点的树,边带权,编号0∼n... 查看详情

ac日记——旅游bzoj2157

2157 思路:  LCT; 代码:#include<bits/stdc++.h>usingnamespacestd;#definemaxn400005#defineINF0x3f3f3f3fintn,val[maxn],sum[maxn],Max[maxn],Min[maxn],ch[maxn][2];intrev[maxn],flag[maxn],f[maxn] 查看详情

bzoj2157旅游(动态树)

 【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=2157 【题目大意】  支持修改边,链上查询最大值最小值总和,以及链上求相反数 【题解】  我们将边转化成点,直接用LCT可以处理以上操作 【代码】#i... 查看详情

bzoj2157旅游(树链剖分+线段树)

 【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=2157 【题目大意】  支持修改边,链上查询最大值最小值总和,以及链上求相反数 【题解】  树链剖分,然后线段树维护线段操作即可。 【代码】#include... 查看详情

bzoj2157旅游lct

模板T,SB的DMoon。。其实样例也是中国好样例。。。一开始不会复制,yangyang:找到“sampleinput”按住shift,按pagedown。。。。1#include<iostream>2#include<cstdio>3#defineinf0x7fffffff4#defineN200105#defineM200106usingname 查看详情

bzoj2157:旅游树链剖分线段树

http://www.lydsy.com/JudgeOnline/problem.php?id=2157 在对树中数据进行改动的时候需要很多pushdown(具体操作见代码),不然会wa,大概原因和线段树区间修改需要很多pushup是一样的。这个轻重链的方法特别好用,虽然第一次写树链剖分... 查看详情

bzoj2157:旅游

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

[bzoj2157]旅游

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

bzoj_2157_旅游_树剖+线段树

BZOJ_2157_旅游_树剖+线段树DescriptionRay乐忠于旅游,这次他来到了T城。T城是一个水上城市,一共有N个景点,有些景点之间会用一座桥连接。为了方便游客到达每个景点但又为了节约成本,T城的任意两个景点之间有且只有一条路径... 查看详情

bzoj2157旅行模拟

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

bzoj1393旅游航道(代码片段)

Description  SGOI旅游局在SG-III星团开设了旅游业务,每天有数以万计的地球人来这里观光,包括联合国秘书长,各国总统和SGOI总局局长等。旅游线路四通八达,每天都有总躲得载客太空飞船在星团的星球之间来往穿梭,他们保... 查看详情

bzoj1097:[poi2007]旅游景点atr(代码片段)

看懂题意就是成功的一半明显状压DPdij预处理K之间的最短路先枚举状态的话就有单调性可以省掉一维了#include<cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>#include<q 查看详情

hdu2157howmanyways??矩阵经典8(代码片段)

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2157Howmanyways??TimeLimit:2000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):5339   & 查看详情

hdu-2157howmanyways??(代码片段)

#include<algorithm>#include<iostream>#include<cmath>#include<cstring>#include<map>#include<string>#include<vector>#include<queue>#include<stack>#i 查看详情

bzoj2157旅行(树链剖分码农题)

写了5KB,1发AC。。。题意:给出一颗树,支持5种操作。1.修改某条边的权值。2.将u到v的经过的边的权值取负。3.求u到v的经过的边的权值总和。4.求u到v的经过的边的权值最大值。5.求u到v经过的边的权值最小值。基于边权的树链剖... 查看详情

[bzoj3999][tjoi2015]旅游(代码片段)

Description为了提高智商,ZJY准备去往一个新世界去旅游。这个世界的城市布局像一棵树。每两座城市之间只有一条路径可以互达。每座城市都有一种宝石,有一定的价格。ZJY为了赚取最高利益,她会选择从A城市买入再转手卖到B... 查看详情