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. WebUploader——一个页面多个实例上传图片

    WebUploader官方例子看的不是很清楚,自己也是费了点劲自己写了一下. 需求:一个单页需要多个实例来上传,一次可上传多张 条件:后台接收C# 首先:引入webuploader     webup ...

  2. Js中的appenChild,insertBefore--createDocumentFragment

    平时项目中会有一些流程,或者是评论相关的东西,这些一般只会是在页面初次加载一部分,剩余部分搞一个更多的标签,当点击更多的时候,ajax请求把所有数据加载完(当然这里也有分页的实现方法,本篇不作讨论), ...

  3. 手动修复OneDrive的DNS污染屏蔽的方法

    随着云计算的发展和微软云战略的持续推进,使用网盘进行文档存储.协同编辑与共享已成为文档操作的新流程.而Office.Office 365和OneDrive等微软产品是Windows用户的首选.但由于国 ...

  4. PB中用回车键实现tab键的功能

    先编辑控件的TabOrder顺序,然后在 global external functions 中定义一个API:Subroutine keybd_event(int bVk,int bScan,ulo ...

  5. jQuery 常用代码集锦

    1. 选择或者不选页面上全部复选框 var tog = false; // or true if they are checked on load $('a').click(function() { ...

  6. 现代密码学应用的范例-PGP

    PGP(Pretty Good Privacy),是一个基于RSA公钥加密体系的邮件加密软件. 产生背景: 电子邮件在传输中使用SMTP协议存在这样的问题 1.无法保证邮件在传输过程中不被人偷看 2. ...

  7. Linux Master/Baremetal Remote 配置下的裸机调试

    为了实现在ZC702开发板上的两颗Cortex-A9处理器上实现Linux Master/Baremetal Remote 配置,并对Remote端的裸机程序进行调试,需要注意的几点如下: 一.建立p ...

  8. HTTP_USER_AGENT

    <!DOCTYPE html><html><head><meta charset="UTF-8" /><title>We ...

  9. 忘记mysql的root密码

    如果忘记root密码或其他用户密码,不要急,按下面操作即可.1. 编辑mysql主配置文件 my.cnfvim /etc/my.cnf   在[mysqld]字段下添加参数  skip-grant   ...

  10. Extjs发票管理系统

    技术特点:Extjs框架,三层架构,Ajax,json 1.仿office2007菜单.介面美观大方,可动态更改皮肤保存至cookie. 2,json数据源与实体类的相互转换. 3.可下载桌面版登录方 ...