题目链接

\(Description\)

有n个座山,其高度为hi。有m条带权双向边连接某些山。多次询问,每次询问从v出发 只经过边权<=x的边 所能到达的山中,第K高的是多少。

\(Solution\)

x的限制将图分成了若干连通块,如果x单调递增的话,只需要合并连通块就可以了。

离线,并查集维护所属连通块,线段树合并状态、查询K大。(强制在线的话可以主席树?BZOJ3551 不想写了还是写了,还好)

强制在线的Kruskal+主席树做法:BZOJ3551

//39360kb	5248ms
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 250000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
const int N=1e5+5,M=5e5+5; int n,m,Q,cnt,ref[N],h[N],Ans[M],fa[N],root[N];
char IN[MAXIN],*SS=IN,*TT=IN;
inline int read();
struct Edge{
int fr,to,val;
inline void Read(){
fr=read(), to=read(), val=read();
}
bool operator <(const Edge &a)const{
return val<a.val;
}
}e[M];
struct QUERY{
int fr,x,k,id;
inline void Read(int i){
fr=read(), x=read(), k=read(), id=i;
}
bool operator <(const QUERY &a)const{
return x<a.x;
}
}q[M];
struct Segment_Tree
{
#define S N*18
#define lson son[x][0]
#define rson son[x][1]
int tot,sz[S],son[S][2]; void Insert(int &x,int l,int r,int p)
{
sz[x=++tot]=1;
if(l==r) return;
int m=l+r>>1;
if(p<=m) Insert(lson,l,m,p);
else Insert(rson,m+1,r,p);
}
int Merge(int x,int y)
{
if(!x||!y) return x^y;
lson=Merge(lson,son[y][0]), rson=Merge(rson,son[y][1]);
sz[x]+=sz[y]; /*sz[y]=0;*/ return x;
}
int Query(int x,int l,int r,int k)
{
if(l==r) return l;
int ls=sz[lson];
if(k<=ls) return Query(lson,l,l+r>>1,k);
return Query(rson,(l+r>>1)+1,r,k-ls);
}
}T; inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=='-'&&(f=-1),c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
}
int Getfa(int x){
return x==fa[x]?x:fa[x]=Getfa(fa[x]);
}
inline int Find(int x)
{
int l=1,r=cnt,mid;
while(l<r)
if(ref[mid=l+r>>1]<x) l=mid+1;
else r=mid;
return l;
}
void Merge(int x,int y)
{
int r1=Getfa(x), r2=Getfa(y);
if(r1==r2) return;
fa[r2]=r1, root[r1]=T.Merge(root[r1],root[r2]);
}
int Query(int x,int k)
{
int rt=Getfa(x);
return T.sz[root[rt]]<k?-1:ref[T.Query(root[rt],1,cnt,T.sz[root[rt]]-k+1)];
} int main()
{
n=read(), m=read(), Q=read();
for(int i=1; i<=n; ++i) ref[i]=h[i]=read(), fa[i]=i;
std::sort(ref+1,ref+1+n), cnt=1;
for(int i=2; i<=n; ++i) if(ref[i]!=ref[i-1]) ref[++cnt]=ref[i];
for(int i=1; i<=n; ++i) h[i]=Find(h[i]), T.Insert(root[i],1,cnt,h[i]); for(int i=1; i<=m; ++i) e[i].Read();
std::sort(e+1,e+1+m);
for(int i=1; i<=Q; ++i) q[i].Read(i);
std::sort(q+1,q+1+Q); for(int i=1,now=1; i<=Q; ++i)
{
while(now<=m && e[now].val<=q[i].x) Merge(e[now].fr,e[now].to), ++now;
Ans[q[i].id]=Query(q[i].fr,q[i].k);
}
for(int i=1; i<=Q; ++i) printf("%d\n",Ans[i]); return 0;
}

2019.11.15:

//39404kb	5876ms 跑的又比以前慢自闭了
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
//#define MAXIN 300000
//#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=1e5+5,M=5e5+5; int ref[N],h[N],Ans[M],root[N],fa[N];
//char IN[MAXIN],*SS=IN,*TT=IN;
struct Edge
{
int u,v,w;
bool operator <(const Edge &x)const
{
return w<x.w;
}
}e[M];
struct Queries
{
int s,v,k,id;
bool operator <(const Queries &x)const
{
return v<x.v;
}
}q[M];
struct Segment_Tree
{
#define S N*18
#define ls son[x][0]
#define rs son[x][1]
#define lson ls,l,l+r>>1
#define rson rs,(l+r>>1)+1,r
int tot,son[S][2],sz[S];
#undef S void Insert(int &x,int l,int r,int p)
{
sz[x=++tot]=1;
if(l<r) p<=(l+r>>1) ? Insert(lson,p) : Insert(rson,p);
}
int Merge(int x,int y)
{
if(!x||!y) return x|y;
ls=Merge(ls,son[y][0]), rs=Merge(rs,son[y][1]);
return sz[x]+=sz[y],x;
}
int Query(int x,int l,int r,int k)
{
if(l==r) return l;
int lsz=sz[ls];
return lsz>=k?Query(lson,k):Query(rson,k-lsz);
}
}T; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-48,c=gc());
return now;
}
int Getfa(int x)
{
return x==fa[x]?x:fa[x]=Getfa(fa[x]);
}
inline int Find(int x,int r)
{
int l=1,mid;
while(l<r)
if(ref[mid=l+r>>1]<x) l=mid+1;
else r=mid;
return l;
}
inline void Merge(int x,int y)
{
int r1=Getfa(x),r2=Getfa(y);
if(r1!=r2) fa[r2]=r1, root[r1]=T.Merge(root[r1],root[r2]);
}
inline int Query(int n,int x,int k)
{
int rt=root[Getfa(x)];
return T.sz[rt]<k?-1:ref[T.Query(rt,1,n,T.sz[rt]-k+1)];
} int main()
{
const int n=read(),m=read(),Q=read();
for(int i=1; i<=n; ++i) ref[i]=h[i]=read();
std::sort(ref+1,ref+1+n); int cnt=1;
for(int i=2; i<=n; ++i) if(ref[i]!=ref[i-1]) ref[++cnt]=ref[i];
for(int i=1; i<=n; ++i) h[i]=Find(h[i],cnt);
for(int i=1; i<=n; ++i) T.Insert(root[i],1,cnt,h[i]); for(int i=1; i<=m; ++i) e[i]=(Edge){read(),read(),read()};
for(int i=1; i<=Q; ++i) q[i]=(Queries){read(),read(),read(),i};
std::sort(e+1,e+1+m), std::sort(q+1,q+1+Q); for(int i=1; i<=n; ++i) fa[i]=i;
e[m+1].w=2e9;
for(int i=1,now=1; i<=Q; ++i)
{
while(e[now].w<=q[i].v) Merge(e[now].u,e[now].v), ++now;
Ans[q[i].id]=Query(cnt,q[i].s,q[i].k);
}
for(int i=1; i<=Q; printf("%d\n",Ans[i++])); return 0;
}

