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. Django基本使用

    目录 1 安装 1.1 安装pip 1.2 安装django 2 创建项目 2.1 使用 管理工具 django-admin.py 来创建 PyLearn 项目: 2.2 启动服务 本文章以下所有列子 ...

  2. [转] PHP在不同页面之间传值的三种常见方式

    转自: http://my.oschina.net/jiec/blog/196153 一. POST传值 post传值是用于html的<form>表单跳转的方法,很方便使用.例如: < ...

  3. Timer的schedule和scheduleAtFixedRate方法的区别解析

    在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 (1)sched ...

  4. github+git提交 基础用法

    git版本管理基本用法: 安装就不用说了 随便一搜 安装完 妥妥的.下边说的是在github从新建一个项目开始: 1.首先打开自己的github地址,如下图所示 点加号 选 New repositor ...

  5. 孤荷凌寒自学python第四十七天通用跨数据库同一数据库中复制数据表函数

    孤荷凌寒自学python第四十七天通用跨数据库同一数据库中复制数据表函数 (完整学习过程屏幕记录视频地址在文末) 今天继续建构自感觉用起来顺手些的自定义模块和类的代码. 今天打算完成的是通用的(至少目 ...

  6. LightGBM的并行优化--机器学习-周振洋

    LightGBM的并行优化 上一篇文章介绍了LightGBM算法的特点,总结起来LightGBM采用Histogram算法进行特征选择以及采用Leaf-wise的决策树生长策略,使其在一批以树模型为基 ...

  7. 普通用户操作tomcat项目时报:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this program

    在使用普通用户更新tomcat项目适合出现这个信息,Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At ...

  8. Python中的返回函数与闭包

    返回函数,顾名思义,就是高阶函数可以把函数作为return值返回.与闭包的关系是:闭包需要以返回函数的形式实现. 一. 返回函数 比如我们有一个求和函数: >>> def calc_ ...

  9. 【bzoj2424】[HAOI2010]订货 费用流

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825296.html 题目描述 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di, ...

  10. P1270 “访问”美术馆

    题目描述 经过数月的精心准备,Peer Brelstet,一个出了名的盗画者,准备开始他的下一个行动.艺术馆的结构,每条走廊要么分叉为两条走廊,要么通向一个展览室.Peer知道每个展室里藏画的数量,并 ...