题意:一个N个点(编号从1开始),M条边的无向图(编号从1开始),有3种操作:

D X:把编号为X的边删了;

Q X K:查询编号为X的结点所在连通分量第K大的元素;

C X V:将编号为X的结点的权值修改为V。

问所有查询的结果的平均值(1 <= N <= 20000, 0 <= M <= 60000, -10^6 <= 点权 <= 10^6, 1 <= Q操作次数 <= 2 * 10^5, C操作次数 <= 2 * 10^5)。

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3032

——>>LJ《训练指南》Treap树的例题,题目中关于Q操作的话:among all vertexes currently connected with vertex X,总觉得指的是与X直接相连的结点,可实现上却应理解为X所在连通分量的所有结点。

#include <cstdio>
#include <cstring>
#include <cstdlib> using namespace std; const int maxn = 20000 + 10;
const int maxm = 60000 + 10;
const int maxc = 500000 + 10; struct Command{
char type;
int x, p;
Command(char type = '\0', int x = 0, int p = 0):type(type), x(x), p(p){}
}; 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& e) const{
return r < e.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;
}
}; int N, M, weight[maxn], from[maxm], to[maxm], fa[maxn], kase, c, query_cnt;
long long query_tot;
bool removed[maxm];
Command commands[maxc];
Node *root[maxn]; int Find(int x){
return x == fa[x] ? x : Find(fa[x]);
} 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;
} 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();
} 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();
} 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 addEdge(int x){
int u = Find(from[x]);
int v = Find(to[x]);
if(u != v){
if(root[u]->s < root[v]->s){
fa[u] = v;
mergeto(root[u], root[v]);
}
else{
fa[v] = u;
mergeto(root[v], root[u]);
}
}
} 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+1) return kth(o->ch[1], k);
else return kth(o->ch[0], k-s-1);
} void query(int x, int k){
query_cnt++;
query_tot += kth(root[Find(x)], k);
} void change_weight(int x, int v){
int u = Find(x);
remove(root[u], weight[x]);
insert(root[u], v);
weight[x] = v;
} void read(){
for(int i = 1; i <= N; i++) scanf("%d", &weight[i]);
for(int i = 1; i <= M; i++) scanf("%d%d", &from[i], &to[i]);
c = 0;
memset(removed, 0, sizeof(removed));
while(1){
char type;
int X, K, V;
scanf(" %c", &type);
if(type == 'E') break;
scanf("%d", &X);
if(type == 'D') removed[X] = 1;
else if(type == 'Q') scanf("%d", &K);
else{
scanf("%d", &V);
K = weight[X];
weight[X] = V;
}
commands[c++] = Command(type, X, K);
}
} void build(){
for(int i = 1; i <= N; i++){
fa[i] = i;
if(root[i] != NULL) removetree(root[i]);
root[i] = new Node(weight[i]);
}
for(int i = 1; i <= M; i++) if(!removed[i]) addEdge(i);
} void solve(){
query_tot = query_cnt = 0;
for(int i = c-1; i >= 0; i--){
if(commands[i].type == 'D') addEdge(commands[i].x);
else if(commands[i].type == 'Q') query(commands[i].x, commands[i].p);
else change_weight(commands[i].x, commands[i].p);
}
printf("Case %d: %.6lf\n", kase++, (double)query_tot / query_cnt);
} int main()
{
kase = 1;
while(scanf("%d%d", &N, &M) == 2){
if(!N && !M) return 0;
read();
build();
solve();
}
return 0;
}

LA - 5031 - Graph and Queries的更多相关文章

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

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

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

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

  3. UVALive - 5031 Graph and Queries (并查集+平衡树/线段树)

    给定一个图,支持三种操作: 1.删除一条边 2.查询与x结点相连的第k大的结点 3.修改x结点的权值 解法:离线倒序操作,平衡树or线段树维护连通块中的所有结点信息,加个合并操作就行了. 感觉线段树要 ...

  4. UVaLive 5031 Graph and Queries (Treap)

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

  5. UVALive 5031 Graph and Queries (Treap)

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

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

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

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

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

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

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

  9. HDU 3726 Graph and Queries treap树

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

随机推荐

  1. Android 自定义View之BounceProgressBar

    之前几天下载了很久没用了的桌面版酷狗来用用的时候,发现其中加载歌曲的等待进度条的效果不错(个人感觉),如下: 然后趁着这周末两天天气较冷,窝在宿舍放下成堆的操作系统作业(目测要抄一节多课的一堆堆文字了 ...

  2. 【Leetcode】二叉树简单路径最大和问题

    问题一:二叉树任意两个叶子间简单路径最大和 示例: -100 /   \ 2   100 /  \ 10   20 思路:这个问题适用于递归思路. 首先,将问题简单化:假设包含最大和summax的简单 ...

  3. vmware重装系统后虚拟机实例文件*.vmdk重用

    如题:vmware重装系统后自定义的:虚拟机名称*.vmdk文件重用. 一.问题描述 系统磁盘坏道,装不上系统直接换了硬盘,但是新装的Vmware不能够通过open方式打开“自定义*.vmdk”(这个 ...

  4. OpenLayers实现覆盖物选择信息提示

    var map; function init() { map = new OpenLayers.Map("map",{projection:"EPSG:3857" ...

  5. uva 10382 Watering Grass_贪心

    题意:给你个矩形n*m,再给你n个圆的圆心坐标和半径,问最用最少用几个圆把这个矩形覆盖 思路:直接想发现这问题不容易,后来发现可以把圆看做区间(能把矩形面积覆盖),然后这个问题就容易解决了 #incl ...

  6. getgrent

    http://baike.baidu.com/link?url=JNyoNvukL-LP7ayYlNNWLv2gPOzn-bjiwuX1CE_QwUTyrRGCWu4NhDW-JznHQoG4aIfw ...

  7. SQL Server中的sysobjects

    摘自:http://www.cnblogs.com/bugY/archive/2011/09/21/2184182.html 关于SQL Server数据库的一切信息都保存在它的系统表格里.我怀疑你是 ...

  8. UIPageViewController跳跃切换的问题

    使用的是XHScrollMenu和UIPageViewController来构建5个页面: ViewController1, ViewController2, ViewController3, Vie ...

  9. [jQuery]无法获取隐藏元素(display:none)宽度(width)和高度(height)的新解决方案

    在做茶城网改版工作的时候,又遇到一个新问题,我需要用jQuery写一个通过点击左右图标来翻阅图片的小插件,写好后测试可以正常运行,但是放到Tab中后发现只有第一个Tab中的代码能够正常运行,其它全部罢 ...

  10. Android学习之简单的数据存储

    在Android中,数据存储是开发人员不可以避免的.Android为开发者提供了很多的存储方法,在前面的博客中,已经讲述了sqlite存储数据.今天将介绍用SharedPreferences来存储数据 ...