首页 > 代码库 > splay启发式合并
splay启发式合并
3545: [ONTAK2010]Peaks
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 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][2]; inline void writeln(int k) { int num=0; if (k<0) putchar(45),k=-k; while (k) st[++num]=k%10,k/=10; if (num!=0)while (num) putchar(st[num--]+48);else putchar(48); putchar(‘\n‘); } inline int read() { int x=0,f=1; char c=getchar(); while(c<‘0‘||c>‘9‘) {if(c==‘-‘) f=-1; c=getchar(); } while(c>=‘0‘&&c<=‘9‘) {x*=10; x+=c-‘0‘; 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][0]]+size[child[x][1]]+1; } inline void zig(int x) { int y=fa[x]; fa[x]=fa[y]; child[fa[x]][child[fa[x]][1]==y]=x; child[y][0]=child[x][1]; fa[child[x][1]]=y; fa[y]=x; child[x][1]=y; update(y); update(x); } inline void zag(int x) { int y=fa[x]; fa[x]=fa[y]; child[fa[x]][child[fa[x]][1]==y]=x; child[y][1]=child[x][0]; fa[child[x][0]]=y; fa[y]=x; child[x][0]=y; update(y); update(x); } inline void splay(int x) { while(fa[x]) { int y=fa[x],z=fa[y]; if(!z) { child[y][0]==x?zig(x):zag(x); break; } child[y][0]==x?zig(x):zag(x); child[z][0]==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][1]; else y=child[y][0]; } 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=1,r=0; q[++r]=x; while(l<=r) { int u=q[l++]; if(child[u][0]) q[++r]=child[u][0]; if(child[u][1]) q[++r]=child[u][1]; } q[0]=y; for(int i=1;i<=r;++i) { child[q[i]][0]=child[q[i]][1]=fa[q[i]]=0; update(q[i]); } for(int i=1;i<=r;++i) insert(q[i],q[i-1]); } inline int findkth(int x,int k) { if(size[child[x][1]]==k-1) return x; if(size[child[x][1]]+1>k) return findkth(child[x][1],k); else return findkth(child[x][0],k-size[child[x][1]]-1); } 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]=-1; 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=1;i<=n;++i) { key[i]=read(); father[i]=i; Rank[i]=size[i]=1; } for(int i=1;i<=m;++i) { e[i].u=read(); e[i].v=read(); e[i].w=read(); } for(int i=1;i<=Q;++i) { x[i].v=read(); x[i].x=read(); x[i].k=read(); x[i].id=i; } sort(e+1,e+m+1,cp); sort(x+1,x+Q+1,cp1); int pos=0; for(int i=1;i<=Q;++i) { while(e[pos+1].w<=x[i].x&&pos+1<=m) { ++pos; link(e[pos].u,e[pos].v); } query(x[i].v,x[i].k,x[i].id); } for(int i=1;i<=Q;++i) writeln(ans[i]); // fclose(stdin); // fclose(stdout); return 0; }
2212: [Poi2011]Tree Rotations
Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 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][2],c[N][2]; ll ans; inline ll min(ll x,ll y) { return x<y?x:y; } inline int read() { int x=0,f=1; char c=getchar(); while(c<‘0‘||c>‘9‘) {if(c==‘-‘) f=-1; c=getchar(); } while(c>=‘0‘&&c<=‘9‘) {x*=10; x+=c-‘0‘; c=getchar(); } return x*f; } inline void update(int x) { size[x]=size[child[x][0]]+size[child[x][1]]+1; } inline void zig(int x) { int y=fa[x]; fa[x]=fa[y]; child[fa[x]][child[fa[x]][1]==y]=x; child[y][0]=child[x][1]; fa[child[x][1]]=y; fa[y]=x; child[x][1]=y; update(y); update(x); } inline void zag(int x) { int y=fa[x]; fa[x]=fa[y]; child[fa[x]][child[fa[x]][1]==y]=x; child[y][1]=child[x][0]; fa[child[x][0]]=y; fa[y]=x; child[x][0]=y; update(y); update(x); } inline void splay(int x) { while(fa[x]) { int y=fa[x],z=fa[y]; if(!z) { child[y][0]==x?zig(x):zag(x); break; } child[y][0]==x?zig(x):zag(x); child[z][0]==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][1]; else y=child[y][0]; } 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 0; if(key[x]<k) return size[child[x][0]]+1+findrank(child[x][1],k); else return findrank(child[x][0],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=1,r=0; q[++r]=x; while(l<=r) { int u=q[l++]; if(child[u][0]) q[++r]=child[u][0]; if(child[u][1]) q[++r]=child[u][1]; } q[0]=y; ll cnt1=0; for(int i=1;i<=r;i++) cnt1+=findrank(y,key[q[i]]); ans+=min(cnt1,(ll)size[x]*size[y]-cnt1); for(int i=1;i<=r;i++) child[q[i]][0]=child[q[i]][1]=fa[q[i]]=0; for(int i=1;i<=r;i++) insert(q[i],q[i-1]); } 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]=1; } else { build(c[u][0],x); build(c[u][1],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[0][0],node[0]); printf("%lld\n",ans); return 0; }
splay启发式合并