[HDU3726]Graph and Queries
Problem
给你一张图,点的权值,边和几个操作:
D x: 删除第x条边
Q x y: 询问包含x的联通块中权值第y大的权值
C x y: 将x这个点的权值改为y
Solution
一看就要离线处理,把所有操作都倒过来
然后删除操作变为加边操作
Notice
记得: 是改完以后再把点一个一个加入Treap中!!
Code
非旋转Treap
#pragma GCC optimize(2)
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 3000000;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
struct Node
{
int num1, num2, type;
}Q[N + 5];
int point = 0, fa[N + 5], Root[N + 5], T[N + 5], From[N + 5], To[N + 5], Flag[N + 5];
struct node
{
int Val[N + 5], Level[N + 5], Size[N + 5], Son[2][N + 5], Num[N + 5];
inline void up(int u)
{
Size[u] = Size[Son[0][u]] + Size[Son[1][u]] + 1;
}
int Newnode(int v)
{
int u = ++point;
Val[u] = v, Level[u] = rand();
Son[0][u] = Son[1][u] = 0, Size[u] = 1;
return u;
}
int Merge(int X, int Y)
{
if (X * Y == 0) return X + Y;
if (Level[X] < Level[Y])
{
Son[1][X] = Merge(Son[1][X], Y);
up(X); return X;
}
else
{
Son[0][Y] = Merge(X, Son[0][Y]);
up(Y); return Y;
}
}
void Split(int u, int t, int &x, int &y)
{
if (!u)
{
x = y = 0;
return;
}
if (Val[u] <= t) x = u, Split(Son[1][u], t, Son[1][u], y);
else y = u, Split(Son[0][u], t, x, Son[0][u]);
up(u);
}
int Find_num(int u, int v)
{
if (!u) return 0;
if (v <= Size[Son[0][u]]) return Find_num(Son[0][u], v);
else if (v <= Size[Son[0][u]] + 1) return u;
else return Find_num(Son[1][u], v - Size[Son[0][u]] - 1);
}
void Insert(int &u, int v)
{
int t = Newnode(v), x, y;
Split(u, v, x, y);
u = Merge(Merge(x, t), y);
}
void Delete(int &u, int v)
{
int x, y, z;
Split(u, v, x, z), Split(x, v - 1, x, y);
u = Merge(Merge(x, Merge(Son[0][y], Son[1][y])), z);
}
}Treap;
int Find(int x)
{
if (fa[x] != x) fa[x] = Find(fa[x]);
return fa[x];
}
void Union(int u, int v)
{
if (Treap.Size[Root[u]] < Treap.Size[Root[v]]) swap(u, v);
while (Treap.Size[Root[v]])
{
int t = Treap.Find_num(Root[v], 1);
Treap.Insert(Root[u], Treap.Val[t]);
Treap.Delete(Root[v], Treap.Val[t]);
}
fa[v] = u;
Root[v] = 0;
}
int sqz()
{
int n, m, cas = 0;
while (~scanf("%d %d", &n, &m) && (n || m))
{
point = 0;
rep(i, 1, n) T[i] = read(), fa[i] = i, Root[i] = 0;
rep(i, 1, m) From[i] = read(), To[i] = read(), Flag[i] = 0;
int q = 0; char op[5];
while (scanf("%s", op) && op[0] != 'E')
{
q++;
if (op[0] == 'D')
Q[q].num1 = read(), Flag[Q[q].num1] = 1, Q[q].type = 0;
else
{
Q[q].num1 = read(), Q[q].num2 = read();
if (op[0] == 'C') swap(T[Q[q].num1], Q[q].num2), Q[q].type = 1;
else Q[q].type = 2;
}
}
rep(i, 1, n) Treap.Insert(Root[i], T[i]);
rep(i, 1, m)
if (!Flag[i])
{
int u = Find(From[i]), v = Find(To[i]);
if (u != v) Union(u, v);
}
ll ans = 0; int tot = 0;
per(i, q, 1)
{
if (Q[i].type == 0)
{
int u = Find(From[Q[i].num1]), v = Find(To[Q[i].num1]);
if (u != v) Union(u, v);
}
else if (Q[i].type == 1)
{
int u = Find(Q[i].num1);
Treap.Delete(Root[u], T[Q[i].num1]);
Treap.Insert(Root[u], Q[i].num2);
T[Q[i].num1] = Q[i].num2;
}
else
{
int u = Find(Q[i].num1);
int t = Treap.Find_num(Root[u], Treap.Size[Root[u]] - Q[i].num2 + 1);
if (t != -INF) ans += Treap.Val[t];
tot++;
}
}
printf("Case %d: %.6f\n", ++cas, ans * 1.0 / tot);
}
return 0;
}
旋转Treap
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 3000000;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
struct Node
{
int num1, num2, type;
}Q[N + 5];
int point = 0, fa[N + 5], Root[N + 5], T[N + 5], From[N + 5], To[N + 5], Flag[N + 5];
struct node
{
int Val[N + 5], Level[N + 5], Size[N + 5], Son[2][N + 5], Num[N + 5];
inline void up(int u)
{
Size[u] = Size[Son[0][u]] + Size[Son[1][u]] + Num[u];
}
inline void Newnode(int &u, int v)
{
u = ++point;
Level[u] = rand(), Val[u] = v;
Size[u] = Num[u] = 1, Son[0][u] = Son[1][u] = 0;
}
inline void Lturn(int &x)
{
int y = Son[1][x]; Son[1][x] = Son[0][y], Son[0][y] = x;
up(x); up(y); x = y;
}
inline void Rturn(int &x)
{
int y = Son[0][x]; Son[0][x] = Son[1][y], Son[1][y] = x;
up(x); up(y); x = y;
}
void Insert(int &u, int t)
{
if (u == 0)
{
Newnode(u, t);
return;
}
Size[u]++;
if (t == Val[u]) Num[u]++;
else if (t > Val[u])
{
Insert(Son[0][u], t);
if (Level[Son[0][u]] < Level[u]) Rturn(u);
}
else if (t < Val[u])
{
Insert(Son[1][u], t);
if (Level[Son[1][u]] < Level[u]) Lturn(u);
}
}
void Delete(int &u, int t)
{
if (!u) return;
if (Val[u] == t)
{
if (Num[u] > 1)
{
Num[u]--, Size[u]--;
return;
}
if (Son[0][u] * Son[1][u] == 0) u = Son[0][u] + Son[1][u];
else if (Level[Son[0][u]] < Level[Son[1][u]]) Rturn(u), Delete(u, t);
else Lturn(u), Delete(u, t);
}
else if (t > Val[u]) Size[u]--, Delete(Son[0][u], t);
else Size[u]--, Delete(Son[1][u], t);
}
int Find_num(int u, int t)
{
if (!u) return -INF;
if (t <= Size[Son[0][u]]) return Find_num(Son[0][u], t);
else if (t <= Size[Son[0][u]] + Num[u]) return Val[u];
else return Find_num(Son[1][u], t - Size[Son[0][u]] - Num[u]);
}
}Treap;
int Find(int x)
{
if (fa[x] != x) fa[x] = Find(fa[x]);
return fa[x];
}
void Union(int u, int v)
{
if (Treap.Size[Root[u]] < Treap.Size[Root[v]]) swap(u, v);
while (Treap.Size[Root[v]])
{
int t = Treap.Find_num(Root[v], 1);
Treap.Insert(Root[u], t);
Treap.Delete(Root[v], t);
}
fa[v] = u;
Root[v] = 0;
}
int sqz()
{
int n, m, cas = 0;
while (~scanf("%d %d", &n, &m) && (n || m))
{
point = 0;
rep(i, 1, n) T[i] = read(), fa[i] = i, Root[i] = 0;
rep(i, 1, m) From[i] = read(), To[i] = read(), Flag[i] = 0;
int q = 0; char op[5];
while (scanf("%s", op) && op[0] != 'E')
{
q++;
if (op[0] == 'D')
Q[q].num1 = read(), Flag[Q[q].num1] = 1, Q[q].type = 0;
else
{
Q[q].num1 = read(), Q[q].num2 = read();
if (op[0] == 'C') swap(T[Q[q].num1], Q[q].num2), Q[q].type = 1;
else Q[q].type = 2;
}
}
rep(i, 1, n) Treap.Insert(Root[i], T[i]);
rep(i, 1, m)
if (!Flag[i])
{
int u = Find(From[i]), v = Find(To[i]);
if (u != v) Union(u, v);
}
ll ans = 0; int tot = 0;
per(i, q, 1)
{
if (Q[i].type == 0)
{
int u = Find(From[Q[i].num1]), v = Find(To[Q[i].num1]);
if (u != v) Union(u, v);
}
else if (Q[i].type == 1)
{
int u = Find(Q[i].num1);
Treap.Delete(Root[u], T[Q[i].num1]);
Treap.Insert(Root[u], Q[i].num2);
T[Q[i].num1] = Q[i].num2;
}
else
{
int u = Find(Q[i].num1);
int t = Treap.Find_num(Root[u], Q[i].num2);
if (t != -INF) ans += t;
tot++;
}
}
printf("Case %d: %.6f\n", ++cas, ans * 1.0 / tot);
}
return 0;
}
[HDU3726]Graph and Queries的更多相关文章
- 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 (离线处理+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: ...
- HDU 3726 Graph and Queries treap树
题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring ...
- 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 ...
- UVALive5031 Graph and Queries(Treap)
反向操作,先求出最终状态,再反向操作. 然后就是Treap 的合并,求第K大值. #include<cstdio> #include<iostream> #include< ...
- UVa 1479 (Treap 名次树) Graph and Queries
这题写起来真累.. 名次树就是多了一个附加信息记录以该节点为根的树的总结点的个数,由于BST的性质再根据这个附加信息,我们可以很容易找到这棵树中第k大的值是多少. 所以在这道题中用一棵名次树来维护一个 ...
- uvalive 5031 Graph and Queries 名次树+Treap
题意:给你个点m条边的无向图,每个节点都有一个整数权值.你的任务是执行一系列操作.操作分为3种... 思路:本题一点要逆向来做,正向每次如果删边,复杂度太高.逆向到一定顺序的时候添加一条边更容易.详见 ...
- 【HDOJ】3726 Graph and Queries
Treap的基础题目,Treap是个挺不错的数据结构. /* */ #include <iostream> #include <string> #include <map ...
随机推荐
- JS 字符串两边截取空白的trim()方法的封装
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 梯度消失 / 梯度爆炸以及Xavier初始化
2018-12-06 16:25:08 首先我们先来看一下求解梯度的公式,以下面三层的网络为例: 如果w初始化为大于1的数字,在深层神经网络计算梯度的时候就会出现梯度爆炸的现象: 如果w初始化为小于1 ...
- 数据库:Mysql中“select ... for update”排他锁分析
Mysql InnoDB 排他锁 用法: select … for update; 例如:select * from goods where id = 1 for update; 排他锁的申请前提:没 ...
- Node.js 知识(教程)
JavaScript on the Server JavaScript was originally built for web browsers, but with Node.js we can u ...
- ASP.Net MVC多语言
.NET MVC 多语言网站 通过浏览器语言首选项改变MVC的语言,通过浏览器语言选项,修改脚本语言. 一.添加资源文件 1.添加App_GlobalResources文件夹. 2.添加默认的资源文件 ...
- android -------- 蓝牙Bluetooth
什么是蓝牙? 也可以说是蓝牙技术.所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,是由爱立信公司公司发明的.利用“蓝牙”技术,能够有效地简化掌上电脑.笔记本电脑和移动电话手机等移动通 ...
- 百度基础架构组-实习生面试(2016.08 java后台开发)
一.项目 1.Spring MVC与Struts2的区别: 2.MVC三层是如何工作的?比如:要访问一个Url?a=xx&b=xx,怎么找到相应的资源,怎么运算,怎么返回等? 3.数据库myb ...
- XXX系统利益相关者分析
小组成员:白悦,张雪薇,李慧,陶雨洁 目标:实现需求的网上填报,征集. 好处: 1.便于统计 2.节约时间,成本 3.快捷简单易操作 度量标准:填报所用时间,精力,以及需求数据整理的有效性. 利益相关 ...
- 笔记react router 4(三)
上一篇我们说到了路由组件的嵌套.想必你已经运用自如了.那么,这一次我们来聊一聊4.X中Router的变更. 在3.X中我们若使用路由的模式,可通过在Router上配置history的值即可. 例如, ...
- atom - Emmet插件使用,代码快速填写
参考转载:http://www.hangge.com/blog/cache/detail_1537.html 用法: 输入:ul>li*6 接着按:tab键 常用语法: 1.后代>: ...