"Dfs sequence" "Mo Team Algorithm" "Weights chunking" bzoj1803 Spoj1487 Query on a tree III

Source: Internet
Author: User

Basically the same as this, just ask for something very different.

Http://www.cnblogs.com/autsky-jadek/p/4159897.html

#include <cstdio> #include <algorithm> #include <cmath>using namespace Std;int num,ch[12],f,c;inline    void R (int &x) {c=0;f=1; for (;c< ' 0 ' | |    C> ' 9 '; C=getchar ()) if (c== '-') f=-1;    for (x=0;c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) (x*=10) + = (c ' 0 '); X*=f;}    inline void P (int x) {if (x<10) putchar (x+ ' 0 '); Else{p (X/10);p Utchar (x%10+ ' 0 ');}} #define MAXN 100001int Num[maxn],num2[maxn],l[maxn],sum=1,b[maxn],w[maxn],now,from,to;int es,n,m,v[200001],first[ Maxn],next[200001],a[maxn],ma[maxn],en,cnts[maxn];int Lt[maxn],rt[maxn],x,k,anss[maxn],fdfn[maxn];bool vis[ 100001];struct point{int v,p;} T[maxn];struct ask{int l,r,k,p;} Q[10001];bool operator < (const point &a,const point &b) {return A.V&LT;B.V;} BOOL operator < (const ask &a,const ask &b) {return NUM2[A.L]!=NUM2[B.L]? NUM2[A.L]&LT;NUM2[B.L]: A.R&LT;B.R;} void Addedge (const int &u,const int &v) {v[++es]=v; next[es]=first[u]; first[u]=es;} void Dfs (int U) {lt[u]=++now; fdfn[now]=u; t[noW].v=w[u]; T[now].p=now;    Vis[u]=1;    for (int i=first[u];i;i=next[i]) if (!vis[v[i]]) DFS (v[i]); Rt[u]=now;}    void Val_make_block () {int sz=sqrt (en), if (!sz) sz=1;        for (; sum*sz<en;++sum) {l[sum]= (sum-1) *sz+1; int r=sum*sz;      for (int i=l[sum];i<=r;++i) num[i]=sum;    } l[sum]= (sum-1) *sz+1; for (int i=l[sum];i<=en;++i) num[i]=sum;}    void Mo_make_block () {int sum=1,sz=sqrt (n); if (!sz) sz=1;        for (; sum*sz<n;++sum) {int r=sum*sz;      for (int i= (sum-1) *sz+1;i<=r;++i) num2[i]=sum; } for (int i= (sum-1) *sz+1;i<=n;++i) num2[i]=sum;} void Insert (const int &x) {++b[x]; ++cnts[num[x]];} void Delete (const int &x) {--b[x];--cnts[num[x]];}    int Kth (const int &x) {int cnt=0; for (int i=1;;        ++i) {cnt+=cnts[i];            if (cnt>=x) {cnt-=cnts[i]; for (int j=l[i];;          ++J) {cnt+=b[j]; if (cnt>=x) return J;} }}}int Main () {R (n); for (int i=1;i<=n;++i) R (w[I]); for (int. i=1;i<n;++i) {R (from);  R (to);  Addedge (from,to);  Addedge (To,from); }dfs (1);    Sort (t+1,t+n+1);    MA[A[T[1].P]=++EN]=FDFN[T[1].P];        for (int i=2;i<=n;++i) {if (T[I].V!=T[I-1].V) ++en;      MA[A[T[I].P]=EN]=FDFN[T[I].P]; } val_make_block ();    R (m); for (int i=1;i<=m;++i) {R (X); R (K); Q[I].L=LT[X]; Q[I].R=RT[X]; Q[i].k=k; Q[i].p=i;}    Mo_make_block (); sort (q+1,q+m+1); for (int i=q[1].l;i<=q[1].r;++i) Insert (A[i]);    Anss[q[1].p]=ma[kth (Q[1].K)];        for (int i=2;i<=m;++i) {if (Q[I].L&LT;Q[I-1].L) for (int j=q[i-1].l-1;j>=q[i].l;--j) Insert (A[j]);        else for (int j=q[i-1].l;j<q[i].l;++j) Delete (A[j]);        if (Q[I].R&LT;Q[I-1].R) for (int j=q[i-1].r;j>q[i].r;--j) Delete (A[j]);        else for (int j=q[i-1].r+1;j<=q[i].r;++j) Insert (A[j]);      Anss[q[i].p]=ma[kth (Q[I].K)]; } for (int i=1;i<=m;++i) P (Anss[i]), puts (""); return 0;}

  

"Dfs sequence" "Mo Team Algorithm" "Weights chunking" bzoj1803 Spoj1487 Query on a tree III

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.