链剖-What you are?-大话西游-校内oj2440
This article is made by Jason-Cow.
Welcome to reprint.
But please post the writer's address.
http://www.cnblogs.com/JasonCow/
链剖+线段树
所以为什么 2017.4.8 C题爆零了!!!
我的暴力分呢?
大话西游AC code 假装考试30分拿到了T△T
1 #include <algorithm> 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <vector> 7 #include <queue> 8 #include <map> 9 #include <set> 10 using namespace std; 11 #define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout) 12 const int maxn=(int)1e5+10,maxm=(int)2e5+10; 13 struct edge{int v,next;}e[maxm]; 14 struct cuttochain{int fa,dep,size,son,top;}T[maxn]; 15 int head[maxn],cnt,idx,n,Q,dfn[maxn],Rank[maxn],U[maxn],V[maxn]; 16 long long W[maxn]; 17 void add(int u,int v){e[++cnt]=(edge){v,head[u]},head[u]=cnt;} 18 void Add(int i){scanf("%d%d",&U[i],&V[i]);add(U[i],V[i]),add(V[i],U[i]);} 19 void dfs1(int u,int fa){ 20 T[u].fa=fa,T[u].dep=T[fa].dep+1,T[u].size=1; 21 for(int i=head[u];i;i=e[i].next){ 22 if(e[i].v!=fa){ 23 dfs1(e[i].v,u);T[u].size+=T[e[i].v].size; 24 if(T[u].son==0||T[e[i].v].size>T[T[u].son].size)T[u].son=e[i].v; 25 } 26 } 27 } 28 void dfs2(int u,int top){ 29 T[u].top=top,dfn[u]=++idx,Rank[dfn[u]]=u; 30 if(T[u].son)dfs2(T[u].son,top); 31 for(int i=head[u];i;i=e[i].next){ 32 if(e[i].v!=T[u].fa && e[i].v!=T[u].son)dfs2(e[i].v,e[i].v); 33 } 34 } 35 #define ls (x<<1) 36 #define rs (x<<1|1) 37 #define mid ((l+r)>>1) 38 struct segmenttree{long long max,min,sum;}E[maxn<<2]; 39 void up(int x){ 40 E[x].max = max(E[ls].max,E[rs].max); 41 E[x].min = min(E[ls].min,E[rs].min); 42 E[x].sum = E[ls].sum+E[rs].sum; 43 } 44 void build(int x,int l,int r){ 45 if(l==r){E[x].max=E[x].sum=E[x].min=W[Rank[l]];} 46 else{build(ls,l,mid),build(rs,mid+1,r);up(x);} 47 } 48 long long Max(int x,int l,int r,int L,int R){ 49 long long ans=0; 50 if(L<=l&&r<=R)return E[x].max; 51 else{ 52 if(R<=mid)ans=Max(ls,l,mid,L,R); 53 else if(L>mid)ans=Max(rs,mid+1,r,L,R); 54 else ans=max( Max(ls,l,mid,L,R) , Max(rs,mid+1,r,L,R) ); 55 } 56 return ans; 57 } 58 long long Min(int x,int l,int r,int L,int R){ 59 long long ans=(int)1e8+10; 60 if(L<=l&&r<=R)return E[x].min; 61 else{ 62 if(R<=mid)ans=Min(ls,l,mid,L,R); 63 else if(L>mid)ans=Min(rs,mid+1,r,L,R); 64 else ans=min( Min(ls,l,mid,L,R) , Min(rs,mid+1,r,L,R) ); 65 } 66 return ans; 67 } 68 long long Sum(int x,int l,int r,int L,int R){ 69 long long ans=0; 70 if(L<=l&&r<=R)return E[x].sum; 71 else{ 72 if(R<=mid)ans=Sum(ls,l,mid,L,R); 73 else if(L>mid)ans=Sum(rs,mid+1,r,L,R); 74 else ans=Sum(ls,l,mid,L,R) + Sum(rs,mid+1,r,L,R); 75 } 76 return ans; 77 } 78 void updata(int x,int l,int r,int P,int val){ 79 if(l==r){E[x].max=E[x].sum=E[x].min=val;} 80 else{ 81 if(P<=mid)updata(ls,l,mid,P,val); 82 else updata(rs,mid+1,r,P,val); 83 up(x); 84 } 85 } 86 void update(int x,int val){int id=dfn[x];updata(1,1,n,id,val);} 87 long long query(int x){ 88 long long MAX1=0,MIN1=(int)1e8+10; 89 long long MAX2=0,MIN2=(int)1e8+10; 90 if(T[U[x]].dep>T[V[x]].dep){ 91 MAX1=Max(1,1,n,dfn[U[x]],dfn[U[x]]+T[U[x]].size-1); 92 MIN1=Min(1,1,n,dfn[U[x]],dfn[U[x]]+T[U[x]].size-1); 93 int LL=1,LR=dfn[U[x]]-1,RL=dfn[U[x]]+T[U[x]].size,RR=n; 94 if(LL<=LR){ 95 MAX2=max(MAX2,Max(1,1,n,LL,LR)); 96 MIN2=min(MIN2,Min(1,1,n,LL,LR)); 97 } 98 if(RL<=RR){ 99 MAX2=max(MAX2,Max(1,1,n,RL,RR)); 100 MIN2=min(MIN2,Min(1,1,n,RL,RR)); 101 } 102 } 103 else{ 104 MAX1=Max(1,1,n,dfn[V[x]],dfn[V[x]]+T[V[x]].size-1); 105 MIN1=Min(1,1,n,dfn[V[x]],dfn[V[x]]+T[V[x]].size-1); 106 int LL=1,LR=dfn[V[x]]-1,RL=dfn[V[x]]+T[V[x]].size,RR=n; 107 if(LL<=LR){ 108 MAX2=max(MAX2,Max(1,1,n,LL,LR)); 109 MIN2=min(MIN2,Min(1,1,n,LL,LR)); 110 } 111 if(RL<=RR){ 112 MAX2=max(MAX2,Max(1,1,n,RL,RR)); 113 MIN2=min(MIN2,Min(1,1,n,RL,RR)); 114 } 115 } 116 //cout<<"MAX1="<<MAX1<<endl;cout<<"MIN1="<<MIN1<<endl;cout<<"MAX2="<<MAX2<<endl;cout<<"MIN2="<<MIN2<<endl; 117 cout<<MAX1*MIN1+MAX2*MIN2<<endl; 118 return 0; 119 } 120 121 int main(){ 122 file("a"); 123 scanf("%d%d",&n,&Q); 124 for(int i=1;i<=n;i++)scanf("%lld",&W[i]); 125 for(int i=1;i<n;i++)Add(i); 126 dfs1(1,0),dfs2(1,1),build(1,1,n); 127 for(int i=1;i<=Q;i++){ 128 char o[15];scanf("%s",o+1); 129 if(o[1]=='Q'){int x;scanf("%d",&x);query(x);} 130 else{int id;long long val;scanf("%d%lld",&id,&val);update(id,val);} 131 } 132 return 0; 133 }
~~Jason_liu O(∩_∩)O