• 月下“毛景树”(树剖)


    一道简单树剖,把边权变为点权,随便写写就好了 

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<string>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<algorithm>
      7 #include<queue>
      8 #include<stack>
      9 #include<set>
     10 #include<bitset>
     11 #include<vector>
     12 #include<cstdlib>
     13 #define QAQ int
     14 #define TAT long long
     15 #define OwO bool
     16 #define ORZ double
     17 #define Ug unsigned
     18 #define F(i,j,n) for(QAQ i=j;i<=n;++i)
     19 #define E(i,j,n) for(QAQ i=j;i>=n;--i)
     20 #define MES(i,j) memset(i,j,sizeof(i))
     21 #define MEC(i,j) memcpy(i,j,sizeof(j))
     22 #define mid (l+r>>1)
     23 #define ls (o<<1)
     24 #define rs (o<<1|1)
     25 #define LS l,mid,ls
     26 #define RS mid+1,r,rs
     27 
     28 using namespace std;
     29 const QAQ N=100005;
     30 
     31 QAQ n;
     32 struct Link{
     33     QAQ to,last,val;
     34 }a[N<<1];
     35 QAQ head[N],js;
     36 QAQ id[N],key[N];
     37 QAQ fa[N],son[N],size[N];
     38 QAQ top[N],val[N],deep[N];
     39 struct Seg{
     40     QAQ Max,ladd,lset;
     41     OwO flag;
     42 }tree[N<<2];
     43 char s[10];
     44 
     45 void add(QAQ x,QAQ y,QAQ z){
     46     a[++js].to=y;a[js].val=z;
     47     a[js].last=head[x];head[x]=js;
     48 }
     49 
     50 void dfs1(QAQ x,QAQ f){
     51     fa[x]=f;size[x]=1;
     52     for(QAQ i=head[x];i;i=a[i].last)if(a[i].to!=f){
     53         deep[a[i].to]=deep[x]+1;
     54         dfs1(a[i].to,x);
     55         val[a[i].to]=a[i].val;
     56         if(!son[x]||size[son[x]]<size[a[i].to]) son[x]=a[i].to;
     57         size[x]+=size[a[i].to];
     58     }
     59 }
     60 
     61 void dfs2(QAQ x,QAQ t){
     62     top[x]=t;id[x]=++js;key[js]=val[x];
     63     if(!son[x]) return ;
     64     dfs2(son[x],t);
     65     for(QAQ i=head[x];i;i=a[i].last) if(a[i].to!=fa[x]&&a[i].to!=son[x]) dfs2(a[i].to,a[i].to);
     66 }
     67 
     68 namespace Tree{
     69     void push_up(QAQ o){
     70         tree[o].Max=max(tree[ls].Max,tree[rs].Max);
     71     }
     72     
     73     void push_down(QAQ o){
     74         if(tree[o].flag){
     75             tree[ls].ladd=tree[rs].ladd=0;
     76             tree[ls].flag=tree[rs].flag=1;
     77             tree[ls].Max=tree[o].lset;
     78             tree[rs].Max=tree[o].lset;
     79             tree[ls].lset=tree[o].lset;
     80             tree[rs].lset=tree[o].lset;
     81             tree[o].lset=0;tree[o].ladd=0;
     82             tree[o].flag=0;
     83         }
     84         if(tree[o].ladd){
     85             tree[ls].ladd+=tree[o].ladd;
     86             tree[ls].lset+=tree[o].ladd;
     87             tree[ls].Max+=tree[o].ladd;
     88             tree[rs].ladd+=tree[o].ladd;
     89             tree[rs].lset+=tree[o].ladd;
     90             tree[rs].Max+=tree[o].ladd;
     91             tree[o].ladd=0;
     92         }
     93     }
     94     
     95     void build(QAQ l,QAQ r,QAQ o){
     96         if(l==r){
     97             tree[o].Max=key[l];
     98             return ;
     99         }
    100         build(LS);build(RS);
    101         push_up(o);
    102     }
    103     
    104     void change(QAQ l,QAQ r,QAQ o,QAQ x,QAQ y,QAQ z){
    105         if(l>=x&&r<=y){
    106             tree[o].Max=z;
    107             tree[o].ladd=0;
    108             tree[o].lset=z;
    109             tree[o].flag=1;
    110             return ;
    111         }
    112         push_down(o);
    113         if(x<=mid) change(LS,x,y,z);
    114         if(y>mid) change(RS,x,y,z);
    115         push_up(o);
    116     }
    117     
    118     void add(QAQ l,QAQ r,QAQ o,QAQ x,QAQ y,QAQ z){
    119         if(l>=x&&r<=y){
    120             tree[o].Max+=z;
    121             tree[o].ladd+=z;
    122             tree[o].lset+=z;
    123             return ;
    124         }
    125         push_down(o);
    126         if(x<=mid) add(LS,x,y,z);
    127         if(y>mid) add(RS,x,y,z);
    128         push_up(o);
    129     }
    130     
    131     QAQ query(QAQ l,QAQ r,QAQ o,QAQ x,QAQ y){
    132         if(l>=x&&r<=y) return tree[o].Max;
    133         push_down(o);
    134         QAQ ans=0;
    135         if(x<=mid) ans=max(ans,query(LS,x,y));
    136         if(y>mid) ans=max(ans,query(RS,x,y));
    137         return ans;
    138     }
    139 }
    140 
    141 void change(QAQ x,QAQ y,QAQ z){
    142     while(top[x]!=top[y]){
    143         if(deep[top[x]]<deep[top[y]]) swap(x,y);
    144         Tree::change(1,n,1,id[top[x]],id[x],z);
    145         x=fa[top[x]];
    146     }
    147     if(x==y) return ;
    148     if(deep[x]>deep[y]) swap(x,y);
    149     Tree::change(1,n,1,id[x]+1,id[y],z);
    150 }
    151 
    152 void Add(QAQ x,QAQ y,QAQ z){
    153     while(top[x]!=top[y]){
    154         if(deep[top[x]]<deep[top[y]]) swap(x,y);
    155         Tree::add(1,n,1,id[top[x]],id[x],z);
    156         x=fa[top[x]];
    157     }
    158     if(x==y) return ;
    159     if(deep[x]>deep[y]) swap(x,y);
    160     Tree::add(1,n,1,id[x]+1,id[y],z);
    161 }
    162 
    163 QAQ query(QAQ x,QAQ y){
    164     QAQ ans=0;
    165     while(top[x]!=top[y]){
    166         if(deep[top[x]]<deep[top[y]]) swap(x,y);
    167         ans=max(ans,Tree::query(1,n,1,id[top[x]],id[x]));
    168         x=fa[top[x]];
    169     }
    170     if(x==y) return ans;
    171     if(deep[x]>deep[y]) swap(x,y);
    172     ans=max(ans,Tree::query(1,n,1,id[x]+1,id[y]));
    173     return ans;
    174 }
    175 
    176 QAQ main(){
    177     scanf("%d",&n);
    178     F(i,1,n-1){
    179         QAQ u,v,w;
    180         scanf("%d%d%d",&u,&v,&w);
    181         add(u,v,w);add(v,u,w);
    182     }
    183     deep[1]=1;js=0;
    184     dfs1(1,0);
    185     dfs2(1,0);
    186     Tree::build(1,n,1);
    187     while(scanf("%s",s),s[0]!='S'){
    188         if(s[1]=='h'){
    189             QAQ k,w;
    190             scanf("%d%d",&k,&w);
    191             if(deep[a[k*2].to]>deep[a[k*2-1].to]) Tree::change(1,n,1,id[a[k*2].to],id[a[k*2].to],w);
    192             else Tree::change(1,n,1,id[a[k*2-1].to],id[a[k*2-1].to],w);
    193         }
    194         else if(s[1]=='o'){
    195             QAQ x,y,z;
    196             scanf("%d%d%d",&x,&y,&z);
    197             change(x,y,z);
    198         }
    199         else if(s[1]=='d'){
    200             QAQ x,y,z;
    201             scanf("%d%d%d",&x,&y,&z);
    202             Add(x,y,z);
    203         }
    204         else {
    205             QAQ x,y;
    206             scanf("%d%d",&x,&y);
    207             printf("%d
    ",query(x,y));
    208         }
    209     }
    210     return 0;
    211 }
    View Code
  • 相关阅读:
    HDU1069:Monkey and Banana(DP+贪心)
    hdu 4497 GCD and LCM(2013 ACM-ICPC吉林通化全国邀请赛——题目重现)
    vb6.0 倒计时
    硬盘分区表
    踽踽独行的岁月,感谢与你的相遇
    每天学点Linux:二
    windows下Qt Creator5.1.0编写程序以及调用OpenCV库
    【每周一译】愚蠢的指标:Java中使用最多的关键字
    centos 7 没有ifconfig 命令
    About stats collected
  • 原文地址:https://www.cnblogs.com/heower/p/8746917.html
Copyright © 2020-2023  润新知