反向操作,先求出最终状态,再反向操作。

然后就是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)的更多相关文章

  1. HDU 3726 Graph and Queries treap树

    题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring ...

  2. UVaLive5031 Graph and Queries(时光倒流+名次树)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20332 [思路] 时光倒流+名次树(rank tree). 所谓“ ...

  3. UVaLive 5031 Graph and Queries (Treap)

    题意:初始时给出一个图,每个点有一个权值,三种操作:(1)删除某个边:(2)修改每个点的权值:(3)询问与节点x在一个连通分量中所有点的第K大的权值. 析:首先是要先离线,然后再倒着做,第一个操作就成 ...

  4. LA 5031 Graph and Queries —— Treap名次树

    离线做法,逆序执行操作,那么原本的删除边的操作变为加入边的操作,用名次树维护每一个连通分量的名次,加边操作即是连通分量合并操作,每次将结点数小的子树向结点数大的子树合并,那么单次合并复杂度O(n1lo ...

  5. [la P5031&hdu P3726] Graph and Queries

    [la P5031&hdu P3726] Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: ...

  6. HDU 3726 Graph and Queries 平衡树+前向星+并查集+离线操作+逆向思维 数据结构大综合题

    Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. HDU 3726 Graph and Queries (离线处理+splay tree)

    Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. uvalive 5031 Graph and Queries 名次树+Treap

    题意:给你个点m条边的无向图,每个节点都有一个整数权值.你的任务是执行一系列操作.操作分为3种... 思路:本题一点要逆向来做,正向每次如果删边,复杂度太高.逆向到一定顺序的时候添加一条边更容易.详见 ...

  9. UVALive 5031 Graph and Queries (Treap)

    删除边的操作不容易实现,那么就先离线然后逆序来做. 逆序就变成了合并,用并存集判断连通,用Treap树来维护一个连通分量里的名次. Treap = Tree + Heap.用一个随机的优先级来平衡搜索 ...

随机推荐

  1. 使用powershell链接到Azure

    一:针对传统的的Azure 的登陆界面: (ASM) 1. 正常来说是 现在本地创建证书文件,然后上传文件,以便本地计算机和Azure建立可靠的安全连接 Get-AzurePublishSetting ...

  2. Delphi 实现数字转大写

    从网上找的一段代码 /// <summary> /// 小写转大写 /// </summary> /// <param name="mmje"> ...

  3. jQuery控制表头

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  4. iOS 图片大小压缩 图片尺寸处理

    图片的压缩其实是俩概念,1.是 “压” 文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降,2.是 “缩” 文件的尺寸变小,也就是像素数减少.长宽尺寸变小,文件体积同样会减小. 这个 UII ...

  5. bzoj 2756奇怪的游戏

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MB Description Blinke 最近喜欢上一个奇怪的游戏. 这个游戏 ...

  6. python学习笔记-(十六)python操作mysql

    一. mysql安装 1. windows下安装mysql 1.1. 下载源: http://dev.mysql.com/downloads/installer/,请认准对应版本 Windows (x ...

  7. 在Application中集成Microsoft Translator服务之使用http获取服务

    一.创建项目 首先我们来创建一个ASP.NET Application 选择时尚时尚最时尚的MVC,为了使演示的Demo更简单,这里选择无身份验证 二.创建相关类 项目需要引入之前两个类AdmAcce ...

  8. [NHibernate]一对多关系(级联删除,级联添加)

    目录 写在前面 文档与系列文章 一对多关系 一个例子 级联删除 级联保存 总结 写在前面 在前面的文章中,我们只使用了一个Customer类进行举例,而在客户.订单.产品中它们的关系,咱们并没有涉及, ...

  9. MySQL数据库索引的设计原则

    为了使索引的使用效率更高,在创建索引时,必须考虑在哪些字段上创建索引和创建什么类型的索引. 1.选择唯一性索引 唯一性索引的值是唯一的,可以更快速的通过该索引来确定某条记录.例如,学生表中学号是具有唯 ...

  10. vijos1250 最勇敢的机器人

    背景 Wind设计了很多机器人.但是它们都认为自己是最强的,于是,一场比赛开始了~ 描述 机器人们都想知道谁是最勇敢的,于是它们比赛搬运一些物品. 它们到了一个仓库,里面有n个物品,每个物品都有一个价 ...