UVALive5031 Graph and Queries(Treap)
反向操作,先求出最终状态,再反向操作。
然后就是Treap 的合并,求第K大值。
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
struct Treap{
int size,key,pri;
Treap *ch[2];
Treap(int key){
size=1;
pri=rand();
this->key=key;
ch[0]=ch[1]=NULL;
}
int compare(int x) const{
if(x==key) return -1;
return x<key? 0:1;
}
void Maintain(){
size=1;
if(ch[0]!=NULL){
size+=ch[0]->size;
}
if(ch[1]!=NULL){
size+=ch[1]->size;
}
}
}; void Rotate(Treap* &t,int d){
Treap *k=t->ch[d^1];
t->ch[d^1]=k->ch[d];
k->ch[d]=t;
t->Maintain();
k->Maintain();
t=k;
} void Insert(Treap* &t,int x){
if(t==NULL){
t=new Treap(x);
}else{
int d=x < t->key ? 0:1;
Insert(t->ch[d],x);
if(t->ch[d]->pri > t->pri){
Rotate(t,d^1);
}
}
t->Maintain();
} void Delete(Treap* &t,int x){
int d=t->compare(x);
if(d==-1){
Treap *tmp=t;
if(t->ch[0]==NULL){
t=t->ch[1];
delete tmp;
tmp=NULL;
}else if(t->ch[1]==NULL){
t=t->ch[0];
delete tmp;
tmp=NULL;
}else{
int k=t->ch[0]->pri > t->ch[1]->pri ? 1:0;
Rotate(t,k);
Delete(t->ch[k],x);
}
}else{
Delete(t->ch[d],x);
}
if(t!=NULL){
t->Maintain();
}
} int Kth(Treap *t,int k){
if(t==NULL||k<=0||k>t->size){
return 0;
}
if(t->ch[0]==NULL&&k==1){
return t->key;
}
if(t->ch[0]==NULL){
return Kth(t->ch[1],k-1);
}
if(t->ch[0]->size>=k){
return Kth(t->ch[0],k);
}
if(t->ch[0]->size+1==k){
return t->key;
}
return Kth(t->ch[1],k-1-t->ch[0]->size);
} void DeleteTreap(Treap* &t){//删除,释放内存
if(t==NULL) return;
if(t->ch[0]!=NULL){
DeleteTreap(t->ch[0]);
}
if(t->ch[1]!=NULL){
DeleteTreap(t->ch[1]);
}
delete t;
t=NULL;
} int n, m;
int g[100000][3];
stack<int> val[40000];
struct Ope{
char t;
int a, b;
void init(char t1, int a1 = 0, int b1 = 0){
t = t1;
a = a1;
b = b1;
}
}op[1000000]; int fa[40000];
Treap* fax[40000]; void mergeTreap(Treap* &a, Treap* &b){
if(a){
if(a -> ch[0]){
mergeTreap(a -> ch[0], b);
}
if(a -> ch[1]){
mergeTreap(a -> ch[1], b);
}
Insert(b, a -> key);
delete a;
a = NULL;
}
} int uFind(int x){
return (fa[x] == x) ? x : fa[x] = uFind(fa[x]);
} void add(int u, int v){
int fx = uFind(u);
int fy = uFind(v);
if(fx != fy){
if(fax[fx] -> size < fax[fy] -> size){
mergeTreap(fax[fx], fax[fy]);
fa[fx] = fy;
}else{
mergeTreap(fax[fy], fax[fx]);
fa[fy] = fx;
} }
} int main(){
int cas = 0;
while(~scanf("%d %d", &n, &m) && n || m){
for(int i = 1; i <= n; i++){
fa[i] = i;
fax[i] = NULL;
int tp;
scanf("%d", &tp);
tp = -tp;
while(!val[i].empty()){
val[i].pop();
}
val[i].push(tp);
}
for(int i = 0; i < m; i++){
int u, v;
scanf("%d %d", &u, &v);
g[i][0] = u;
g[i][1] = v;
g[i][2] = 0;
}
char str[10];
int tot = 0;
while(~scanf("%s", str) && str[0] != 'E'){
int a, b;
if(str[0] == 'D'){
scanf("%d", &a);
a--;
g[a][2] = 1;
}else if(str[0] == 'C'){
scanf("%d %d", &a, &b);
b = -b;
val[a].push(b);
}else{
scanf("%d %d", &a, &b);
}
op[tot++].init(str[0], a, b); }
for(int i = 1; i <= n; i++){
Insert(fax[i], val[i].top());
}
for(int i = 0; i < m; i++){
if(g[i][2] == 0){
add(g[i][0], g[i][1]);
}
}
int cnt = 0;
LL ans = 0;
while(tot--){
if(op[tot].t == 'C'){
int rt = uFind(op[tot].a);
Delete(fax[rt], val[op[tot].a].top());
val[op[tot].a].pop(); Insert(fax[rt], val[op[tot].a].top()); }else if(op[tot].t == 'D'){
int id = op[tot].a;
add(g[id][0], g[id][1]);
}else{
cnt++;
ans += Kth(fax[ uFind(op[tot].a) ], op[tot].b);
}
}
printf("Case %d: %.6f\n", ++cas, (double)-ans / cnt);
for(int i = 1; i <= n; i++){
int fx = uFind(i);
if(fax[fx] != NULL){
DeleteTreap(fax[fx]);
}
}
} return 0;
}
已经释放了内存,但在VS中使用_CrtDumpMemoryLeaks()函数检查还是有内存泄漏问题,原因还没弄清楚
UVALive5031 Graph and Queries(Treap)的更多相关文章
- HDU 3726 Graph and Queries treap树
题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring ...
- UVaLive5031 Graph and Queries(时光倒流+名次树)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20332 [思路] 时光倒流+名次树(rank tree). 所谓“ ...
- UVaLive 5031 Graph and Queries (Treap)
题意:初始时给出一个图,每个点有一个权值,三种操作:(1)删除某个边:(2)修改每个点的权值:(3)询问与节点x在一个连通分量中所有点的第K大的权值. 析:首先是要先离线,然后再倒着做,第一个操作就成 ...
- LA 5031 Graph and Queries —— Treap名次树
离线做法,逆序执行操作,那么原本的删除边的操作变为加入边的操作,用名次树维护每一个连通分量的名次,加边操作即是连通分量合并操作,每次将结点数小的子树向结点数大的子树合并,那么单次合并复杂度O(n1lo ...
- [la P5031&hdu P3726] Graph and Queries
[la P5031&hdu P3726] Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: ...
- HDU 3726 Graph and Queries 平衡树+前向星+并查集+离线操作+逆向思维 数据结构大综合题
Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 3726 Graph and Queries (离线处理+splay tree)
Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- uvalive 5031 Graph and Queries 名次树+Treap
题意:给你个点m条边的无向图,每个节点都有一个整数权值.你的任务是执行一系列操作.操作分为3种... 思路:本题一点要逆向来做,正向每次如果删边,复杂度太高.逆向到一定顺序的时候添加一条边更容易.详见 ...
- UVALive 5031 Graph and Queries (Treap)
删除边的操作不容易实现,那么就先离线然后逆序来做. 逆序就变成了合并,用并存集判断连通,用Treap树来维护一个连通分量里的名次. Treap = Tree + Heap.用一个随机的优先级来平衡搜索 ...
随机推荐
- 解决 Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
转载 php 5个版本,5.2.5.3.5.4.5.5,怕跟不上时代,新的服务器直接上5.5,但是程序出现如下错误:Deprecated: mysql_connect(): The mysql ext ...
- bzoj4498: 魔法的碰撞
首先,如果排列确定,那么就可以组合学解决了,不过排列数很多,显然不能枚举. 我们发现如果区间不能重叠的话,总长度减去所有区间长度就是能用的多余格子数. 然而相邻区间可以重叠较小区间一半的长度,因此这些 ...
- spring-Formatter(格式化器)-validator(验证器)-错误信息定制
项目结构
- WPF基础到企业应用系列6——布局全接触
本文转自:http://knightswarrior.blog.51cto.com/1792698/365351 一. 摘要 首先很高兴这个系列能得到大家的关注和支持,这段时间一直在研究Windows ...
- Unity 难点目录
1.mesh的任意切割.(难点) 2.扇形区域识别玩家.(解决) 3.NGUI横滑同时竖滑,或滑动同时点击冲突处理.(解决)
- ypzl药品质量不合格数据库-excel自动排版
原创: qq:231469242 import xlrdimport pandas,numpyimport matplotlib.pyplot as pltimport pandas as pd #参 ...
- 再谈 $* 和 $@ 在 Bash 中的表现
除非特别说明,本文中出现的 Shell 均指 Bash 4.3.首先说一个基础知识:Shell 中的变量在展开成值(Parameter Expansion)之后,这个值在某些上下文(Context)中 ...
- ROS 5.x自动定时备份并发送到邮箱(实用)
博主使用ROS已经有很长一段时间了,但经常会忘记备份配置与数据库,加上ROS本身自带的User-Man数据库并不是非常稳定,1年中总会出现1-2次数据丢失的情况.所以费了一定功夫才找到真正可用自动备份 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- PHP的继承方法如何获取子类名?get_class() 和 get_called_class()
PHP里的__CLASS__这类东西是静态绑定的,如果不在子类里重载的话,那么继承父类方法所得到的依旧是父类的名称,而不是子类的名称,比如: <?php class A { function _ ...