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大的权值. 析:首先是要先离线,然后再倒着做,第一个操作就成 ...
随机推荐
- JavaScript best practices JS最佳实践
JavaScript best practices JS最佳实践 0 简介 最佳实践起初比较棘手,但最终会让你发现这是非常明智之举. 1.合理命名方法及变量名,简洁且可读 var someItem = ...
- 继续ajax长轮询解决方案--递归
如果使用for,会有一种情况发生,就是ajax的执行会大于其他的动作的执行,那么这样的一段代码就不能实现了 for(var i=0;i<20;i++){ console.log('你好') $. ...
- 利用tengine的nginx_upstream_check_module来检测后端服务状态
nginx_upstream_check_module 是专门提供负载均衡器内节点的健康检查的外部模块,由淘宝的姚伟斌大神开发,通过它可以用来检测后端 realserver 的健康状态.如果后端 re ...
- nginxhttp请求限制丶tcp会话限制和下载速度限制
(1)nginx请求限制 ngx_http_limit_req_module:开启对单个ip丶单个会话在单位时间内请求的限制rate表示限制的速率 1.修改nginx配置文件 #vim /usr/lo ...
- Druid 架构
本篇译自 Druid 项目白皮书部分内容( https://github.com/apache/incubator-druid/tree/master/publications/whitepaper/ ...
- 转:VMware攻击界面分析
转:https://comsecuris.com/blog/posts/vmware_vgpu_shader_vulnerabilities/ Wandering through the Shady ...
- 洛谷P1940买蛋糕
题目传送门 题意:给定你一个数n,要求用最小个数的整数组成小于等于n的所有整数,并求出方案数. 很明显,擅长二进制的大犇们肯定一眼就看得出方案数是log2(n)+1,然而我并不擅长,但是推了一小会儿也 ...
- NOI2015 D1T2 软件包管理器
题目传送门; 这个貌似是我这个蒟蒻做的第一道NOI系列的题了吧...这题的算法是树链剖分,其实基本上就是很常见的树剖+线段树,题目既然是要求每次安装或卸载改变的软件包的数目,那么就在每次操作前记录下线 ...
- oracle10g中判断字段是否为空的坑
RT,在oracle中,写SQL时,假设这个字段为STA Char(3),判断这个字段是否为空一般都是这两个:STA = '' or STA is null 但是今天这两种方法失效了,无论是STA = ...
- 【贪心】【堆】bzoj2590 [Usaco2012 Feb]Cow Coupons
每个物品有属性a,b 考虑在仅仅用光优惠券时的最优方案. 显然是按照b排序,取前K个. 但是我们还要尽可能去取剩余的. 假设朴素地取剩余的话,应该把剩余的对a排序,然后尽量去取. 但是有可能对其用优惠 ...