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. LSJ_NHibernate第一章 NHibernate介绍

    前言: 说起NHibernate网上资料真不少,但用的人却并不多,说起java的SSH框架大家可能就知道了,这里的H指的就是Hibernate,一款好用的ORM框架,在.net重写了这款好用的框架,名 ...

  2. 最全的ASP.NET开源CMS汇总

    转载:http://www.cnblogs.com/cxd4321/archive/2011/11/16/2250707.html 国内: 1.SiteServer CMS SiteServer CM ...

  3. 尚学堂马士兵Oracle教程笔记

    检查Oracle安装 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba 然后,解除对scott用户的锁 alter user scott account ...

  4. String类与Date类的转换

    public class DateTest { public static void main(String[] args) throws ParseException { Date date = n ...

  5. java之泛型潜在错误

    如果使用带泛型声明的类时,没有传入类型参数,那么这个类型参数默认是声明该参数时指定的第一个上限类型,这个类型参数被称为raw type(原始类型 ). eg:     public class Lis ...

  6. UVA 10881 Piotr's Ants(等效变换 sort结构体排序)

    Piotr's AntsTime Limit: 2 seconds Piotr likes playing with ants. He has n of them on a horizontal po ...

  7. 文件(夹)比较 Beyond Compare, Diff

    文件(夹)比较 Beyond Compare, Diff, UltraCompare 1.Beyond Compare(无与伦比) 2.Diff 参考 1.diff详解

  8. Java LoggingAPI 使用方法

    因为不想导入Log4j的jar,项目只是测试一些东西,因此选用了JDK 自带的Logging,这对于一些小的项目或者自己测试一些东西是比较好的选择. Log4j中是通过log4j.properties ...

  9. c语言实现交换两个数的值

    C语言中要实现交换两个数的值,可以有很多种方法,具体如下所述. 不使用中间变量: // 异或, a^=b^=a^=b; a ^= b; b ^= a; a ^= b; // 加减 a = a + b; ...

  10. 关于C# Winform 程序开机自动启动

    1.程序运行时调用下面方法即可. /// <summary> /// 设置开机自动启用 /// </summary> private void SetAutoStart() { ...