题意:初始时给出一个图,每个点有一个权值,三种操作:(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)的更多相关文章

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

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

  2. UVALive 5031 Graph and Queries (Treap)

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

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

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

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

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

  5. HDU 3726 Graph and Queries treap树

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

  6. UVALive5031 Graph and Queries(Treap)

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

  7. LA - 5031 - Graph and Queries

    题意:一个N个点(编号从1开始),M条边的无向图(编号从1开始),有3种操作: D X:把编号为X的边删了: Q X K:查询编号为X的结点所在连通分量第K大的元素: C X V:将编号为X的结点的权 ...

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

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

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

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

随机推荐

  1. 斯坦福机器学习视频笔记 Week1 线性回归和梯度下降 Linear Regression and Gradient Descent

    最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...

  2. AIM Tech Round 3 (Div. 2) A , B , C

    A. Juicer time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  3. ubantu删除文件(夹)

    格式:rm -rf 目录名字 -r 就是向下递归,不管有多少级目录,一并删除 -f 就是直接强行删除,不作任何提示的意思 名称 rm - 移除文件或者目录 概述 rm [选项]... 文件列表... ...

  4. Linux课程---1、VMWare安装CentOS虚拟机(安装重要注意)

    Linux课程---1.VMWare安装CentOS虚拟机(安装重要注意) 一.总结 一句话总结: 可以先去百度搜一篇对应系统的安装教程:比如 CentOS 7 安装 1.安装VMWare之后,打开w ...

  5. appium-DesiredCapability详解与实战

    DesiredCapability对启动app至关重要,是启动app前的准备工作.如果配置错误,app不会成功启动. DesiredCapability有appium公共健值对.Android专有和I ...

  6. Java_注解_异常_01_ElementType cannot be resolved to a variable

    一.异常现象: 自定义注解时, @Retention和@Target都能导入进来,但是却报下列错误: RetentionPolicy cannot be resolved to a variable ...

  7. java--xml文件读取(JDOM&DOM4J)

    1.JDOM解析 首先导入额外的jar包: Build Path:jdom-2.0.6.jar 准备工做获取到子节点的集合: package com.imooc_xml.jdom.text; impo ...

  8. 数据交换格式XML和JSON对比

    1.简介: XML:extensible markup language,一种类似于HTML的语言,他没有预先定义的标签,使用DTD(document type definition)文档类型定义来组 ...

  9. luogu2627 修剪草坪

    dp[i]表示1~i最大效率 记一下前缀和 转移就是f[i]=max(f[i],f[j-1]-sum[j])+sum[i] (i-k<=j<=i) 发现括号里的只与j有关 开一个单调队列维 ...

  10. BZOJ1707:[Usaco2007 Nov]tanning分配防晒霜

    我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...