【HDOJ】3726 Graph and Queries
Treap的基础题目,Treap是个挺不错的数据结构。
/* */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
char c;
int x, y;
} Cmd; typedef struct Node {
Node* ch[];
int r, v, s; Node() {} Node(int v_) {
ch[] = ch[] = NULL;
r = rand();
v = v_;
s = ;
} friend bool operator<(const Node& a, const Node& b) {
return a.r < b.r;
} int cmp(int x) const {
if (x == v) return -;
return x<v ? :;
} void maintain() {
s = ;
if (ch[] != NULL) s += ch[]->s;
if (ch[] != NULL) s += ch[]->s;
} } Node; void rotate(Node*& o, int d) {
Node* k = o->ch[d^];
o->ch[d^] = 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 ? :;
insert(o->ch[d], x);
if (o->ch[d]->r > o->r)
rotate(o, d^);
}
o->maintain();
} void remove(Node*& o, int x) {
int d = o->cmp(x); if (d == -) {
Node* u = o;
if (o->ch[]!=NULL && o->ch[]!=NULL) {
int d2 = o->ch[]->r > o->ch[]->r ? :;
rotate(o, d2);
remove(o->ch[d2], x);
} else {
if (o->ch[] == NULL)
o = o->ch[];
else
o = o->ch[];
delete u;
}
} else {
remove(o->ch[d], x);
}
if (o != NULL)
o->maintain();
} const int maxc = 5e5+;
const int maxn = 2e4+;
const int maxm = 6e4+;
Cmd cmd[maxc];
Node* rt[maxn];
int W[maxn], pre[maxn];
int U[maxm], V[maxm];
bool mark[maxm]; int find(int x) {
if (x == pre[x])
return x;
return pre[x] = find(pre[x]);
} void removetree(Node*& x) {
if (x->ch[] != NULL) removetree(x->ch[]);
if (x->ch[] != NULL) removetree(x->ch[]);
delete x;
x = NULL;
} void mergeto(Node*& src, Node*& des) {
if (src->ch[] != NULL) mergeto(src->ch[], des);
if (src->ch[] != NULL) mergeto(src->ch[], des);
insert(des, src->v);
delete src;
src = NULL;
} void addEdge(int k) {
int u = U[k], v = V[k];
int fu = find(u), fv = find(v); if (fu != fv) {
if (rt[fu]->s < rt[fv]->s) {
pre[fu] = fv;
mergeto(rt[fu], rt[fv]);
} else {
pre[fv] = fu;
mergeto(rt[fv], rt[fu]);
}
}
} int kth(Node* o, int k) {
if (o==NULL || k<= || k>o->s)
return ; int s = o->ch[]==NULL ? :o->ch[]->s;
if (k == s+)
return o->v;
else if (k <= s)
return kth(o->ch[], k);
else
return kth(o->ch[], k-s-);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int tt = ;
int n, m, nc;
int x, y;
char s[];
__int64 tot, ans; while (scanf("%d %d",&n,&m)!=EOF && (n||m)) {
rep(i, , n+)
scanf("%d", &W[i]);
rep(i, , m+)
scanf("%d %d", &U[i], &V[i]); memset(mark, false, sizeof(mark));
nc = ;
while (scanf("%s", s)!=EOF && s[]!='E') {
cmd[nc].c = s[];
if (s[] == 'D') {
scanf("%d", &x);
cmd[nc].x = x;
mark[x] = true;
} else if (s[] == 'Q') {
scanf("%d %d", &x, &y);
cmd[nc].x = x;
cmd[nc].y = y;
} else if (s[] == 'C') {
scanf("%d %d", &x, &y);
cmd[nc].x = x;
cmd[nc].y = W[x];
W[x] = y;
}
++nc;
} rep(i, , n+) {
pre[i] = i;
if (rt[i] != NULL)
removetree(rt[i]);
rt[i] = new Node(W[i]);
} rep(i, , m+) {
if (!mark[i])
addEdge(i);
} ans = tot = ;
per(i, , nc) {
if (cmd[i].c == 'D') {
addEdge(cmd[i].x);
} else if (cmd[i].c == 'Q') {
int fx = find(cmd[i].x);
++tot;
ans += kth(rt[fx], cmd[i].y);
} else if (cmd[i].c == 'C') {
int fx = find(cmd[i].x);
remove(rt[fx], W[cmd[i].x]);
insert(rt[fx], cmd[i].y);
W[cmd[i].x] = cmd[i].y;
}
}
printf("Case %d: %.06lf\n", ++tt, 1.0*ans/tot);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】3726 Graph and Queries的更多相关文章
- 【HDOJ】3560 Graph’s Cycle Component
并查集的路径压缩. #include <stdio.h> #include <string.h> #define MAXNUM 100005 int deg[MAXNUM], ...
- HDU 3726 Graph and Queries treap树
题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring ...
- 【LeetCode】图论 graph(共20题)
[133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...
- 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 ...
- 【HDOJ】1706 The diameter of graph
这么个简单的题目居然没有人题解.floyd中计算数目,同时注意重边. /* 1706 */ #include <iostream> #include <string> #inc ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 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 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
随机推荐
- 使用ICSharpZipLib将文件夹压缩为zip文件
序言: 在我接触Git和SVN之前,我最常用的保存数据的办法就是把文件夹压缩成一个zip文件,添加上时间戳.下面是我在学习C#的文件操作之后做的一个练习,使用开源的ICSharpZipLib来 ...
- PhotoView开源项目剖析
http://blog.csdn.net/wu928320442/article/details/43056731 介绍 上一节呢,我们介绍了怎么下载和编译Android源码,这节呢,我们来讨论Pho ...
- Android TabHost的使用
标签显示界面的主要特点是可以在一个窗口中显示多组标签栏的类容. 在Android系统中,每个标签栏称为一个Tab,而包含多个标签栏的内容就称为TabHost. 通过TabHost的继承结构来看,Tab ...
- C#对于sql server数据库的简单操作
1.在用windows模式登陆sql server 数据库 简历一个student的数据库,然后新建查询: create table student ( id int auto_increment p ...
- 球面墨卡托(Spherical Mercator)
地理信息描述空间位置相关的信息,在空间位置的表达中,需要基于空间参照系来保证数据精度以及不同数据源之间的相互叠加/空间分析操作.自Google Maps与2005年发布以来,电子地图服务与普通民众的日 ...
- 页面嵌套 Iframe 产生缓存导致页面数据不刷新问题
最近遇到个比较古怪的问题:当页面嵌套多个 Iframe 时会出现 Iframe 里包含的页面无法看到最新的页面信息. 初步解决方案,在 Iframe 指向的页面地址后缀添加一个随机数或者时间戳.这样能 ...
- YYKit之YYText
原文:http://www.cnblogs.com/lujianwenance/p/5716804.html 本文的目的是希望能帮助到我们更快的熟悉和学习YYText的结构和实现的思路,如有不正确 ...
- javascript函数 第14节
<html> <head> <title>function</title> </head> <body> 1.函数形式<b ...
- OpenCV(7)-图像直方图
直方图定义可参考这里.图像的直方图用来表示图像像素的统计信息,它统计了图像每一个通道(如果是多通道)中,每个像素的个数(比例). 计算直方图 OpenCV提供了直接计算直方图的函数 void calc ...
- (转)IOS学习笔记-2015-03-29 int、long、long long取值范围
unsigned - - unsigned - - unsigned __int64的最大值: __int64的最小值:- unsigned __int64的最大值: