bzoj 1036

颓废1个月。。。

开始学链剖了。。

  1 #include<bits/stdc++.h>
  2 #define inc(i,l,r) for(i=l;i<=r;i++)
  3 #define dec(i,l,r) for(i=l;i>=r;i--)
  4 #define link(x) for(edge *j=h[x];j;j=j->next)
  5 #define mem(a) memset(a,0,sizeof(a))
  6 #define inf 1e9
  7 #define ll long long
  8 #define succ(x) (1<<x)
  9 #define NM 30000+5
 10 using namespace std;
 11 int read(){
 12     int x=0,f=1;char ch=getchar();
 13     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
 14     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
 15     return x*f;
 16 }
 17 struct info{
 18     int m,s;
 19 }T[3*NM],null;
 20 struct edge{
 21     int t;
 22     edge *next;
 23 }e[2*NM],*h[NM],*p=e;
 24 int n,m,f[NM],d[NM],size[NM],son[NM],top[NM],a[NM],id[NM],_id[NM],TOP,tot;
 25 int _x,_y,l,r,i;
 26 char opt[100];
 27 info operator+(const info&x,const info&y){
 28     info f;
 29     f.s=x.s+y.s;
 30     f.m=max(x.m,y.m);
 31     return f;
 32 }
 33 void add(int x,int y){
 34     p->t=y;p->next=h[x];h[x]=p;p++;
 35 }
 36 void dfs1(int x){
 37     link(x)
 38     if(!f[j->t]){
 39         f[j->t]=x;
 40         d[j->t]=d[x]+1;
 41         dfs1(j->t);
 42         size[x]+=size[j->t];
 43         if(size[j->t]>size[son[x]])son[x]=j->t;
 44     }
 45     size[x]++;
 46 }
 47 void dfs2(int x){
 48     id[x]=++tot;top[x]=TOP;_id[tot]=x;
 49     if(son[x])dfs2(son[x]);
 50     link(x)
 51     if(!top[j->t])
 52     dfs2(TOP=j->t);
 53 }
 54 void build(int i,int x,int y){
 55     int t=x+y>>1;
 56     if(x==y){
 57         T[i].m=T[i].s=a[_id[x]];
 58         return;
 59     }
 60     build(i<<1,x,t);
 61     build(i<<1|1,t+1,y);
 62     T[i]=T[i<<1]+T[i<<1|1];
 63 }
 64 void ch(int i,int x,int y){
 65     int t=x+y>>1;
 66     if(x==y){
 67         T[i].m=T[i].s=_y;
 68         return;
 69     }
 70     if(_x<=t)ch(i<<1,x,t);
 71     else ch(i<<1|1,t+1,y);
 72     T[i]=T[i<<1]+T[i<<1|1];
 73 }
 74 info query(int i,int x,int y){
 75     int t=x+y>>1;
 76     if(l<=x&&y<=r)return T[i];
 77     if(r<x||y<l)return null;
 78     return query(i<<1,x,t)+query(i<<1|1,t+1,y);
 79 }
 80 int main(){
 81     null.s=0;null.m=-inf;
 82     n=read();
 83     inc(i,1,n-1){
 84         _x=read();_y=read();
 85         add(_x,_y);add(_y,_x);
 86     }
 87     inc(i,1,n)a[i]=read();
 88     f[1]=1;
 89     dfs1(1);
 90     dfs2(TOP=1);
 91     build(1,1,n);
 92     m=read();
 93     while(m--){
 94         info _t;
 95         scanf("%s",opt);_x=read();_y=read();
 96         _t.s=0;_t.m=-inf;
 97         if(opt[0]=='C'){
 98             a[_x]=_y;_x=id[_x];
 99             ch(1,1,n);
100         }else{
101             while(top[_x]!=top[_y]){
102                 if(d[top[_x]]<d[top[_y]])swap(_x,_y);
103                 l=id[top[_x]];r=id[_x];
104                 _t=_t+query(1,1,n);
105                 _x=f[top[_x]];
106             }
107             l=id[_x];r=id[_y];
108             if(l>r)swap(l,r);
109             _t=_t+query(1,1,n);
110             if(opt[1]=='M')printf("%d
",_t.m);
111             else printf("%d
",_t.s);
112         }
113     }
114 }
View Code
原文地址:https://www.cnblogs.com/onlyRP/p/5041188.html