Bzoj2588:spoj 10628. Count on a tree chairman +dfs sequence

Source: Internet
Author: User
Tags hash

HZHWCMHF said, the title is the chairman of the tree, and then I said that according to the tree chain split, was not dropped, and later found that is the DFS sequence, although there is no big difference, but think carefully, the tree is completely redundant. I was too weak in the past.

#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <
Cstring> #define INF 0x7fffffff #define LL Long long #define N 100005 #define M 2000005 using namespace std;
    inline ll read () {ll x=0,f=1;char Ch=getchar (); 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,tot,siz,cnt,ind,last;
int num[n],pos[n];
int v[n],tmp[n],hash[n],root[n];
int ls[m],rs[m],sum[m];
int deep[n],fa[n][17]; struct Data{int to,next;}
E[200005];int Head[n]; void ins (int u,int v) {e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;} void Insert (int u,int v) {ins (u,v); ins (v,u);} int
    Find (int x) {int l=1,r=tot;
        while (l<=r) {int mid= (L+R) >>1;
        if (hash[mid]<x) l=mid+1;
        else if (hash[mid]==x) return mid;
    else r=mid-1;
    }} void Dfs (int x) {ind++;num[ind]=x;pos[x]=ind; for (int i=1;i<=16;i++) if ((1<<i) <=deep[x]) fa[x][i]=fa[fa[x][i-1]][i-1];
    else break;
            for (int i=head[x];i;i=e[i].next) if (fa[x][0]!=e[i].to) {deep[e[i].to]=deep[x]+1;
        DFS (E[I].TO);
    }} int LCA (int x,int y) {if (Deep[x]<deep[y]) swap (x, y);
    int t=deep[x]-deep[y];
    for (int i=0;i<=16;i++) if ((1<<i) &t) x=fa[x][i];
    for (int i=16;i>=0;i--) if (Fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
    if (x==y) return x;
return fa[x][0];
    } void Update (int l,int r,int x,int &y,int val) {siz++;
    if (l==r) return;
    int mid= (L+R) >>1;
    if (val<=mid) update (l,mid,ls[x],ls[y],val);
else update (mid+1,r,rs[x],rs[y],val);
    } int que (int x,int y,int k) {int A=x,b=y,c=lca (x, y), d=fa[c][0];
    int L=1,r=tot; while (l&LT;R) {int mid= (L+R) >>1;
        int Tmp=sum[ls[a]]+sum[ls[b]]-sum[ls[c]]-sum[ls[d]];
            if (tmp>=k) {r=mid;
        A=LS[A];B=LS[B];C=LS[C];d =ls[d];
            } else {k-=tmp;
        A=RS[A];B=RS[B];C=RS[C];d =rs[d];
}} return hash[l];
    } int main () {N=read (), M=read ();
    for (int i=1;i<=n;i++) V[i]=read (), tmp[i]=v[i];
    Sort (tmp+1,tmp+n+1);
    for (int i=2;i<=n;i++) if (tmp[i]!=tmp[i-1]) hash[++tot]=tmp[i];
    for (int i=1;i<=n;i++) V[i]=find (V[i]);
        for (int i=1;i<n;i++) {int u=read (), V=read ();
    Insert (U,V);
    } dfs (1);
    for (int i=1;i<=n;i++) {update (1,tot,root[pos[fa[num[i]][0]]],root[i],v[num[i]]);
        } for (int i=1;i<=m;i++) {int x=read (), Y=read (), Rk=read ();
        Last=que (X,Y,RK);
        printf ("%d", last); if (i!=m) printf ("\ n");
} return 0; }

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: 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.