BZOJ 2588 Spoj 10628. Count on a tree
来源:互联网 发布:放大电路 知乎 编辑:程序博客网 时间:2024/06/08 06:18
主席树+LCA
对于每个点,建一棵该点到根路径上的权值线段树,用主席树搞一搞,对于每个询问(a,b),记c是他们的LCA,d是c父亲,用siz[a]+siz[b]-siz[c]-siz[d]查找即可- -
WA到吐血才发现自己lower_bound里面的cnt打成n了。。。QAQ
#include<cstdio>#include<cmath>#include<iostream>#include<algorithm>#define N 100005 using namespace std;int w[N], v[N], n, m, cnt, fa[N][20], dep[N], last[N]; struct edge{ int next,to;}e[N<<1];int e_cnt=0;void add(int a, int b){ e[++e_cnt]=(edge){last[a],b}; last[a]=e_cnt;}struct node{ node *ch[2]; int cnt, siz; node() { ch[0]=ch[1]=NULL; cnt=siz=0; } void update() { siz=cnt; if(ch[0])siz+=ch[0]->siz; if(ch[1])siz+=ch[1]->siz; }}*root[N]={NULL}, *null=new node(), q[2000000];int q_cnt=0;void inser(node *&x, node *&y, int l, int r, int pos){ if(x==NULL)x=null; y=&q[++q_cnt]; if(l==r) { *y=*x; y->siz++; y->cnt++; return; } int mid=(l+r)>>1; if(pos<=mid) { y->ch[1]=x->ch[1]; inser(x->ch[0],y->ch[0],l,mid,pos); y->update(); } else { y->ch[0]=x->ch[0]; inser(x->ch[1],y->ch[1],mid+1,r,pos); y->update(); }}void dfs(int x){ inser(root[fa[x][0]],root[x],1,cnt,w[x]); dep[x]=dep[fa[x][0]]+1; for(int i = 1; (1<<i)<dep[x]; i++) fa[x][i]=fa[fa[x][i-1]][i-1]; for(int i = last[x]; i; i=e[i].next) { if(e[i].to!=fa[x][0]) { fa[e[i].to][0]=x; dfs(e[i].to); } }}int LCA(int a, int b){ if(dep[a]<dep[b])swap(a,b); for(int d=dep[a]-dep[b], i = 0; d; i++) if(d&(1<<i))a=fa[a][i],d^=(1<<i); for(int i = 19; i >= 0; i--) { if(fa[a][i]!=fa[b][i]) a=fa[a][i],b=fa[b][i]; } if(a==b)return a; else return fa[a][0];}int query(node *a, node *b, node *c, node *d, int l, int r, int k){ if(a==NULL)a=null; if(b==NULL)b=null; if(c==NULL)c=null; if(d==NULL)d=null; if(l==r)return v[l]; int size = 0; if(a->ch[0])size+=a->ch[0]->siz; if(b->ch[0])size+=b->ch[0]->siz; if(c->ch[0])size-=c->ch[0]->siz; if(d->ch[0])size-=d->ch[0]->siz; int mid=(l+r)>>1; if(k<=size)return query(a->ch[0],b->ch[0],c->ch[0],d->ch[0],l,mid,k); else return query(a->ch[1],b->ch[1],c->ch[1],d->ch[1],mid+1,r,k-size);}int solve(int a, int b, int k){ int c=LCA(a,b), d=fa[c][0]; return query(root[a],root[b],root[c],root[d],1,cnt,k);}int main(){ scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++) { scanf("%d",&w[i]); v[i]=w[i]; } for(int i = 1; i < n; i++) { int a, b; scanf("%d%d",&a,&b); add(a,b);add(b,a); } sort(v+1,v+n+1); cnt = unique(v+1,v+n+1)-v-1; for(int i = 1; i <= n; i++) { w[i]=lower_bound(v+1,v+cnt+1,w[i])-v; } dfs(1); for(int i = 1, ans = 0; i <= m; i++) { int a, b, c; scanf("%d%d%d",&a,&b,&c); a^=ans; ans=solve(a,b,c); printf("%d",ans); if(i!=m)printf("\n"); }}
1 0
- BZOJ 2588: Spoj 10628. Count on a tree|主席树
- bzoj 2588 Spoj 10628. Count on a tree
- bzoj 2588 Spoj 10628. Count on a tree
- [主席树] BZOJ 2588 Spoj 10628. Count on a tree
- BZOJ 2588==Spoj 10628. Count on a tree
- BZOJ 2588 Spoj 10628. Count on a tree
- 【BZOJ】2588: Spoj 10628. Count on a tree
- BZOJ 2588 Spoj 10628. Count on a tree 主席树
- BZOJ 2588: Spoj 10628. Count on a tree
- 2588: Spoj 10628. Count on a tree
- 2588: Spoj 10628. Count on a tree
- 2588: Spoj 10628. Count on a tree
- BZOJ 2588/SPOJ Count on a Tree LCA+线段树
- BZOJ 2588 Spoj 10628 Count on a tree
- BZOJ BZOJ 2588: Spoj 10628. Count on a tree 树上主席树
- 【BZOJ 2588】 Spoj 10628. Count on a tree|树上K大|树链剖分|主席树
- bzoj 2588: Spoj 10628. Count on a tree(树上主席树)
- [bzoj 2588] Spoj 10628. Count on a tree:函数式线段树
- 逃离x86架构-----CPU体系结构CISC与RISC之争
- unity小地图制作和方向导航
- fopen函数和fread函数、fwrite函数
- Erlang 速查表 v1.0
- android对手机通讯录的操作
- BZOJ 2588 Spoj 10628. Count on a tree
- 实现photoshop 正常混合的shader
- Unity5中新的Shader体系简析
- Qt movie图片发布时动画效果不显示问题
- JDK里的设计模式
- Android 必知必会-使用 supportV4 的 RoundedBitmapDrawable 实现圆角
- 欢迎进入阿钟的博客导航——快速查找所有博客
- fedora安装codeblocks 亲测可用
- 从零开始学习SLAM