[JZOJ4927]第K大
来源:互联网 发布:员工定位软件 编辑:程序博客网 时间:2024/04/27 13:34
题目大意
给定一棵具有
本题开O2……
题目分析
裸的点分治。
二分答案,转化为判定性问题,然后使用点剖的重心树来判定。
每个重心维护重心树子树中所有点到其距离的排序,以及到其上级重心的距离的排序,二分查找即可。
时间复杂度
代码实现
#include <algorithm>#include <iostream>#include <cstdio>#include <cctype>using namespace std;inline int read(){ int x=0,f=1; char ch=getchar(); while (!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar(); while (isdigit(ch)) x=x*10+ch-'0',ch=getchar(); return x*f;}int buf[30];inline void write(int x){ if (x<0) putchar('-'),x=-x; for (;x;x/=10) buf[++buf[0]]=x%10; if (!buf[0]) buf[++buf[0]]=0; for (;buf[0];putchar('0'+buf[buf[0]--]));}const int N=10050;const int LGN=15;const int E=N<<1;const int EL=N<<1;const int LGEL=15;int deep[N],top[N],fa[N],pos[N],last[N],que[N],size[N],crf[N],st[N],en[N];int tov[E],nxt[E],len[E];int LOG[EL],euler[EL];int data[2][N*LGN];int rmq[EL][LGEL];bool vis[N];int n,tot,head,tail,cnt,el,lgel,sum;void insert(int x,int y,int z){tov[++tot]=y,len[tot]=z,nxt[tot]=last[x],last[x]=tot;}int core(int src){ for (head=0,fa[que[tail=1]=src]=0,top[src]=0;head!=tail;) { int x=que[++head],i=last[x],y; for (size[x]=1;i;i=nxt[i]) if ((y=tov[i])!=fa[x]&&!vis[y]) fa[que[++tail]=y]=x,top[y]=top[x]+len[i]; } for (head=tail;head>1;size[fa[que[head]]]+=size[que[head]],head--); int ret=0,res=n+1; for (head=1;head<=tail;head++) { int x=que[head],i=last[x],y,tmp=0; for (;i;i=nxt[i]) if ((y=tov[i])!=fa[x]&&!vis[y]) tmp=max(tmp,size[y]); tmp=max(tmp,size[src]-size[x]); if (tmp<res) ret=x,res=tmp; } return ret;}int build(int c,int f,int delta){ fa[c=core(c)]=0; for (head=0,deep[que[tail=1]=c]=0;head!=tail;) for (int x=que[++head],i=last[x],y;i;i=nxt[i]) if ((y=tov[i])!=fa[x]&&!vis[y]) deep[que[++tail]=y]=deep[fa[y]=x]+len[i]; st[c]=cnt+1,cnt=en[c]=cnt+tail; for (head=1;head<=tail;head++) data[0][st[c]+head-1]=deep[que[head]],f?data[1][st[c]+head-1]=top[que[head]]+delta:0; sort(data[0]+st[c],data[0]+en[c]+1),f?sort(data[1]+st[c],data[1]+en[c]+1),0:0; vis[c]=1; for (int i=last[c],y;i;i=nxt[i]) if (!vis[y=tov[i]]) crf[build(y,c,len[i])]=c; return c;}void dfs(int x){ rmq[pos[euler[++el]=x]=el][0]=x; for (int i=last[x],y;i;i=nxt[i]) if ((y=tov[i])!=fa[x]) deep[y]=deep[x]+1,top[y]=top[x]+len[i],fa[y]=x,dfs(y),euler[++el]=x,rmq[el][0]=x;}void pre(){ LOG[1]=0; for (int i=2;i<=el;i++) LOG[i]=LOG[i-1]+(1<<LOG[i-1]+1==i); lgel=LOG[el]; for (int j=1;j<=lgel;j++) for (int i=1;i+(1<<j)-1<=el;i++) rmq[i][j]=deep[rmq[i][j-1]]<deep[rmq[i+(1<<j-1)][j-1]]?rmq[i][j-1]:rmq[i+(1<<j-1)][j-1];}int getrmq(int l,int r){ int lgr=LOG[r-l+1]; return deep[rmq[l][lgr]]<deep[rmq[r-(1<<lgr)+1][lgr]]?rmq[l][lgr]:rmq[r-(1<<lgr)+1][lgr];}int lca(int x,int y){ if ((x=pos[x])>(y=pos[y])) swap(x,y); return getrmq(x,y);}int dist(int x,int y){return top[x]+top[y]-(top[lca(x,y)]<<1);}int query(bool flag,int x,int y){ int l=st[x],r=en[x],mid,ret=l-1; while (l<=r) { mid=l+r>>1; if (data[flag][mid]<=y) ret=mid,l=mid+1; else r=mid-1; } return ret=ret-st[x]+1;}int count(int src,int l){ int ret=0; for (int x=src,y=crf[x];x;x=y,y=crf[x]) { ret+=query(0,x,l-dist(src,x)); if (y) ret-=query(1,x,l-dist(src,y)); } return ret;}int kth(int x,int k){ int l=0,r=sum,mid,ret=-1; while (l<=r) { mid=l+r>>1; if (count(x,mid)>=k) ret=mid,r=mid-1; else l=mid+1; } return ret;}int main(){ freopen("treekth.in","r",stdin),freopen("treekth.out","w",stdout); n=read(); for (int i=1,x,y,z;i<n;i++) x=read(),y=read(),z=read(),insert(x,y,z),insert(y,x,z),sum+=z; build(1,0,0),deep[1]=top[1]=fa[1]=0,dfs(1),pre(); for (int i=1;i<=n;i++) write(kth(i,read())),putchar('\n'); fclose(stdin),fclose(stdout); return 0;}
0 0
- [JZOJ4927]第K大
- JZOJ4927. 第K大
- P1788第k大
- 找第k大
- 第K大素数
- 数列第k大
- 区间第K大
- 第K大素数
- 第k大元素
- 寻找第K大
- 第k大元素
- P1788 第k大
- 寻找第K大
- 第K大
- 求第k大
- 第k大元素
- 寻找第K大
- 第K大区间
- replace函数浅析
- COM(4)
- LeetCode_414 Third Maximum Number(easy)
- java核心技术卷一学习 2016.12.1
- TODO:
- [JZOJ4927]第K大
- java中的进制转化
- 欢迎使用CSDN-markdown编辑器
- PAT基础编程 5-14 求整数段和 (15分)
- 什么是协程?
- JavaScript&&jQuery
- Java轻量级框架选择
- 树梅派应用21:Lakka 让树莓派变身全能游戏模拟器
- codeblocks中我认为比较常用的快捷键