• BZOJ1036: [ZJOI2008]树的统计Count


    1036: [ZJOI2008]树的统计Count

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 5084  Solved: 2156
    [Submit][Status]

    Description

    一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w。我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问从点u到点v的路径上的节点的权值和 注意:从点u到点v的路径上的节点包括u和v本身

    Input

    输入的第一行为一个整数n,表示节点的个数。接下来n – 1行,每行2个整数a和b,表示节点a和节点b之间有一条边相连。接下来n行,每行一个整数,第i行的整数wi表示节点i的权值。接下来1行,为一个整数q,表示操作的总数。接下来q行,每行一个操作,以“CHANGE u t”或者“QMAX u v”或者“QSUM u v”的形式给出。 对于100%的数据,保证1<=n<=30000,0<=q<=200000;中途操作中保证每个节点的权值w在-30000到30000之间。

    Output

    对于每个“QMAX”或者“QSUM”的操作,每行输出一个整数表示要求输出的结果。

    Sample Input

    4
    1 2
    2 3
    4 1
    4 2 1 3
    12
    QMAX 3 4
    QMAX 3 3
    QMAX 3 2
    QMAX 2 3
    QSUM 3 4
    QSUM 2 1
    CHANGE 1 5
    QMAX 3 4
    CHANGE 3 6
    QMAX 3 4
    QMAX 2 4
    QSUM 3 4

    Sample Output

    4
    1
    2
    2
    10
    6
    5
    6
    5
    16

    HINT

     

    Source

    题解:
    原来树链剖分水过。
    LCT大法好,不用码SGT了。。。注意access的时候也要pushup
    代码:
      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<cmath>
      4 #include<cstring>
      5 #include<algorithm>
      6 #include<iostream>
      7 #include<vector>
      8 #include<map>
      9 #include<set>
     10 #include<queue>
     11 #include<string>
     12 #define inf 1000000000
     13 #define maxn 30000+1000
     14 #define maxm 500+100
     15 #define eps 1e-10
     16 #define ll long long
     17 #define pa pair<int,int>
     18 using namespace std;
     19 inline int read()
     20 {
     21     int x=0,f=1;char ch=getchar();
     22     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     23     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
     24     return x*f;
     25 }
     26 struct edge{int go,next;}e[2*maxn];
     27 int n,m,tot,head[maxn],fa[maxn],c[maxn][2],sta[maxn],next[maxn],mx[maxn],sum[maxn],v[maxn];
     28 bool rev[maxn];
     29 inline void insert(int x,int y)
     30 {
     31     e[++tot].go=y;e[tot].next=head[x];head[x]=tot;
     32     e[++tot].go=x;e[tot].next=head[y];head[y]=tot;
     33 }
     34 inline bool isroot(int x)
     35 {
     36     return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
     37 }
     38 inline void pushup(int x)
     39 {
     40     mx[x]=max(v[x],max(mx[c[x][0]],mx[c[x][1]]));
     41     sum[x]=sum[c[x][0]]+sum[c[x][1]]+v[x];
     42 }
     43 void rotate(int x)
     44 {
     45     int y=fa[x],z=fa[y],l=(c[y][1]==x),r=l^1;
     46     if(!isroot(y))c[z][c[z][1]==y]=x;
     47     fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
     48     c[y][l]=c[x][r];c[x][r]=y;
     49     pushup(y);pushup(x);
     50 }
     51 void pushdown(int x)
     52 {
     53     if(!rev[x])return;
     54     rev[x]^=1;rev[c[x][0]]^=1;rev[c[x][1]]^=1;
     55     swap(c[x][0],c[x][1]);
     56 }
     57 void splay(int x)
     58 {
     59     int top=0;sta[++top]=x;
     60     for(int y=x;!isroot(y);y=fa[y])sta[++top]=fa[y];
     61     for(;top;)pushdown(sta[top--]);
     62     while(!isroot(x))
     63     {
     64         int y=fa[x],z=fa[y];
     65         if(!isroot(y))
     66          {
     67              if(c[z][0]==y^c[y][0]==x)rotate(x);else rotate(y);
     68          }
     69         rotate(x); 
     70     }
     71     
     72 }
     73 void access(int x)
     74 {
     75     for(int y=0;x;x=fa[x])
     76     {
     77         splay(x);c[x][1]=y;pushup(x);y=x;
     78     }
     79 }
     80 void makeroot(int x)
     81 {
     82     access(x);splay(x);rev[x]^=1;
     83 }
     84 int find(int x)
     85 {
     86     access(x);splay(x);
     87     while(c[x][0])x=c[x][0];
     88     return x;
     89 }
     90 void link(int x,int y)
     91 {
     92     makeroot(x);fa[x]=y;splay(x);
     93 }
     94 void cut(int x,int y)
     95 {
     96     makeroot(x);access(y);splay(y);c[y][0]=fa[x]=0;
     97 }
     98 int main()
     99 {
    100     freopen("input.txt","r",stdin);
    101     freopen("output.txt","w",stdout);
    102     n=read();
    103     int x,y,top=0;char ch[10];
    104     for(int i=1;i<n;i++)x=read(),y=read(),insert(x,y);
    105     for(int i=1;i<=n;i++)mx[i]=sum[i]=v[i]=read();
    106     sta[++top]=1;
    107     for(int i=1;i<=top;i++)
    108      for(int k=sta[i],j=head[k];j;j=e[j].next)
    109       if(e[j].go!=fa[k])
    110        {
    111            fa[y=e[j].go]=k;sta[++top]=y;
    112        }  
    113     mx[0]=-inf;
    114     m=read();
    115     while(m--)
    116     {
    117         scanf("%s",ch);x=read();y=read();
    118         if(ch[0]=='C')
    119         {
    120             makeroot(x);v[x]=y;pushup(x);
    121         }
    122         else
    123         {
    124             makeroot(x);
    125             access(y);splay(y);printf("%d
    ",ch[1]=='M'?mx[y]:sum[y]);
    126         }
    127     }   
    128     return 0;
    129 }
    View Code
  • 相关阅读:
    python的三大控制机构(ifelse、for、while)
    python 异常处理
    《精通javascript》笔记
    IE6与!important
    point
    js 自制滚动条
    《Head first 设计模式》读书笔记
    Asp.net Webform 数据源绑定控件的扩展(懒人的办法):DropDownList
    Asp.net Binarysoft.Library 数据库通用操作层(Binarysoft.Library.Data)
    Asp.net Webform 从项目的数据库设计说起,什么是一个好的数据库设计。
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3946265.html
Copyright © 2020-2023  润新知