2733

思路:

  启发式合并splay(n*log^2n);

来,上代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 100001 int n,q,tot,ch[maxn*][],key[maxn*],w[maxn*],opi[maxn*],m;
int size[maxn*],root[maxn],id[maxn*],f[maxn],cnt,dis[maxn*],lar[maxn]; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} inline int getson(int now)
{
return ch[opi[now]][]==now;
} inline void updata(int now)
{
size[now]=w[now];
if(ch[now][]) size[now]+=size[ch[now][]];
if(ch[now][]) size[now]+=size[ch[now][]];
} inline void rotate(int now)
{
int fa=opi[now],ffa=opi[fa],pos=getson(now);
ch[fa][pos]=ch[now][pos^];
if(ch[fa][pos]) opi[ch[fa][pos]]=fa;
if(ffa) ch[ffa][getson(fa)]=now;
ch[now][pos^]=fa,opi[fa]=now,opi[now]=ffa;
updata(fa),updata(now);
} inline void splay(int now,int to)
{
for(int fa;fa=opi[now];rotate(now))
{
if(opi[fa]) rotate(getson(now)==getson(fa)?fa:now);
}
root[to]=now;
} inline void insert(int x,int y,int to)
{
if(!root[to])
{
root[to]=++tot,key[tot]=x,w[tot]=size[tot]=,id[tot]=y;
return ;
}
int now=root[to],fa=;
while()
{
fa=now;
if(x<key[now]) now=ch[now][];
else now=ch[now][];
if(!now)
{
now=ch[fa][x>key[fa]]=++tot;
key[now]=x,id[now]=y,w[now]=size[now]=,opi[now]=fa;
splay(now,to);break;
}
}
} inline int find(int x)
{
if(x==f[x]) return x;
return f[x]=find(f[x]);
} inline int irank(int k,int to)
{
int now=root[to];
while()
{
int dis=size[ch[now][]];
if(k<=dis) now=ch[now][];
else
{
k-=dis;
if(k<=w[now])
{
splay(now,to);
return id[now];
}
else k-=w[now],now=ch[now][];
}
}
} inline void merge(int noww,int now)
{
if(!now) return ;
if(ch[now][]) merge(noww,ch[now][]);
insert(key[now],id[now],noww);
if(ch[now][]) merge(noww,ch[now][]);
} int main()
{
in(n),in(m);int x,y;char ch[];
for(int i=;i<=n;i++) in(dis[i]),f[i]=i,insert(dis[i],i,i),lar[i]=;
for(;m--;)
{
in(x),in(y);
x=find(x),y=find(y);
if(lar[x]<lar[y]) swap(x,y);lar[x]+=lar[y];
if(x!=y) f[y]=x,merge(x,root[y]);
}
in(q);
for(;q--;)
{
scanf("%s",ch);in(x),in(y);
if(ch[]=='Q')
{
x=find(x);
if(y<=size[root[x]]) printf("%d\n",irank(y,x));
else printf("-1\n");
}
else
{
x=find(x),y=find(y);
if(lar[x]<lar[y]) swap(x,y);lar[x]+=lar[y];
if(x!=y) f[y]=x,merge(x,root[y]);
}
}
return ;
}

AC日记——[HNOI2012]永无乡 bzoj 2733的更多相关文章

  1. 2733: [HNOI2012]永无乡 - BZOJ

    Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...

  2. BZOJ 2733: [HNOI2012]永无乡 启发式合并treap

    2733: [HNOI2012]永无乡 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  3. bzoj 2733: [HNOI2012]永无乡 离线+主席树

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1167  Solved: 607[Submit][Status ...

  4. BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)

    不难...treap + 启发式合并 + 并查集 搞搞就行了 --------------------------------------------------------------------- ...

  5. BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]

    2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...

  6. bzoj 2733: [HNOI2012]永无乡 -- 线段树

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...

  7. Bzoj 2733: [HNOI2012]永无乡 数组Splay+启发式合并

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3955  Solved: 2112[Submit][Statu ...

  8. Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...

  9. 线段树合并+并查集 || BZOJ 2733: [HNOI2012]永无乡 || Luogu P3224 [HNOI2012]永无乡

    题面:P3224 [HNOI2012]永无乡 题解: 随便写写 代码: #include<cstdio> #include<cstring> #include<iostr ...

随机推荐

  1. 内存压缩PK页面交换 解决内存问题谁更在行

    一台服务器能够支持的虚拟机数量通常取决于物理硬件所能够提供的可用计算资源.大多数资源, 比如处理器周期.存储I/O和网络带宽等,都能够相对简单地进行共享.这种做法的原理在于负载并不总是处于忙碌状态,因 ...

  2. python学习笔记十七:base64及md5编码

    一.Python Base64编码 Python中进行Base64编码和解码要用base64模块,代码示例: #-*- coding: utf-8 -*- import base64 str = 'c ...

  3. linux运维笔记

    一.查找大文件 sudo find / -size +100M -exec ls -lh {} \;

  4. c语言字符串内存分配小记

    一.疑问 有这样一道题: #include "stdio.h" int main() { ]; ]; scanf("%s", word1); scanf(&qu ...

  5. Python 列表、元组、字典及集合操作详解

    一.列表 列表是Python中最基本的数据结构,是最常用的Python数据类型,列表的数据项不需要具有相同的类型 列表是一种有序的集合,可以随时添加和删除其中的元素 列表的索引从0开始 1.创建列表 ...

  6. Django学习笔记(二):使用Template让HTML、CSS参与网页建立

    Django学习笔记(二):使用Template让HTML.CSS参与网页建立 通过本文章实现: 了解Django中Template的使用 让HTML.CSS等参与网页建立 利用静态文件应用网页样式 ...

  7. codeblocks17.12 debug 报错:ERROR: You need to specify a debugger program in the debuggers's settings.

    DebugERROR: You need to specify a debugger program in the debuggers's settings.(For MinGW compilers, ...

  8. hnust 分蛋糕

    问题 B: 分蛋糕 时间限制: 1 Sec  内存限制: 128 MB提交: 2430  解决: 966[提交][状态][讨论版] 题目描述 今天是DK生日,由于DK的朋友很多,所以DK在蛋糕店定制了 ...

  9. 【Luogu】P3228数列(数学题)

    题目链接 考虑我们把所有的增加量拿出来做成一个序列b. 那么在所有n中开头中$1~\sum\limits_{i=1}^{k-1}b[i]$是合法的 也就是说我们枚举所有b[i],然后答案就是$n*m^ ...

  10. registry --------->仓库 ----------------->镜像

    registry --------->仓库 ----------------->镜像 本地镜像都保存在宿主机下 : /var/lib/docker/containers 镜像从仓库下载下来 ...