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的更多相关文章

  1. 【HDOJ】3560 Graph’s Cycle Component

    并查集的路径压缩. #include <stdio.h> #include <string.h> #define MAXNUM 100005 int deg[MAXNUM], ...

  2. HDU 3726 Graph and Queries treap树

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

  3. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...

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

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

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

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

  6. 【HDOJ】1706 The diameter of graph

    这么个简单的题目居然没有人题解.floyd中计算数目,同时注意重边. /* 1706 */ #include <iostream> #include <string> #inc ...

  7. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

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

  9. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

随机推荐

  1. CAS 单点登录,通过ticket 获取登录用户

    string url =SSOValidate+"?service=" + WebValidate + "&ticket=" + Ticket + &q ...

  2. CSS的!important修改权重

    !important语法和描述 !important为开发者提供了一个增加样式权重的方法.应当注意的是!important是对整条样式的声明,包括这个样式的属性和属性值. #example { fon ...

  3. zoj2432 hdoj1423 最长公共上升子序列(LCIS)

    zoj2431  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2432 hdoj 1423 http://acm.hdu. ...

  4. 第11条:理解objc_msgSend的作用

    C语言使用“静态绑定”,也就是说,在编译期就能决定运行时所应调用的函数(也就是说函数地址硬编码在指令之中). 如果是内联函数,就无法硬编码在指令之中,而是要在运行期读取出来(也就是动态绑定). 在底层 ...

  5. 什么是NSTimer

    本文主要是介绍什么是NSTimer,具体使用请参考上一篇博客. 1.什么是NSTimer? NSTimer就是timer就是一个能在从现在开始的后面的某一个时刻或者周期性的执行我们指定的方法的对象. ...

  6. 最近在学习UDP方面的通信,找到一个很棒的博客

    http://blog.csdn.net/kesalin/article/details/8798039

  7. Get AD user 的三种方法

    一. 通过AccountManagement 程序集(System.DirectoryServices.AccountManagement) acountManagement 包含有: 1. User ...

  8. linux进程与端口查看命令

    查看程序对应进程号:ps –ef|grep 进程名 查看进程号所占用的端口号:netstat –nltp|grep 进程号 使用lsof命令: lsof –i:端口号

  9. (转)Libevent(1)— 简介、编译、配置

    转自:http://name5566.com/4190.html 参考文献列表:http://www.wangafu.net/~nickm/libevent-book/ 此文编写的时候,使用到的 Li ...

  10. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...