bzoj1455
2016-06-29 20:29
253 查看
只能说随机是个好东西比左偏树好写一点
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1000010
using namespace std;
struct Heap{
Heap *son[2];
int val;
Heap(int _):val(_) {
son[0] = son[1] = NULL;
}
Heap() {}
}*heap[MAX],mempool[MAX],*C = mempool + 1;
Heap *Merge(Heap *x,Heap *y)
{
if(x == NULL) return y;
if(y == NULL) return x;
if(x->val > y->val) swap(x,y);
bool k = rand()&1;
x->son[k] = Merge(x->son[k],y);
return x;
}
int points,asks;
int src[MAX];
bool killed[MAX];
char s[10];
int father[MAX];
int Find(int x)
{
if(father[x] == x) return x;
return father[x] = Find(father[x]);
}
int main()
{
srand(19970806);
cin >> points;
for(int i = 1; i <= points; ++i)
father[i] = i;
for(int x,i = 1; i <= points; ++i) {
scanf("%d",&x);
heap[i] = new (C++)Heap(x);
}
cin >> asks;
for(int x,y,i = 1; i <= asks; ++i) {
scanf("%s",s);
if(s[0] == 'M') {
scanf("%d%d",&x,&y);
if(killed[x] || killed[y]) continue;
int fx = Find(x),fy = Find(y);
if(fx == fy) continue;
father[fy] = fx;
heap[fx] = Merge(heap[fx],heap[fy]);
}
else {
scanf("%d",&x);
if(killed[x]) {
puts("0");
continue;
}
int fx = Find(x);
printf("%d\n",heap[fx]->val);
killed[heap[fx] - mempool] = true;
heap[fx] = Merge(heap[fx]->son[0],heap[fx]->son[1]);
}
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1000010
using namespace std;
struct Heap{
Heap *son[2];
int val;
Heap(int _):val(_) {
son[0] = son[1] = NULL;
}
Heap() {}
}*heap[MAX],mempool[MAX],*C = mempool + 1;
Heap *Merge(Heap *x,Heap *y)
{
if(x == NULL) return y;
if(y == NULL) return x;
if(x->val > y->val) swap(x,y);
bool k = rand()&1;
x->son[k] = Merge(x->son[k],y);
return x;
}
int points,asks;
int src[MAX];
bool killed[MAX];
char s[10];
int father[MAX];
int Find(int x)
{
if(father[x] == x) return x;
return father[x] = Find(father[x]);
}
int main()
{
srand(19970806);
cin >> points;
for(int i = 1; i <= points; ++i)
father[i] = i;
for(int x,i = 1; i <= points; ++i) {
scanf("%d",&x);
heap[i] = new (C++)Heap(x);
}
cin >> asks;
for(int x,y,i = 1; i <= asks; ++i) {
scanf("%s",s);
if(s[0] == 'M') {
scanf("%d%d",&x,&y);
if(killed[x] || killed[y]) continue;
int fx = Find(x),fy = Find(y);
if(fx == fy) continue;
father[fy] = fx;
heap[fx] = Merge(heap[fx],heap[fy]);
}
else {
scanf("%d",&x);
if(killed[x]) {
puts("0");
continue;
}
int fx = Find(x);
printf("%d\n",heap[fx]->val);
killed[heap[fx] - mempool] = true;
heap[fx] = Merge(heap[fx]->son[0],heap[fx]->son[1]);
}
}
return 0;
}
相关文章推荐
- iOS设计模式
- SSAS更改默认端口号,使用非默认端口号的时候Olap连接字符串的格式
- 第五章 标准I/O库
- table合并单元格colspan和rowspan
- [Leetcode]20. Valid Parentheses
- Hello freeMarker
- Android自助餐之View生命周期
- WPF中利用Image控件显示bitmap图片
- Linux下的TCP/IP编程----线程及多线程服务端
- Linux 下网络配置
- 传统高斯模糊与优化算法(附完整C++代码)
- 恭贺文化部中国文化网隆重上线
- exports 和 module.exports 的区别
- 字符串字母全排列练习
- c++ new高级用法
- 无法在JDK源码中设置断点---- unable to install breakpoint in ....due to missing line number attributes
- HashMap源码分析
- 2016-6-29 杂感
- android-----事件分发机制测试系列(二)
- C++中substr函数的用法