[HDU5818]Joint Stacks

题意:维护两个栈,支持按插入时间合并

本来是左偏树的题,现在用treap水一下

把加入时间作为节点编号就可以很方便地实现了

#include<stdio.h>
#include<stdlib.h>
int l[100010],r[100010],fix[100010];
unsigned v[100010];
struct pair{
	int l,r;
	pair(int a=0,int b=0){l=a;r=b;}
};
pair split(int x,int d){
	if(x==0)return pair();
	pair s;
	if(d<x){
		s=split(l[x],d);
		l[x]=s.r;
		s.r=x;
	}else{
		s=split(r[x],d);
		r[x]=s.l;
		s.l=x;
	}
	return s;
}
void swap(int&a,int&b){a^=b^=a^=b;}
int merge(int x,int y){
	if(x==0)return y;
	if(y==0)return x;
	if(fix[x]>fix[y])swap(x,y);
	pair s=split(y,x);
	l[x]=merge(l[x],s.l);
	r[x]=merge(r[x],s.r);
	return x;
}
int main(){
	srand(19260817);
	int cas,m,x,tot,rt[2];
	pair p;
	char s[10],st[4];
	scanf("%d",&m);
	cas=0;
	while(m){
		cas++;
		printf("Case #%d:\n",cas);
		tot=rt[0]=rt[1]=0;
		while(m--){
			scanf("%s",s);
			if(s[1]==‘u‘){
				tot++;
				l[tot]=r[tot]=0;
				fix[tot]=rand();
				scanf("%s%u",st,v+tot);
				rt[st[0]-‘A‘]=merge(rt[st[0]-‘A‘],tot);
			}
			if(s[1]==‘o‘){
				scanf("%s",st);
				for(x=rt[st[0]-‘A‘];r[x];x=r[x]);
				p=split(rt[st[0]-‘A‘],x-1);
				rt[st[0]-‘A‘]=p.l;
				printf("%u\n",v[p.r]);
			}
			if(s[1]==‘e‘){
				scanf("%s",st);
				rt[st[0]-‘A‘]=merge(rt[0],rt[1]);
				scanf("%s",st);
				rt[st[0]-‘A‘]=0;
			}
		}
		scanf("%d",&m);
	}
}

原文地址:https://www.cnblogs.com/jefflyy/p/8295991.html

时间: 2024-10-12 14:03:36

[HDU5818]Joint Stacks的相关文章

多校7 HDU5818 Joint Stacks

1 多校7 HDU5818 Joint Stacks 2 题意:n次操作.模拟栈的操作,合并的以后,每个栈里的元素以入栈顺序排列 3 思路:开三个栈,并且用到了merge函数 4 O(n)的复杂度 5 6 #include <bits/stdc++.h> 7 using namespace std; 8 #define LL long long 9 const int inf = 0x3f3f3f3f; 10 const int MOD =998244353; 11 const int N =

hdu-5818 Joint Stacks(模拟)

题目链接: Joint Stacks Time Limit: 8000/4000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the

HDU 5818 Joint Stacks(联合栈)

HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description 题目描述 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of

HDU 5818:Joint Stacks(stack + deque)

http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is

暑假练习赛 004 E Joint Stacks

Joint StacksCrawling in process... Crawling failed Time Limit:4000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 5818 uDebug Description Input Output Sample Input Sample Output Hint Description A stack is a d

2016暑假多校联合---Joint Stacks (STL)

HDU  5818 Problem Description A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is the first one that will be removed. In another wor

HDU 5818 Joint Stacks

搞了第三个栈来表示合并之后的.偷懒写了一个优先队列. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #inc

hdu 5818 (优先队列) Joint Stacks

题目:这里 题意: 两个类似于栈的列表,栈a和栈b,n个操作,push a x表示把数x放进a栈的栈底,pop b 表示将栈b的栈顶元素取出输出,并释放这个栈顶元素,merge a b表示把后面的那个 栈里的元素全部压进前面的那个栈里面,并且压入后前面的栈的所有元素按其进栈顺序排列然后后面的栈变为了空. push和pop操作都还好,就是优先队列或者栈的操作,主要是merge操作,如果啊按照题目来模拟每次将后面的栈元素取出放入前面的栈,可能会超时,而超时的主要因素是每次都将 元素多的栈压入了元素少

HDU 5818 Joint Stacks(左偏树)

题目链接:点击打开链接 思路: 该题的关键是怎么把两个栈合并, 我们可以使用一种叫左偏树的数据结构, 满足堆的性质和集合的性质,支持在O(logn)的复杂度下进行删除堆顶元素, 插入一个元素,合并两个堆. 细节参见代码: #include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; const int maxn = 152400; P v[maxn]; int tot, u, a, b, l[maxn