【BZOJ】2588 Spoj 10628. Count on a tree LCA+主席树
来源:互联网 发布:c语言根号2 编辑:程序博客网 时间:2024/05/24 01:17
题目传送门
如果是强制在线的话,那就只能用主席树了。
这题的主席树建立方法也是挺好的,每个节点向它的父亲节点建立主席树。
对于每个询问
附上AC代码:
#include <cstdio>#include <cctype>#include <algorithm>using namespace std;const int N=1e5+10;struct side{ int to,nt;}s[N<<1];struct tree{ int lt,rt,sum;}t[5000010];int n,m,a[N],x,y,h[N],num,b[N],root[N],ans,w;int d[N],f[N],sz[N],hs[N],top[N],wz[N],rl[N],size,len;inline char nc(void){ static char ch[100010],*p1=ch,*p2=ch; return p1==p2&&(p2=(p1=ch)+fread(ch,1,100010,stdin),p1==p2)?EOF:*p1++;}inline void read(int &a){ static char c=nc();int f=1; for (;!isdigit(c);c=nc()) if (c=='-') f=-1; for (a=0;isdigit(c);a=(a<<3)+(a<<1)+c-'0',c=nc()); return (void)(a*=f);}inline void add(int x,int y){ s[++num]=(side){y,h[x]},h[x]=num; s[++num]=(side){x,h[y]},h[y]=num;}inline void so1(int x,int fa){ d[x]=d[f[x]=fa]+1,sz[x]=1; for (int i=h[x]; i; i=s[i].nt) if (s[i].to!=fa){ so1(s[i].to,x),sz[x]+=sz[s[i].to]; if (sz[s[i].to]>sz[hs[x]]) hs[x]=s[i].to; } return;}inline void so2(int x,int fa){ top[x]=fa,wz[x]=++size,rl[size]=x; if (hs[x]) so2(hs[x],fa); for (int i=h[x]; i; i=s[i].nt) if (s[i].to!=f[x]&&s[i].to!=hs[x]) so2(s[i].to,s[i].to); return;}inline int lca(int x,int y){ for (int fx=top[x],fy=top[y]; fx!=fy; x=f[fx],fx=top[x]) if (d[fx]<d[fy]) swap(fx,fy),swap(x,y); return d[x]<d[y]?x:y;}#define mid (l+r>>1)inline void updata(int pre,int &k,int l,int r,int w){ t[k=++size]=t[pre],++t[k].sum; if (l==r) return; if (mid>=w) updata(t[pre].lt,t[k].lt,l,mid,w); else updata(t[pre].rt,t[k].rt,mid+1,r,w); return;}inline int query(int x,int y,int w){ int d1=x,d2=y,d3=lca(x,y),d4=f[d3],l=1,r=len,ret=0; d1=root[wz[d1]],d2=root[wz[d2]],d3=root[wz[d3]],d4=root[wz[d4]]; while (l<r){ int tmp=t[t[d1].lt].sum+t[t[d2].lt].sum-t[t[d3].lt].sum-t[t[d4].lt].sum; if (tmp>=w) r=mid,d1=t[d1].lt,d2=t[d2].lt,d3=t[d3].lt,d4=t[d4].lt; else l=mid+1,d1=t[d1].rt,d2=t[d2].rt,d3=t[d3].rt,d4=t[d4].rt,w-=tmp; } return b[l];}int main(void){ read(n),read(m); for (int i=1; i<=n; ++i) read(a[i]),b[i]=a[i]; for (int i=1; i<n; ++i) read(x),read(y),add(x,y); so1(1,0),so2(1,1),sort(b+1,b+1+n),len=unique(b+1,b+1+n)-b-1,size=0; for (int i=1; i<=n; ++i) updata(root[wz[f[rl[i]]]],root[i],1,len,lower_bound(b+1,b+1+len,a[rl[i]])-b); while (m--){ read(x),read(y),read(w),x^=ans,printf("%d",ans=query(x,y,w)); if (m) puts(""); } return 0;}
阅读全文
1 0
- 2588: Spoj 10628. Count on a tree 主席树+LCA
- Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)
- [BZOJ]2588: Spoj 10628. Count on a tree 主席树+LCA
- 【BZOJ】2588 Spoj 10628. Count on a tree LCA+主席树
- BZOJ 2588: Spoj 10628. Count on a tree|主席树
- [主席树] BZOJ 2588 Spoj 10628. Count on a tree
- BZOJ 2588 Spoj 10628. Count on a tree 主席树
- 【bzoj2588】Spoj 10628. Count on a tree LCA+主席树
- 【BZOJ2588】Spoj 10628. Count on a tree【主席树】【LCA】
- BZOJ 2588 Count on a tree 主席树+倍增LCA
- BZOJ-2588-Count-on-a-tree-SPOJ10628-LCA+主席树
- BZOJ 2588 Count On a Tree 【LCA】【主席树】
- BZOJ 2588/SPOJ Count on a Tree LCA+线段树
- spoj 10628 Count on a tree (主席树+lca)
- SPOJ 10628 Count on a tree (lca+主席树)
- spoj Count on a tree【主席树+在线LCA】
- 【SPOJ-COT】Count on a tree【主席树】【LCA】
- SPOJ Count on a tree 在线LCA + 主席树
- 图片变形
- leetcode 593. Valid Square 正方形的判定
- L0范式,L1范式,L2范式的简单理解
- 25 个最基本的 JavaScript 面试问题及答案
- git 提示:error: unable to rewind rpc post data
- 【BZOJ】2588 Spoj 10628. Count on a tree LCA+主席树
- 关于neutron.service启动不成功
- bzoj 3246: [Ioi2013]Dreaming
- Memcache,Redis,MongoDB(数据缓存系统)方案对比与分析
- 外贸 服装内衣 接单15天成交50万 分享 (普宁电商外贸)
- 关于支付宝服务商模式的刷卡支付没有分润的问题
- 数学算法中的各种距离
- ubuntu再次体验之【美化】--修改主题、字体、字体大小
- Lintcode179 Update Bits solution 题解