HDU 3726 Graph and Queries treap树
题目来源:HDU 3726 Graph and Queries
题意:见白书
思路:刚学treap 參考白皮书
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; struct Node
{
Node *ch[2];
int r;
int v;
int s;
Node(int v): v(v) {
ch[0] = ch[1] = NULL; r = rand(); s = 1;
}
bool operator < (const Node& rhs) const{
return r < rhs.r;
}
int cmp(int x) const{
if(x == v) return -1;
return x < v ? 0 : 1;
}
void maintain(){
s = 1;
if(ch[0] != NULL) s += ch[0]->s;
if(ch[1] != NULL) 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 == NULL){
o = new Node(x);
}
else{
int d = (x < o->v ? 0 : 1);
insert(o->ch[d], x);
if(o->ch[d] > o) rotate(o, d^1);
}
o->maintain();
//printf("--------+%d\n", o->s);
} void remove(Node* &o, int x){
int d = o->cmp(x);
if(d == -1){
Node* u = o;
if(o->ch[0] != NULL && o->ch[1] != NULL){
int d2 = o->ch[0] > o->ch[1] ? 1 : 0;
rotate(o, d2); remove(o->ch[d2], x);
}
else{
if(o->ch[0] == NULL) o = o->ch[1];
else o = o->ch[0];
delete u;
}
}
else
remove(o->ch[d], x);
if(o != NULL) o->maintain();
} const int maxn = 20010;
const int maxc = 500010;
int n, m, w[maxn], from[maxn*3], to[maxn*3], removed[maxn*3];
int f[maxn]; Node* root[maxn]; struct Command
{
char type;
int x, p;
Command(){}
Command(char type, int x, int p): type(type), x(x), p(p) { }
}commands[maxc]; int find(int x)
{
if(x != f[x])
return f[x] = find(f[x]);
return f[x];
}
int kth(Node* o, int k){
if(o == NULL || k <= 0 || k > o->s)
return 0;
int s = (o->ch[1] == NULL ? 0 : o->ch[1]->s);
if(k == s+1) return o->v;
else if(k <= s) return kth(o->ch[1], k);
else return kth(o->ch[0], k-s-1);
} void mergeto(Node* &src, Node* &dest){ if(src->ch[0] != NULL) mergeto(src->ch[0], dest);
if(src->ch[1] != NULL) mergeto(src->ch[1], dest);
insert(dest, src->v);
delete src;
src = NULL;
} void add_edge(int x){
int u = find(from[x]), v = find(to[x]);
//printf("-/++++%d %d\n", u, v);
if(u != v){
if(root[u]->s > root[v]->s){
f[v] = u;
//printf("----%d %d\n", root[v]->s, root[u]->s);
mergeto(root[v], root[u]);
//printf("----%d\n", root[u]->s);
}
else{
f[u] = v;
//printf("----%d %d\n", root[u]->s, root[v]->s);
mergeto(root[u], root[v]);
//printf("++++%d\n", root[v]->s);
}
}
} void removetree(Node* &x){
if(x->ch[0] != NULL) removetree(x->ch[0]);
if(x->ch[1] != NULL) removetree(x->ch[1]);
delete x;
x = NULL;
} int query_cnt;
long long query_tol; void query(int x, int k){
query_cnt++;
//printf("%d %d\n", root[find(x)]->s, kth(root[find(x)], k));
query_tol += kth(root[find(x)], k);
} void change_weight(int x, int v){
int u = find(x);
remove(root[u], w[x]);
insert(root[u], v);
w[x] = v;
}
int main()
{
int kase = 0;
while(scanf("%d %d", &n, &m) && (n||m))
{
for(int i = 1; i <= n; i++)
scanf("%d", &w[i]);
for(int i = 1; i <= m; i++)
scanf("%d %d", &from[i], &to[i]);
memset(removed, 0, sizeof(removed));
int c = 0;
while(1)
{
char type[2];
int x, p = 0, v = 0;
scanf("%s", type);
if(type[0] == 'E')
break;
scanf("%d", &x);
if(type[0] == 'D')
removed[x] = 1;
if(type[0] == 'Q')
scanf("%d", &p);
if(type[0] == 'C')
{
scanf("%d", &v);
p = w[x];
w[x] = v;
}
commands[c++] = Command(type[0], x, p);
} for(int i = 1; i <= n; i++)
{
f[i] = i;
if(root[i] != NULL)
removetree(root[i]);
root[i] = new Node(w[i]);
//printf("******%d\n", root[i]->s);
}
for(int i = 1; i <= m; i++)
if(!removed[i])
{
add_edge(i);
//puts("gh");
}
query_tol = query_cnt = 0;
for(int i = c-1; i >= 0; i--){
if(commands[i].type == 'D')
add_edge(commands[i].x);
if(commands[i].type == 'Q')
query(commands[i].x, commands[i].p);
if(commands[i].type == 'C')
change_weight(commands[i].x, commands[i].p);
}
printf("Case %d: %.6lf\n", ++kase, query_tol/(double)query_cnt);
}
return 0;
}
HDU 3726 Graph and Queries treap树的更多相关文章
- 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(平衡二叉树)(2010 Asia Tianjin Regional Contest)
Description You are given an undirected graph with N vertexes and M edges. Every vertex in this grap ...
- HDU 3726 Graph and Queries (离线处理+splay tree)
Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- [la P5031&hdu P3726] Graph and Queries
[la P5031&hdu P3726] Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: ...
- uvalive 5031 Graph and Queries 名次树+Treap
题意:给你个点m条边的无向图,每个节点都有一个整数权值.你的任务是执行一系列操作.操作分为3种... 思路:本题一点要逆向来做,正向每次如果删边,复杂度太高.逆向到一定顺序的时候添加一条边更容易.详见 ...
- LA 5031 Graph and Queries —— Treap名次树
离线做法,逆序执行操作,那么原本的删除边的操作变为加入边的操作,用名次树维护每一个连通分量的名次,加边操作即是连通分量合并操作,每次将结点数小的子树向结点数大的子树合并,那么单次合并复杂度O(n1lo ...
- UVALive5031 Graph and Queries(Treap)
反向操作,先求出最终状态,再反向操作. 然后就是Treap 的合并,求第K大值. #include<cstdio> #include<iostream> #include< ...
- 【HDOJ】3726 Graph and Queries
Treap的基础题目,Treap是个挺不错的数据结构. /* */ #include <iostream> #include <string> #include <map ...
- UVaLive 5031 Graph and Queries (Treap)
题意:初始时给出一个图,每个点有一个权值,三种操作:(1)删除某个边:(2)修改每个点的权值:(3)询问与节点x在一个连通分量中所有点的第K大的权值. 析:首先是要先离线,然后再倒着做,第一个操作就成 ...
随机推荐
- vivo手机的坑-禁止微信浏览器网页点击图片,图片会自动放大
这个坑让我郁闷极了,之前我的手机在微信浏览器打开网页点击图片时,啥事也没有 现在,咋回事,变了呢! 现在我打开微信浏览器网页面html,点击一个img标签的图片,图片会自动满屏放大,吓死宝宝了.其他人 ...
- centos 7 开机启动配置
centos 7 开机启动 1 开机启动配置文件位于/usr/lib/systemd/system/ 2 nginx的配置[Unit]Description=nginx - high performa ...
- (6) go 流程控制
一. if else (1)如果只有一条语句,大括号不能省略 (2)右括号 和 else 在一行 (3)支持 if 时可以定义变量 (4)if 的风格尽量不要加括号,用空格代替 (5)多分支 二.sw ...
- AngularJS自定义指令及指令配置项
两种写法 //第一种 angular.module('MyApp',[]) .directive('zl1',zl1) .controller('con1',['$scope',func1]); fu ...
- 丑数(LintCode)
丑数 设计一个算法,找出只含素因子3,5,7 的第 k大的数. 符合条件的数如:3,5,7,9,15...... 您在真实的面试中是否遇到过这个题? Yes 样例 如果k=4, 返回 9 挑战 要求时 ...
- HDU6074 Phone Call (并查集 LCA)
Phone Call Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Tota ...
- Sqli-labs less 12
Less-12 本关和less11是类似的,只是在id 的参数的处理上有一定的不同 当输入username:admin" Password: (随便) 报错后的结果为: You have a ...
- nyoj 300 (矩阵快速幂)Kiki & Little Kiki 2
描述 There are n lights in a circle numbered from 1 to n. The left of light 1 is light n, and the left ...
- 【20181019T2】硬币【矩阵快速幂优化DP】
题面 [错解] 哎\(N \leq 50\)?双向搜索? 切了切-- 等下,好像要求方案数-- 好像搜不了 哎他给\(V_{i} | V_{i+1}\)干嘛? 肯定有用啊 为了体现条件的用处,我在搜下 ...
- BZOJ 3632 外太空旅行(最大团)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3632 [题目大意] 求图中的最大团. [题解] 最大团问题是npc问题,因此可以考虑随 ...