UVaLive 5031 Graph and Queries (Treap)
题意:初始时给出一个图,每个点有一个权值,三种操作:(1)删除某个边;(2)修改每个点的权值;(3)询问与节点x在一个连通分量中所有点的第K大的权值。
析:首先是要先离线,然后再倒着做,第一个操作就成了加边操作,很容易实现,第二操作,就是分成两个操作,先把x结点删掉,然后再插入一个新结点,
最后一个是就是求某个连通分量的第 k 大,直接用treap直接查找就好,注意问是第 k 大,不是第 k 小。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int w[maxn], a[maxn], b[maxn];
bool removed[maxn];
LL cnt, tot; struct Command{
char type;
int x, p;
};
Command com[maxn];
int p[maxn];
int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } struct Node{
Node *ch[2];
int r, v, s;
Node(int v) : v(v){ ch[0] = ch[1] = nullptr; r = rand(); s = 1; }
bool operator < (const Node &p) const{
return r < p.r;
}
int cmp(int x) const{
if(x == v) return -1;
return x < v ? 0 : 1;
}
void maintain(){
s = 1;
if(ch[0] != nullptr) s += ch[0]->s;
if(ch[1] != nullptr) s += ch[1]->s;
}
}; void Rotate(Node* &o, int d){
Node *k = o->ch[d^1]; o->ch[d^1] = k->ch[d]; k->ch[d] = o;
o->maintain(); k->maintain(); o = k;
} void Insert(Node* &o, int x){
if(o == nullptr) o = new Node(x);
else{
int d = (x < o->v ? 0 : 1);
Insert(o->ch[d], x);
if(o->ch[d]->r > o->r) Rotate(o, d^1);
}
o->maintain();
} void Remove(Node* &o, int x){
int d = o->cmp(x);
if(d == -1){
Node* u = o;
if(o->ch[0] != nullptr && o->ch[1] != nullptr){
int d2 = (o->ch[0]->r > o->ch[1]->r ? 1 : 0);
Rotate(o, d2); Remove(o->ch[d2], x);
}
else{
if(o->ch[0] == nullptr) o = o->ch[1];
else o = o->ch[0];
delete u;
}
}
else Remove(o->ch[d], x);
if(o != nullptr) o->maintain();
}
Node* root[maxn]; void removeTree(Node* &o){
if(o->ch[0] != nullptr) removeTree(o->ch[0]);
if(o->ch[1] != nullptr) removeTree(o->ch[1]);
delete o;
o = nullptr;
} void Merge(Node* &src, Node* &des){
if(src->ch[0] != nullptr) Merge(src->ch[0], des);
if(src->ch[1] != nullptr) Merge(src->ch[1], des);
Insert(des, src->v);
delete src;
src = nullptr;
} void add(int i){
int x = Find(a[i]);
int y = Find(b[i]);
if(x != y){
if(root[x]->s > root[y]->s){ p[y] = x; Merge(root[y], root[x]); }
else{ p[x] = y; Merge(root[x], root[y]); }
}
} int kTh(Node* &o, int k){
if(o == nullptr || k <= 0 || k > o->s) return 0;
int s = (o->ch[1] == nullptr ? 0 : o->ch[1]->s);
if(s + 1 == k) return o->v;
if(k <= s) return kTh(o->ch[1], k);
return kTh(o->ch[0], k - s - 1);
} void change(int i, int v){
int x = Find(i);
Remove(root[x], w[i]);
Insert(root[x], v);
w[i] = v;
} void query(int x, int k){
++cnt; x = Find(x);
tot += kTh(root[x], k);
} int main(){
srand(233333);
int kase = 0;
while(scanf("%d %d", &n, &m) == 2 && m+n){
for(int i = 1; i <= n; ++i) scanf("%d", w+i);
for(int i = 1; i <= m; ++i) scanf("%d %d", a+i, b+i);
memset(removed, 0, sizeof removed); int c = 0;
while(1){
int x, p = 0, v = 0;
char type;
scanf(" %c", &type);
if(type == 'E') break;
scanf("%d", &x);
if(type == 'D') removed[x] = 1;
else if(type == 'Q') scanf("%d", &p);
else {
scanf("%d", &v);
p = w[x]; w[x] = v;
}
com[c++] = (Command){type, x, p};
}
for(int i = 1; i <= n; ++i){
p[i] = i;
root[i] = new Node(w[i]);
}
for(int i = 1; i <= m; ++i) if(!removed[i]) add(i); cnt = tot = 0;
for(int i = c-1; i >= 0; --i){
if(com[i].type == 'D') add(com[i].x);
else if(com[i].type == 'C') change(com[i].x, com[i].p);
else query(com[i].x, com[i].p);
}
printf("Case %d: %f\n", ++kase, cnt == 0 ? 0 : (double)tot / cnt);
for(int i = 1; i <= n; ++i) if(root[i] != nullptr) removeTree(root[i]);
}
return 0;
}
UVaLive 5031 Graph and Queries (Treap)的更多相关文章
- uvalive 5031 Graph and Queries 名次树+Treap
题意:给你个点m条边的无向图,每个节点都有一个整数权值.你的任务是执行一系列操作.操作分为3种... 思路:本题一点要逆向来做,正向每次如果删边,复杂度太高.逆向到一定顺序的时候添加一条边更容易.详见 ...
- UVALive 5031 Graph and Queries (Treap)
删除边的操作不容易实现,那么就先离线然后逆序来做. 逆序就变成了合并,用并存集判断连通,用Treap树来维护一个连通分量里的名次. Treap = Tree + Heap.用一个随机的优先级来平衡搜索 ...
- UVALive - 5031 Graph and Queries (并查集+平衡树/线段树)
给定一个图,支持三种操作: 1.删除一条边 2.查询与x结点相连的第k大的结点 3.修改x结点的权值 解法:离线倒序操作,平衡树or线段树维护连通块中的所有结点信息,加个合并操作就行了. 感觉线段树要 ...
- LA 5031 Graph and Queries —— Treap名次树
离线做法,逆序执行操作,那么原本的删除边的操作变为加入边的操作,用名次树维护每一个连通分量的名次,加边操作即是连通分量合并操作,每次将结点数小的子树向结点数大的子树合并,那么单次合并复杂度O(n1lo ...
- HDU 3726 Graph and Queries treap树
题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring ...
- UVALive5031 Graph and Queries(Treap)
反向操作,先求出最终状态,再反向操作. 然后就是Treap 的合并,求第K大值. #include<cstdio> #include<iostream> #include< ...
- LA - 5031 - Graph and Queries
题意:一个N个点(编号从1开始),M条边的无向图(编号从1开始),有3种操作: D X:把编号为X的边删了: Q X K:查询编号为X的结点所在连通分量第K大的元素: C X V:将编号为X的结点的权 ...
- [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 ...
随机推荐
- Delphi 函数的重载和作用域
1.在delphi 中,我们可以使用相同的函数名来调用不同的函数,我们称这个函数为重载,函数的参数类型和参数的个数可以不同,用到的关键字overload:格式如下: function addInt(x ...
- 【转】BNF和EBNF的含义与用法
[转]BNF和EBNF的含义与用法 BNF 和EBNF的含义与用法 1简介 关于本文 什么是BNF?工作原理 基本原理 一个实例 EBNF及其用途 ...
- linux新增用户和删除用户
新增用户 新增用户命令:useradd 参数: 参数 说明 -u 指定UID,也就是自定义UID -g 知道GID,也就是初始化用户组,/etc/passwd文件中的第四个字段. -G 后面接用户组的 ...
- 英语发音规则---oo
英语发音规则---oo 一.总结 一句话总结: 1.重读音节词尾的字母组合oo发音素[u:]的音? too [tu:] adv.太;也 zoo [zu:] n.动物园 room [ru:m] n.房间 ...
- “Hello World”—— 第一个汇编程序
Hello World这是每一门编程语言的第一个最简单程序,下面那个程序就是汇编语言的Hello World.学汇编一段时间了,到现在才记录下自己的第一个汇编程序笔记.虽然这是个相当简单的小程序,但这 ...
- mesos的zookeeper变更
采用rpm方式安装你了mesos,碰到zookeeper(采用了cloudera的zookeeper)的IP地址变化了,肿么办? 在master机器中: /etc/mesos/zk进行编辑修改zk路径 ...
- TVYJ1266:费解的开关
我对状态空间的理解:https://www.cnblogs.com/AKMer/p/9622590.html 题目传送门:http://www.joyoi.cn/problem/tyvj-1266 这 ...
- HDOJ1059(多重背包)
1.解法一:多重背包 #include<iostream> #include<cstdio> using namespace std; #define MAX(a,b) (a& ...
- C++11中的原子操作(atomic operation)
所谓的原子操作,取的就是“原子是最小的.不可分割的最小个体”的意义,它表示在多个线程访问同一个全局资源的时候,能够确保所有其他的线程都不在同一时间内访问相同的资源.也就是他确保了在同一时刻只有唯一的线 ...
- 关于 vs 2012 键盘无法输入的问题
使用vs2012 新建了一个类文件之后,vs2012的编辑界面突然出现奇怪的问题,键盘无法输入! 最后调查的结果是由于resharper插件导致的. 可以将插件禁用然后启用. 也可以删除resharp ...