BZOJ.3545.[ONTAK2010]Peaks(线段树合并)的更多相关文章

  1. 【bzoj3545】[ONTAK2010]Peaks 线段树合并

    [bzoj3545][ONTAK2010]Peaks 2014年8月26日3,1512 Description 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路 ...

  2. BZOJ 3545: [ONTAK2010]Peaks( BST + 启发式合并 + 并查集 )

    这道题很好想, 离线, 按询问的x排序从小到大, 然后用并查集维护连通性, 用平衡树维护连通块的山的权值, 合并就用启发式合并.时间复杂度的话, 排序是O(mlogm + qlogq), 启发式合并是 ...

  3. BZOJ 3545: [ONTAK2010]Peaks [Splay启发式合并]

    3545: [ONTAK2010]Peaks 题意:带权图,多组询问与一个点通过边权\(\le x\)的边连通的点中点权k大值 又读错题了,输出点一直WA,问的是点权啊 本题加强版强制在线了,那这道题 ...

  4. Peaks 线段树合并

    Peaks 线段树合并 \(n\)个带权值\(h_i\)山峰,有\(m\)条山峰间双向道路,\(q\)组询问,问从\(v_i\)开始只经过\(h_i\le x\)的路径所能到达的山峰中第\(k\)高的 ...

  5. ●BZOJ 3545 [ONTAK2010]Peaks(离线)

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3545 http://www.lydsy.com/JudgeOnline/problem.ph ...

  6. bzoj 4631: 踩气球 线段树合并

    4631: 踩气球 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 265  Solved: 136[Submit][Status][Discuss] ...

  7. BZOJ:5457: 城市(线段树合并)(尚待优化)

    5457: 城市 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 18  Solved: 12[Submit][Status][Discuss] Des ...

  8. bzoj3545 Peaks 线段树合并

    离线乱搞... 也就是一个线段树合并没什么 #include<algorithm> #include<iostream> #include<cstring> #in ...

  9. bzoj 3545: [ONTAK2010]Peaks

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1124  Solved: 304[Submit][Status][Discuss] Descripti ...

随机推荐

  1. 使用python中的matplotlib 画图,show后关闭窗口,继续运行命令

    使用python中的matplotlib 画图,show后关闭窗口,继续运行命令 在用python中的matplotlib 画图时,show()函数总是要放在最后,且它阻止命令继续往下运行,直到1.0 ...

  2. 嵌入式系统C编程之错误处理

    前言 本文主要总结嵌入式系统C语言编程中,主要的错误处理方式.文中涉及的代码运行环境如下: 一  错误概念 1.1 错误分类 从严重性而言,程序错误可分为致命性和非致命性两类.对于致命性错误,无法执行 ...

  3. lxde 的安装和卸载以及注意事项,lubuntu

    安装: $ sudo apt install lxde $ sudo apt install lxde-common 安装完毕后,可能没法关机及logout,可以使用如下安装: $ sudo apt ...

  4. 在Mac上安装GTK(go语言GUI)

    1.在终端输入:xcode-select --install 安装command line工具, 如果安装了Xcode, 就直接跳过该步骤 2. 在终端输入:ruby -e "$(curl ...

  5. Unicode范围预览

    链接: https://www.zhangxinxu.com/study/201611/show-character-by-charcode.php?range=4E00-9FBB 备注: Unico ...

  6. android 获取手机GSM/CDMA信号信息,并获得基站信息

    本文转自:http://software.intel.com/zh-cn/blogs/2011/12/16/android-gsmcdma/ 在Android中我们常用的轻松获取WIFI信号列表,那如 ...

  7. jquery之data()

    $("#test").data("myProp","123") alert($("#test").data(" ...

  8. python魔法函数(二)之__getitem__、__len__、__iter__

    魔法函数会增强python类的类型,独立存在 __getitem class Company: def __init__(self, employees): self.employees = empl ...

  9. 【ES】学习5-全文搜索

    全文搜索两个最重要的方面是:相关性, 分析. 一旦谈论相关性或分析这两个方面的问题时,我们所处的语境是关于查询的而不是过滤. match:单个词查询 GET /my_index/my_type/_se ...

  10. poj2299树状数组入门,求逆序对

    今天入门了树状数组 习题链接 https://blog.csdn.net/liuqiyao_01/article/details/26963913 离散化数据:用一个数组来记录每个值在数列中的排名,不 ...