BZOJ3083: A faraway country

Source: Internet
Author: User

Transmission Door

BZOJ100 (the correct rate of not being able to look directly at

The tree splits the board question, the notice and the DFS sequence union, according to the root Change transformation statistic way can.

Bzoj 3083//by cydiater//2016.10.23#include <iostream> #include <cstring> #include <string># Include <algorithm> #include <queue> #include <map> #include <ctime> #include <iomanip># Include <cstdlib> #include <cstdio> #include <cmath>using namespace std; #define LL Long long#define up ( i,j,n) for (int i=j;i<=n;i++) #define-down (i,j,n) for (int i=j;i>=n;i--) #define FILE "BBBBB" const int maxn=1e5+5; CONST LL Oo=1ll<<32;inline ll read () {char ch=getchar (); ll X=0,f=1;while (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int N,m,dep[maxn],link[maxn],len=0,fa[maxn][25],son[maxn],siz[maxn],top[maxn],seg[maxn],cnt=0,pos[maxn],root, Opt,l,r;ll v,pro[maxn];int c=0;struct edge{int y,next;} E[maxn<<1];struct tree{ll V,delta;} T[maxn<<3];namespace solution{inline void Insert (int x,int y) {e[++len].next=link[x]; Link[x]=len;e[len].y=y;} inline void reload (int root) {t[root].v=min (T[ROOT&LT;&LT;1].V,T[ROOT&LT;&LT;1|1].V);} inline void downit (int root) {if (t[root].delta==0) Return;int delta=t[root].delta;t[root].delta=0;t[root<<1]. Delta=delta;t[root<<1].v=delta;t[root<<1|1].delta=delta;t[root<<1|1].v=delta;} void Init () {n=read (); M=read (); up (i,2,n) {int x=read (), Y=read (); Insert (x, y); insert (y,x);} Up (I,1,n) pro[i]=read (); Root=read ();} void dfs1 (int node,int deep,int father) {Fa[node][0]=father;dep[node]=deep;son[node]=0;siz[node]=1;int max_siz=0;for (int i=link[node];i;i=e[i].next) if (e[i].y!=father) {DFS1 (E[i].y,deep+1,node); Siz[node]+=siz[e[i].y];if (Siz[e[i]. Y]>max_siz) {max_siz=siz[e[i].y];son[node]=e[i].y;}}} void dfs2 (int node,int TOP) {top[node]=top;seg[++cnt]=node;pos[node]=cnt;if (Son[node]) DFS2 (son[node],top); for (int i =link[node];i;i=e[i].next) if (E[i].y!=fa[node][0]&&e[i].y!=son[node]) DFS2 (E[I].Y,E[I].Y);} void Get_ancestor () {Up (i,1,21) up (node,1,n) if (fa[node][i-1]!=0) fa[node][i]=fa[fa[node][i-1]][i-1];} void BuiLD (int leftt,int rightt,int root) {if (leftt==rightt) {T[root].delta=0;t[root].v=pro[seg[leftt]];return;} int mid= (LEFTT+RIGHTT) >>1;build (leftt,mid,root<<1); build (mid+1,rightt,root<<1|1); Reload (Root) ;} void Build () {DFS1 (root,0,0);d FS2 (root,root); Get_ancestor (); Build (1,n,1);} int LCA (int x,int y) {if (x==y) return x;if (Dep[x]<dep[y]) swap (x, y);d Own (i,21,0) if (dep[x]-(1<<i) >=dep[y]) X=fa[x][i];if (x==y) return X;down (i,21,0) if (Fa[x][i]!=0&&fa[x][i]!=fa[y][i]) {x=fa[x][i];y=fa[y][i];} return fa[x][0];} void Updata (int leftt,int rightt,int root) {downit (root); if (leftt>r| | Rightt<l) return;if (leftt>=l&&rightt<=r) {T[root].delta=t[root].v=v;return;} int mid= (LEFTT+RIGHTT) >>1;updata (leftt,mid,root<<1); Updata (mid+1,rightt,root<<1|1); Reload ( root);} void Change (Int. X,int aim) {while (Top[x]!=top[aim]) {r=pos[x]; L=pos[top[x]];updata (1,n,1); x=fa[top[x]][0];} R=POS[X]; L=pos[aim];updata (1,n,1);} void change (int x,int y) {int Lca=lca (x, y); change (X,LCA); cHange (Y,LCA);} ll get (int leftt,int rightt,int root) {downit (root); if (leftt>r| | RIGHTT&LT;L) return oo;if (leftt>=l&&rightt<=r) return t[root].v;int mid= (LEFTT+RIGHTT) >>1; return min (Get (leftt,mid,root<<1), get (mid+1,rightt,root<<1|1));} void Slove () {while (m--) {opt=read (); if (opt==1) root=read (); else if (opt==2) {int x=read (), Y=read (); V=read (); Change (x, y);} Else{int Node=read (), Pos=pos[root];ll ans;int leftt=pos[node],rightt=leftt+siz[node]-1;if (POS==LEFTT) {L=1; R=n;ans=get (1,n,1);} else if (! ( Pos>=leftt&&pos<=rightt)) {L=LEFTT; R=rightt;ans=get (1,n,1);} else{for (int i=link[node];i;i=e[i].next) if (E[i].y!=fa[node][0]) {LEFTT=POS[E[I].Y];RIGHTT=POS[E[I].Y]+SIZ[E[I].Y ]-1;if (pos>=leftt&&pos<=rightt) {l=1; R=leftt-1;if (r>=l) ans=get (1,n,1); L=rightt+1,r=n;if (l<=r) ans=min (Ans,get (1,n,1)); printf ("%lld\n", ans);}}} int main () {//freopen (file ".", "R", stdin),//freopen (file ". Out", "w", stdout);//freopen ("Input.in", "R", stdin);// Freopen ("Out.out", "W", stdout); using namespace Solution;init (); Build (); Slove (); return 0;}

BZOJ3083: A distant country

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.