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

10 11 4
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

6
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

3
0
0
3
1
2

Sample Output

1

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启发式合并的更多相关文章

  1. 【BZOJ-2809】dispatching派遣 Splay + 启发式合并

    2809: [Apio2012]dispatching Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2334  Solved: 1192[Submi ...

  2. 【BZOJ-2733】永无乡 Splay+启发式合并

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

  3. BZOJ2733 永无乡【splay启发式合并】

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

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

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

  5. bzoj2733: [HNOI2012]永无乡(splay+启发式合并/线段树合并)

    这题之前写过线段树合并,今天复习Splay的时候想起这题,打算写一次Splay+启发式合并. 好爽!!! 写了长长的代码(其实也不长),只凭着下午的一点记忆(没背板子...),调了好久好久,过了样例, ...

  6. 【BZOJ2733】永无乡[HNOI2012](splay启发式合并or线段树合并)

    题目大意:给你一些点,修改是在在两个点之间连一条无向边,查询时求某个点能走到的点中重要度第k大的点.题目中给定的是每个节点的排名,所以实际上是求第k小:题目求的是编号,不是重要度的排名.我一开始差点被 ...

  7. 算法复习——splay+启发式合并(bzoj2733-永无乡)

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

  8. 【BZOJ 2733】【HNOI 2012】永无乡 Splay启发式合并

    启发式合并而已啦,, 调试时发现的错误点:insert后没有splay,把要拆开的树的点插入另一个树时没有把ch[2]和fa设为null,找第k大时没有先减k,,, 都是常犯的错误,比赛时再这么粗心就 ...

  9. 【BZOJ2809】【splay启发式合并】dispatching

    Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级. ...

  10. 【洛谷P3224】永无乡 并查集+Splay启发式合并

    题目大意:给定 N 个点的图,点有点权,初始有一些无向边,现在有 Q 个询问,每个询问支持动态增加一条无向边连接两个不连通的点和查询第 X 个点所在的联通块中权值第 K 大的是哪个点. 题解:学会了平 ...

随机推荐

  1. POJ 1080 Human Gene Functions 【dp】

    题目大意:每次给出两个碱基序列(包含ATGC的两个字符串),其中每一个碱基与另一串中碱基如果配对或者与空串对应会有一个分数(可能为负),找出一种方式使得两个序列配对的分数最大 思路:字符串动态规划的经 ...

  2. hdu 4801模拟题

    /* 模拟: 注意:实质上一次魔方的一半要变化 用c++超内存 用g++过了 */ #include<stdio.h> #include<string.h> #include& ...

  3. hdu3709 Balanced Number 树形dp

    A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. ...

  4. Codeforces936D. World of Tank

    $n \leq 1e9$,$n*2$的网格里有$m_1+m_2 \leq 1e6$个障碍物,现有一坦克从$(0,1)$出发要到$(n+1,1/2)$,他每秒可以换行(纵坐标1变2或2变1)也可以发炮弹 ...

  5. UVAlive 3026 KMP 最小循环节

    KMP算法: 一:next数组:next[i]就是前面长度为i的字符串前缀和后缀相等的最大长度,也即索引为i的字符失配时的前缀函数. 二:KMP模板 /* pku3461(Oulipo), hdu17 ...

  6. P1918 保龄球 洛谷

    https://www.luogu.org/problem/show?pid=1918 题目描述 DL 算缘分算得很烦闷,所以常常到体育馆去打保龄球解闷.因为他保龄球已经打了几十年了,所以技术上不成问 ...

  7. uva 10559

    记忆话搜索 DP 看了网上题解  状态方程真是巧妙 orz #include <cstdio> #include <cstdlib> #include <cmath> ...

  8. 标准格式包含: 私有属性 无参构造 有参构造 setter 和getter 需求中的方法 需求一: 员工类Employee 属性:姓名name,工号id,工资salary 行为:显示所有成员信息的方法show() 需求二: 动物类Animal 属性:姓名name,年龄age 行为:吃饭

      // 员工类 public class Employee { private String name; private int id; private double salary; public ...

  9. 转: 使用valgrind检查内存问题

    作者:gfree.wind@gmail.com 博客:blog.focus-linux.net   linuxfocus.blog.chinaunix.net    本文的copyleft归gfree ...

  10. eclipse菜单字体乱码的解决

    方法一: 这个跟活动控制台代码页有关. 如果要更改为 UTF-8,则需要运行 chcp 命令: chcp 65001 有时新安装的系统可能在运行一些中文软件时显示错乱,可通过控制面板修改系统区域来管理 ...