题目来源: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树的更多相关文章

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

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

  2. 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 ...

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

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

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

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

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

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

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

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

  7. UVALive5031 Graph and Queries(Treap)

    反向操作,先求出最终状态,再反向操作. 然后就是Treap 的合并,求第K大值. #include<cstdio> #include<iostream> #include< ...

  8. 【HDOJ】3726 Graph and Queries

    Treap的基础题目,Treap是个挺不错的数据结构. /* */ #include <iostream> #include <string> #include <map ...

  9. UVaLive 5031 Graph and Queries (Treap)

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

随机推荐

  1. centos6.9 部署wordpress

    用centos6.9搭建wordpressLinux.Nginx.Mariadb(Mysql).PHP 1 yum install nginx mariadb php php-fpm php-mysq ...

  2. 如何学习Docker

    如何学习Docker 学习Docker,如果没有云计算的基本知识,以及内核的基本知识,那么学习并理解起来会稍吃力.作为容器,Docker容器的优势在哪,不足在哪,最好了解容器的实现是怎样的(简单了解) ...

  3. [thinkphp] 隐藏后台地址

    转自 http://document.thinkphp.cn/manual_3_2.html#module_deploy 如果不希望用户直接访问某个模块,可以设置模块映射(对后台的保护会比较实用). ...

  4. mysql source 乱码

    mysql -u root -p --default-character-set=utf8 use dbname source /root/newsdata.sql

  5. SpringBoot+Mybatis整合实例

    前言 大家都知道springboot有几大特点:能创建独立的Spring应用程序:能嵌入Tomcat,无需部署WAR文件:简化Maven配置:自动配置Spring等等.这里整合mybatis,创建一个 ...

  6. SecureCRT、Xmanager对Linux上传下载文件或文件夹

    (1).SecureCRT SecureCRT对Linux上传下载文件或文件夹拥有一个专门的软件SecureFXPortable.对于它来说只有两个的难题,一个是版本问题,尽量去官网下载最近版本:另一 ...

  7. 36、Flask实战第36天:客户端权限验证

    编辑cms_base.html <li><a href="#">{{ g.cms_user.username }}<span>[超级管理员]&l ...

  8. [2018湖南省队集训] 6.28 T3 simulate

    这道模拟题出的我毫无脾气2333 最重要的是先要发现操作顺序不影响最后的答案,也就是每次随便挑一个>=2的数进行操作最后总是可以得到同样的数列. (这个还不太难想qwq) 但是最骚的是接下来的模 ...

  9. BZOJ 1109 [POI2007]堆积木Klo(树状数组)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1109 [题目大意] Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体. ...

  10. Problem K: 零起点学算法107——统计元音

    #include<stdio.h> int main() { int n; ]; while(scanf("%d%*c",&n)!=EOF) { while(n ...