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 大的是哪个点. 题解:学会了平 ...
随机推荐
- 【USACO】wormholes 【暴力】
题意:给出2K个平面上的点,给它们一一配对,问有多少种配对方法使得存在从某个点一直向右走会陷在循环里(K<=6) 思路:由于k很小,配对方法的话暴力枚举,然后判环,判环时需要注意的是一条直线上的 ...
- hdu 1043 A*
http://www.cnblogs.com/183zyz/archive/2011/08/12/2135827.html #include<stdio.h> #define N 3630 ...
- iOS时钟,秒针扫秒样式
昨天做一个时钟小demo,发现了一些问题. 描述能力有限,我封装好了一个时钟框架,朋友们可以参考 https://github.com/qianlishun/ClockView 点击这里可以 ...
- 2016 年末 QBXT 入学测试
P4744 A’s problem(a) 时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试题,每三天结算一次成绩.参与享优惠 描述 这是一道有背 ...
- BZOJ3408: [Usaco2009 Oct]Heat Wave 热浪
最短路模板.选迪杰. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<alg ...
- Linux内核设计与实现——读书笔记2:进程管理
1.进程: (1)处于执行期的程序,但不止是代码,还包括各种程序运行时所需的资源,实际上进程是正在执行的 程序的实时结果. (2)程序的本身并不是进程,进程是处于执行期的程序及其相关资源的总称. (3 ...
- PAT (Advanced Level) 1036. Boys vs Girls (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- curl post请求方式
curl -l -H "application/x-www-form-urlencoded; charset=UTF-8" -X POST -d "query=SELEC ...
- eclipse设置全局编码为UTF-8的方法
1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右侧Text file encoding ...
- OSChinaclient源代码学习(3)--轮询机制的实现
主要以OSChina Androidclient源代码中Notice的轮询机制进行解读. 一.基础知识 一般IM(即使通讯)的实现有两种方式:推送和轮询,推送就是server主动向client发送消息 ...