hdu1512 左偏树(可并堆)
来源:互联网 发布:拍网络电影多少钱 编辑:程序博客网 时间:2024/05/29 18:37
#include <iostream>#include <cstring>#include <cstdio>#include<cmath>using namespace std;const int N= 1e5+10;typedef long long LL;struct node{ int l,r,dis,val,dad;}heap[N<<2];inline int max(const int &a,const int &b){ return a>b?a:b;}inline void swap(int &a,int &b){ a^=b^=a^=b;}inline int find(int &x){ return heap[x].dad==x?x:heap[x].dad=find(heap[x].dad);}int merge(int x,int y){ if(x==0) return y; if(y==0) return x; if(heap[x].val<heap[y].val) swap(x,y); heap[x].r = merge(heap[x].r,y); heap[heap[x].r].dad = x; if(heap[heap[x].l].dis<heap[heap[x].r].dis) swap(heap[x].l,heap[x].r); if(heap[x].r==0) heap[x].dis = 0; else heap[x].dis = heap[heap[x].r].dis+1; return x;}inline int pop(int &x){ int l = heap[x].l; int r = heap[x].r; heap[l].dad = l; heap[r].dad = r; heap[x].dis = heap[x].l = heap[x].r = 0; return merge(l,r);}inline int push(int x,int y){ return merge(x,y);}int main(){ int num, que; while(scanf("%d",&num)==1){ for(int i=1;i<=num;i++){ scanf("%d",&heap[i].val); heap[i].l=heap[i].r=heap[i].dis=0; heap[i].dad= i ; } scanf("%d",&que); int a,b,x,y; while(que--){ scanf("%d%d",&a,&b); x = find(a); y = find(b); if(x==y) puts("-1"); else{ heap[x].val/=2; int xx = pop(x); xx = push(xx,x); heap[y].val/=2; int yy = pop(y); yy = push(yy,y); int tmp = merge(yy,xx); printf("%d\n",heap[tmp].val); } } } return 0;}
0 0
- hdu1512 左偏树(可并堆)
- HDU1512 左偏树(可并堆)
- HDU1512 可并堆
- [HDU1512]Monkey King(可并堆)
- hdu1512 Monkey King(可并堆插入+删除)
- [HDU1512/ZOJ2334]Monkey King-左偏树-可合并堆
- HDU1512 左偏树||斜堆
- HDU1512 Monkey King 解题报告【数据结构】【可并堆】【并查集】
- 左偏树(可并堆)
- 可并堆?左偏树?
- 左偏树/斜堆/可并堆-洛谷P3377 【模板】左偏树(可并堆)
- 可并堆(左偏树)简单学习
- 数据结构--左偏树(可并堆)
- luoguP3377 【模板】左偏树(可并堆)
- 左偏树(可并堆)模板
- 斜堆——【模板】左偏树(可并堆)
- 可并堆之左偏树
- 【左偏树(可并堆)模板】
- hdu 1325 Is It A Tree?
- 总结 特征选择(feature selection)算法笔记
- Spark1.3从创建到提交:9)Stage的划分和提交源码分析
- Kafka安装配置测试
- SpringMVC整合mybatis(附源码)
- hdu1512 左偏树(可并堆)
- [小技巧] trac 如何设置 Search 的最小长度
- IDEA【spark&mapreduce混合配置】mapreduce spark 本地调试
- 泛型和继承
- Minimum Window Substring
- Tomcat源码分析-线程池应用
- (翻译)draft.js 入门
- 查看程序或dll依赖的dll
- 折半查找法