splay启发式合并
3545: [ONTAK2010]Peaks
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1889 Solved: 501
[Submit][Status][Discuss]
Description
在Bytemountains有N座山峰,每座山峰有他的高度h_i。有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询问从点v开始只经过困难值小于等于x的路径所能到达的山峰中第k高的山峰,如果无解输出-1。
Input
第一行三个数N,M,Q。
第二行N个数,第i个数为h_i
接下来M行,每行3个数a b c,表示从a到b有一条困难值为c的双向路径。
接下来Q行,每行三个数v x k,表示一组询问。
Output
对于每组询问,输出一个整数表示答案。
Sample Input
1 2 3 4 5 6 7 8 9 10
1 4 4
2 5 3
9 8 2
7 8 10
7 1 4
6 7 1
6 4 8
2 1 5
10 8 10
3 4 7
3 4 6
1 5 2
1 5 6
1 5 8
8 9 2
Sample Output
1
-1
8
HINT
【数据范围】
N<=10^5, M,Q<=5*10^5,h_i,c,x<=10^9。
Source
莫名其妙的tle了。昨天查了一下午,和上一题的模板一模一样,就是查不出来。最后发现似乎放入队列时点的儿子和父亲要清空。。。
为什么网上的题解没加都过了。。。我不知道。。。
还是tle了。。。是不是得学treap了
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1500010
struct edge
{
int u,v,w;
}e[N];
struct data
{
int v,x,k,id;
}x[N];
int n,m,Q;
int size[N],fa[N],key[N],father[N],Rank[N],q[N],ans[N],st[N];
int child[N][];
inline void writeln(int k)
{
int num=;
if (k<) putchar(),k=-k;
while (k) st[++num]=k%,k/=;
if (num!=)while (num) putchar(st[num--]+);else putchar();
putchar('\n');
}
inline int read()
{
int x=,f=; char c=getchar();
while(c<''||c>'') {if(c=='-') f=-; c=getchar(); }
while(c>=''&&c<='') {x*=; x+=c-''; c=getchar(); }
return x*f;
}
inline bool cp(edge x,edge y)
{
return x.w<y.w;
}
inline bool cp1(data x,data y)
{
return x.x<y.x;
}
inline void update(int x)
{
size[x]=size[child[x][]]+size[child[x][]]+;
}
inline void zig(int x)
{
int y=fa[x];
fa[x]=fa[y]; child[fa[x]][child[fa[x]][]==y]=x;
child[y][]=child[x][]; fa[child[x][]]=y;
fa[y]=x; child[x][]=y;
update(y); update(x);
}
inline void zag(int x)
{
int y=fa[x];
fa[x]=fa[y]; child[fa[x]][child[fa[x]][]==y]=x;
child[y][]=child[x][]; fa[child[x][]]=y;
fa[y]=x; child[x][]=y;
update(y); update(x);
}
inline void splay(int x)
{
while(fa[x])
{
int y=fa[x],z=fa[y];
if(!z)
{
child[y][]==x?zig(x):zag(x); break;
}
child[y][]==x?zig(x):zag(x);
child[z][]==x?zig(x):zag(x);
}
}
inline void insert(int x,int root)
{
int y=root,last;
while(y)
{
last=y;
if(key[x]>key[y]) y=child[y][];
else y=child[y][];
}
fa[x]=last; child[last][(key[x]>key[last])]=x;
update(x); update(last);
splay(x);
}
inline void merge(int x,int y)
{
splay(x); splay(y);
if(size[x]>size[y]) swap(x,y);
int l=,r=;
q[++r]=x;
while(l<=r)
{
int u=q[l++];
if(child[u][]) q[++r]=child[u][];
if(child[u][]) q[++r]=child[u][];
}
q[]=y;
for(int i=;i<=r;++i)
{
child[q[i]][]=child[q[i]][]=fa[q[i]]=;
update(q[i]);
}
for(int i=;i<=r;++i) insert(q[i],q[i-]);
}
inline int findkth(int x,int k)
{
if(size[child[x][]]==k-) return x;
if(size[child[x][]]+>k) return findkth(child[x][],k);
else return findkth(child[x][],k-size[child[x][]]-);
}
inline int find(int x)
{
return father[x]==x?father[x]:find(father[x]);
}
inline void connect(int x,int y)
{
int a=find(x),b=find(y);
if(a==b) return;
if(Rank[a]>=Rank[b])
{
Rank[a]+=Rank[b];
father[b]=a;
}
else
{
Rank[b]+=Rank[a];
father[a]=b;
}
}
inline void link(int x,int y)
{
if(find(x)==find(y)) return;
merge(x,y);
connect(x,y);
}
inline void query(int x,int k,int id)
{
splay(x);
if(size[x]<k)
{
ans[id]=-;
return;
}
ans[id]=key[findkth(x,k)];
}
int main()
{
// freopen("szc102.in","r",stdin);
// freopen("output.txt","w",stdout);
n=read(); m=read(); Q=read();
for(int i=;i<=n;++i)
{
key[i]=read();
father[i]=i;
Rank[i]=size[i]=;
}
for(int i=;i<=m;++i)
{
e[i].u=read();
e[i].v=read();
e[i].w=read();
}
for(int i=;i<=Q;++i)
{
x[i].v=read();
x[i].x=read();
x[i].k=read();
x[i].id=i;
}
sort(e+,e+m+,cp);
sort(x+,x+Q+,cp1);
int pos=;
for(int i=;i<=Q;++i)
{
while(e[pos+].w<=x[i].x&&pos+<=m)
{
++pos;
link(e[pos].u,e[pos].v);
}
query(x[i].v,x[i].k,x[i].id);
}
for(int i=;i<=Q;++i) writeln(ans[i]);
// fclose(stdin);
// fclose(stdout);
return ;
}
2212: [Poi2011]Tree Rotations
Time Limit: 20 Sec Memory Limit: 259 MB
Submit: 860 Solved: 335
[Submit][Status][Discuss]
Description
Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some interesting features: The tree consists of straight branches, bifurcations and leaves. The trunk stemming from the ground is also a branch. Each branch ends with either a bifurcation or a leaf on its top end. Exactly two branches fork out from a bifurcation at the end of a branch - the left branch and the right branch. Each leaf of the tree is labelled with an integer from the range . The labels of leaves are unique. With some gardening work, a so called rotation can be performed on any bifurcation, swapping the left and right branches that fork out of it. The corona of the tree is the sequence of integers obtained by reading the leaves' labels from left to right. Byteasar is from the old town of Byteburg and, like all true Byteburgers, praises neatness and order. He wonders how neat can his tree become thanks to appropriate rotations. The neatness of a tree is measured by the number of inversions in its corona, i.e. the number of pairs(I,j), (1< = I < j < = N ) such that(Ai>Aj) in the corona(A1,A2,A3…An).
The original tree (on the left) with corona(3,1,2) has two inversions. A single rotation gives a tree (on the right) with corona(1,3,2), which has only one inversion. Each of these two trees has 5 branches. Write a program that determines the minimum number of inversions in the corona of Byteasar's tree that can be obtained by rotations.
现在有一棵二叉树,所有非叶子节点都有两个孩子。在每个叶子节点上有一个权值(有n个叶子节点,满足这些权值为1..n的一个排列)。可以任意交换每个非叶子节点的左右孩子。
要求进行一系列交换,使得最终所有叶子节点的权值按照遍历序写出来,逆序对个数最少。
Input
In the first line of the standard input there is a single integer (2< = N < = 200000) that denotes the number of leaves in Byteasar's tree. Next, the description of the tree follows. The tree is defined recursively: if there is a leaf labelled with ()(1<=P<=N) at the end of the trunk (i.e., the branch from which the tree stems), then the tree's description consists of a single line containing a single integer , if there is a bifurcation at the end of the trunk, then the tree's description consists of three parts: the first line holds a single number , then the description of the left subtree follows (as if the left branch forking out of the bifurcation was its trunk), and finally the description of the right subtree follows (as if the right branch forking out of the bifurcation was its trunk).
第一行n
下面每行,一个数x
如果x==0,表示这个节点非叶子节点,递归地向下读入其左孩子和右孩子的信息,
如果x!=0,表示这个节点是叶子节点,权值为x
1<=n<=200000
Output
In the first and only line of the standard output a single integer is to be printed: the minimum number of inversions in the corona of the input tree that can be obtained by a sequence of rotations.
一行,最少逆序对个数
Sample Input
0
0
3
1
2
Sample Output
HINT
Source
常数太大了,卡不过去
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define N 2000010
int n,cnt,x,y;
int fa[N],size[N],q[N],key[N],node[N];
int child[N][],c[N][];
ll ans;
inline ll min(ll x,ll y)
{
return x<y?x:y;
}
inline int read()
{
int x=,f=; char c=getchar();
while(c<''||c>'') {if(c=='-') f=-; c=getchar(); }
while(c>=''&&c<='') {x*=; x+=c-''; c=getchar(); }
return x*f;
}
inline void update(int x)
{
size[x]=size[child[x][]]+size[child[x][]]+;
}
inline void zig(int x)
{
int y=fa[x];
fa[x]=fa[y]; child[fa[x]][child[fa[x]][]==y]=x;
child[y][]=child[x][]; fa[child[x][]]=y;
fa[y]=x; child[x][]=y;
update(y); update(x);
}
inline void zag(int x)
{
int y=fa[x];
fa[x]=fa[y]; child[fa[x]][child[fa[x]][]==y]=x;
child[y][]=child[x][]; fa[child[x][]]=y;
fa[y]=x; child[x][]=y;
update(y); update(x);
}
inline void splay(int x)
{
while(fa[x])
{
int y=fa[x],z=fa[y];
if(!z)
{
child[y][]==x?zig(x):zag(x); break;
}
child[y][]==x?zig(x):zag(x);
child[z][]==x?zig(x):zag(x);
}
}
inline void insert(int x,int root)
{
int y=root,last;
while(y)
{
last=y;
if(key[x]>key[y]) y=child[y][];
else y=child[y][];
}
fa[x]=last; child[last][(key[x]>key[last])]=x;
update(x); update(last);
splay(x);
}
inline int findrank(int x,int k)
{
if(!x) return ;
if(key[x]<k) return size[child[x][]]++findrank(child[x][],k);
else return findrank(child[x][],k);
}
inline void merge(int x,int y)
{
if(!x||!y) return;
splay(x); splay(y);
if(size[x]>size[y]) swap(x,y);
int l=,r=;
q[++r]=x;
while(l<=r)
{
int u=q[l++];
if(child[u][]) q[++r]=child[u][];
if(child[u][]) q[++r]=child[u][];
}
q[]=y;
ll cnt1=;
for(int i=;i<=r;i++) cnt1+=findrank(y,key[q[i]]);
ans+=min(cnt1,(ll)size[x]*size[y]-cnt1);
for(int i=;i<=r;i++) child[q[i]][]=child[q[i]][]=fa[q[i]]=;
for(int i=;i<=r;i++) insert(q[i],q[i-]);
}
inline void build(int&u,int&v)
{
int x; x=read(); u=++cnt;
if(x)
{
key[u]=x;
node[u]=u;
v=u;
size[u]=;
}
else
{
build(c[u][],x); build(c[u][],y);
merge(x,y);
v=x;
}
}
int main()
{
// int size = 1024 << 20; // 256MB
// char *p = (char*)malloc(size) + size;
// __asm__("movl %0, %%esp\n" :: "r"(p));
n=read();
build(c[][],node[]);
printf("%lld\n",ans);
return ;
}
splay启发式合并的更多相关文章
- 【BZOJ-2809】dispatching派遣 Splay + 启发式合并
2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2334 Solved: 1192[Submi ...
- 【BZOJ-2733】永无乡 Splay+启发式合并
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2048 Solved: 1078[Submit][Statu ...
- BZOJ2733 永无乡【splay启发式合并】
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]
2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...
- bzoj2733: [HNOI2012]永无乡(splay+启发式合并/线段树合并)
这题之前写过线段树合并,今天复习Splay的时候想起这题,打算写一次Splay+启发式合并. 好爽!!! 写了长长的代码(其实也不长),只凭着下午的一点记忆(没背板子...),调了好久好久,过了样例, ...
- 【BZOJ2733】永无乡[HNOI2012](splay启发式合并or线段树合并)
题目大意:给你一些点,修改是在在两个点之间连一条无向边,查询时求某个点能走到的点中重要度第k大的点.题目中给定的是每个节点的排名,所以实际上是求第k小:题目求的是编号,不是重要度的排名.我一开始差点被 ...
- 算法复习——splay+启发式合并(bzoj2733-永无乡)
题目: Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通 ...
- 【BZOJ 2733】【HNOI 2012】永无乡 Splay启发式合并
启发式合并而已啦,, 调试时发现的错误点:insert后没有splay,把要拆开的树的点插入另一个树时没有把ch[2]和fa设为null,找第k大时没有先减k,,, 都是常犯的错误,比赛时再这么粗心就 ...
- 【BZOJ2809】【splay启发式合并】dispatching
Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级. ...
- 【洛谷P3224】永无乡 并查集+Splay启发式合并
题目大意:给定 N 个点的图,点有点权,初始有一些无向边,现在有 Q 个询问,每个询问支持动态增加一条无向边连接两个不连通的点和查询第 X 个点所在的联通块中权值第 K 大的是哪个点. 题解:学会了平 ...
随机推荐
- idea添加虚拟参数
1. 2.
- MTK GPIO 新增变量配置
主要涉及的文件: 1.需要配置preloader ,lk ,kernel vendor GPIO_YuSu.cmp文件增加IO别名: 2.需要配置preloader ,lk ,kernel vendo ...
- apache + DSO -动态共享对象(DSO)
http://www.jinbuguo.com/apache/menu22/dso.html
- hdu6080(最小环)
题目 http://acm.hdu.edu.cn/showproblem.php?pid=6080 分析 很妙的思路,将里面的点集当作A,将外面的点集当作B 然后O(n^2)枚举两两B点,设一个是u, ...
- MySQL事务及Spring事务管理
事务,是在数据库中用于保证数据正确性的一种机制,涉及到很多概念以及不同的情况,这里做一个总结 相关概念 事务四特性(ACID) 原子性(Atomicity,或称不可分割性):要么全部完成或者全部不完成 ...
- Ubuntu 16.04安装Fiddler抓包工具(基于Mono,且会有BUG)
说明:Fiddler官方提供了Mono版本的,但是只有2014版本的,不是最新的,并且运行期间会有BUG,比如界面错乱卡死等等,但是勉强能代理,抓SSL的包,如果使用了要做好心理准备.将就一下还是可以 ...
- apache下配置认证用户
有时候我们须要给我apacheserver下制定的文件夹加上用户认证,方便一些而用户进行文件的浏览.配置例如以下: 1 设置用户 1 htpasswd -c file_path user_name 回 ...
- 模仿猫眼电影App一个动画效果
看真正的猫眼效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIxMDYyMA==/font/5a6L5L2T/fontsize/400/f ...
- HBuilder开发App教程05-滴石和websql
滴石 介绍 滴石是用HBuilder开发的一款计划类app. 用到HBuilder,mui.nativejs以及h5一些特性. 预期 眼下仅仅开发到todolist级别, 以后计划做成日计划,月计划, ...
- 那条linq语句为啥这么慢
目前所在的项目大量使用了linq,结果有个地方出现了严重的性能问题.一个统计需要3.40秒.头头焦头烂额之际,也让我看看. 我向来喜欢性能调优,自诩编码极为注重性能.曾几何时,也动不动就把性能挂在嘴边 ...