zoukankan      html  css  js  c++  java
  • POJ

    吐槽:1. poj 没有万能库,不能用 auto 变量,差评!

       2. NEGATE 是取相反数的操作,我还以为是全部赋值0 ,题目表述不清,同时没有说明边权的数据范围;

       3. 我写代码时,忽略了 两次取反等于没取 ,该骂 (我骂我自己)

    题目:传送门

    思路:重链剖分,边权转点权

      1 //#include<bits/stdc++.h>
      2 #include<cstdio>
      3 #include<cmath>
      4 #include<cstring>
      5 #include<vector>
      6 #include<cctype>
      7 #include<queue>
      8 #include<algorithm>
      9 #include<map>
     10 #pragma GCC optimize(2)
     11 using namespace std;
     12 typedef long long LL;
     13 typedef pair<int,int> pii;
     14 typedef pair<double,double> pdd;
     15 const int N=1e4+5;
     16 const int inf=0x3f3f3f3f;
     17 const int mod=300;
     18 const double eps=1e-9;
     19 const long double pi=acos(-1.0L);
     20 #define ls (i<<1)
     21 #define rs (i<<1|1)
     22 #define fi first
     23 #define se second
     24 #define pb push_back
     25 #define mk make_pair
     26 #define mem(a,b) memset(a,b,sizeof(a))
     27 LL read()
     28 {
     29     LL x=0,t=1;
     30     char ch;
     31     while(!isdigit(ch=getchar())) if(ch=='-') t=-1;
     32     while(isdigit(ch)){ x=10*x+ch-'0'; ch=getchar(); }
     33     return x*t;
     34 }
     35 struct edge
     36 {
     37     int x,y,z;
     38     edge(){}
     39     edge(int X,int Y,int Z){ x=X,y=Y,z=Z; }
     40 }a[N];
     41 struct node{ int ma,mi; }c[N<<2];
     42 vector<pii> e[N];
     43 int lazy[N<<2];
     44 int deep[N],f[N],cnt[N],son[N],id[N],top[N],rk[N],tot,n;
     45 void dfs(int u,int pre)
     46 {
     47     f[u]=pre;
     48     deep[u]=deep[pre]+1;
     49     cnt[u]=1;
     50     for(int i=0;i<e[u].size();i++)
     51     {
     52         pii x=e[u][i];
     53         if(x.fi==pre) continue;
     54         dfs(x.fi,u);
     55         cnt[u]+=cnt[x.fi];
     56         if(cnt[son[u]]<cnt[x.fi]) son[u]=x.fi;
     57     }
     58 }
     59 void dfs2(int u,int pre,int t)
     60 {
     61     id[u]=++tot;
     62     rk[tot]=u;
     63     top[u]=t;
     64     if(son[u]) dfs2(son[u],u,t);
     65     for(int i=0;i<e[u].size();i++)
     66     {
     67         pii x=e[u][i];
     68         if(x.fi==pre||x.fi==son[u]) continue;
     69         dfs2(x.fi,u,x.fi);
     70     }
     71 }
     72 inline void pushdown(int i)
     73 {
     74     c[ls].ma=-c[ls].ma;
     75     c[ls].mi=-c[ls].mi;
     76     swap(c[ls].ma,c[ls].mi);
     77     c[rs].ma=-c[rs].ma;
     78     c[rs].mi=-c[rs].mi;
     79     swap(c[rs].ma,c[rs].mi);
     80     lazy[ls]^=1,lazy[rs]^=1;
     81     lazy[i]=0;
     82 }
     83 void update(int i,int l,int r,int ll,int rr)
     84 {
     85     if(ll<=l&&r<=rr)
     86     {
     87         lazy[i]^=1;
     88         c[i].ma=-c[i].ma;
     89         c[i].mi=-c[i].mi;
     90         swap(c[i].ma,c[i].mi);
     91         return ;
     92     }
     93     if(lazy[i]) pushdown(i);
     94     int mid=l+r>>1;
     95     if(mid>=ll) update(ls,l,mid,ll,rr);
     96     if(mid<rr) update(rs,mid+1,r,ll,rr);
     97     c[i].ma=max(c[ls].ma,c[rs].ma);
     98     c[i].mi=min(c[ls].mi,c[rs].mi);
     99 }
    100 void update2(int i,int l,int r,int pos,int val)
    101 {
    102     if(l==r)
    103     {
    104         c[i].ma=c[i].mi=val;
    105         return ;
    106     }
    107     if(lazy[i]) pushdown(i);
    108     int mid=l+r>>1;
    109     if(mid>=pos) update2(ls,l,mid,pos,val);
    110     else update2(rs,mid+1,r,pos,val);
    111     c[i].ma=max(c[ls].ma,c[rs].ma);
    112     c[i].mi=min(c[ls].mi,c[rs].mi);
    113 }
    114 int query(int i,int l,int r,int ll,int rr)
    115 {
    116     if(ll<=l&&r<=rr) return c[i].ma;
    117     if(lazy[i]) pushdown(i);
    118     int mid=l+r>>1;
    119     int t1=-inf,t2=-inf;
    120     if(mid>=ll) t1=query(ls,l,mid,ll,rr);
    121     if(mid<rr) t2=query(rs,mid+1,r,ll,rr);
    122     return max(t1,t2);
    123 }
    124 void change(int x,int y)
    125 {
    126     int fx=top[x],fy=top[y];
    127     while(fx!=fy)
    128     {
    129         if(deep[fx]>=deep[fy]) update(1,1,n,id[fx],id[x]),x=f[fx],fx=top[x];
    130         else update(1,1,n,id[fy],id[y]),y=f[fy],fy=top[y];
    131     }
    132     int L=min(id[x],id[y])+1;
    133     int R=max(id[x],id[y]);
    134     if(L<=R) update(1,1,n,L,R);
    135 }
    136 int getm(int x,int y)
    137 {
    138     int fx=top[x],fy=top[y],res=-inf;
    139     while(fx!=fy)
    140     {
    141         if(deep[fx]>=deep[fy]) res=max(res,query(1,1,n,id[fx],id[x])),x=f[fx],fx=top[x];
    142         else res=max(res,query(1,1,n,id[fy],id[y])),y=f[fy],fy=top[y];
    143     }
    144     int L=min(id[x],id[y])+1;
    145     int R=max(id[x],id[y]);
    146     if(L<=R) res=max(res,query(1,1,n,L,R));
    147     return res;
    148 }
    149 inline void init()
    150 {
    151     tot=0;
    152     mem(lazy,0);
    153     mem(c,0);
    154     mem(son,0);
    155     for(int i=1;i<=n;i++)
    156         e[i].clear();
    157 }
    158 int main()
    159 {
    160     int T=read();
    161     while(T--)
    162     {
    163         n=read();
    164         for(int i=1;i<n;i++)
    165         {
    166             int x=read(),y=read(),z=read();
    167             a[i]=edge(x,y,z);
    168             e[x].pb(mk(y,z));
    169             e[y].pb(mk(x,z));
    170         }
    171         dfs(1,0); dfs2(1,0,1);
    172         for(int i=1;i<n;i++)
    173         {
    174             int pos=max(id[a[i].x],id[a[i].y]);
    175             update2(1,1,n,pos,a[i].z);
    176         }
    177         char cmd[11];
    178         scanf("%s",cmd);
    179         while(cmd[0]!='D')
    180         {
    181             int x=read(),y=read();
    182             if(cmd[0]=='Q') printf("%d
    ",getm(x,y));
    183             else if(cmd[0]=='C')
    184             {
    185                 int pos=max(id[a[x].x],id[a[x].y]);
    186                 update2(1,1,n,pos,y);
    187             }
    188             else change(x,y);
    189             scanf("%s",cmd);
    190         }
    191         init();
    192     }
    193     return 0;
    194 }
    195 /*
    196 2
    197 3
    198 1 2 1
    199 2 3 2
    200 QUERY 1 2
    201 CHANGE 1 3
    202 QUERY 1 2
    203 QUERY 2 2
    204 NE 1 3
    205 QUERY 1 2
    206 Query 1 3
    207 DONE
    208 3
    209 1 2 1
    210 1 3 2
    211 QUERY 3 2
    212 CHANGE 1 3
    213 QUERY 2 3
    214 QUERY 2 2
    215 NE 1 2
    216 QUERY 2 3
    217 DONE
    218 */
    View Code
  • 相关阅读:
    [ZZ]终极期望
    推荐一部好电影
    读书笔记:《Java2 教程》(五)
    波音飞机的消息
    雪景
    [ZZ]候捷谈Java反射机制
    关于J2ME开发的感想(20060505更新)
    读书笔记:《Java2 教程》(七)
    读书笔记:《Java2 教程》(六)
    注册了Bloglines
  • 原文地址:https://www.cnblogs.com/DeepJay/p/12809522.html
Copyright © 2011-2022 走看看