hdu 3726 (数据结构综合(好题))

来源:互联网 发布:ipad淘宝店怎么上货 编辑:程序博客网 时间:2024/04/27 21:37

题目链接

LRJ白书上的例题, 最近复习数据结构时又A了一遍这题, 此题很经典同时考察了数据结构中的离线,平衡树, 并查集, 

启发式合并等多个常用技巧, 其中离线将所有操作反向处理值得回味, 详细讲解可以看白书。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cstdlib>using namespace std;inline int read() {//used to read 32bit positive integerchar c = getchar();while (!isdigit(c)) c = getchar();int x = 0;while (isdigit(c)) {x = x * 10 + c - '0';c = getchar();}return x;}const int N = 20005 << 4;const int M = 60015;const int C = 500015;int typ[C], V[C], arg[C], root[N], fa[N], W[N], done[M];int r[N], v[N], ch[N][2], sz[N];int it, n, m;typedef pair<int, int> pi;#define fi first#define se secondpi edge[M];void init() {it = 1;memset(done, 0, sizeof(done));memset(root, 0, sizeof(root));for (int i = 1; i <= n; i++)fa[i] = i;}#define lch ch[rt][0]#define rch ch[rt][1]inline void push_up(int rt) {sz[rt] = sz[lch] + sz[rch] + 1;}inline int cmp(int rt, int x) {if (v[rt] == x) return -1;else if (x < v[rt])return 0;elsereturn 1;}void rotate(int& rt, int d) {        int t = ch[rt][d ^ 1];        ch[rt][d ^ 1] = ch[t][d];        ch[t][d] = rt;        push_up(rt);        push_up(t);        rt = t;}void insert(int& rt, int x, bool flag) { //falg == 0 x is a number otherwise...if (!rt) {if (!flag) {rt = it++;v[rt] = x;r[rt] = rand();lch = rch = 0;sz[rt] = 1;}else {rt = x;sz[rt] = 1;}}else {int d = ((flag ? v[x] : x) < v[rt] ? 0 : 1);insert(ch[rt][d], x, flag);if (r[ch[rt][d]] > r[rt]) rotate(rt, d ^ 1);}push_up(rt);}int kth(int rt, int k) {if (!rt || k <= 0 || k > sz[rt]) return 0;int  t = sz[rch] + 1;if (k == t)return v[rt];else if (k < t) return kth(rch, k);elsereturn kth(lch, k - t);}void remove(int& rt, int x) {int d = cmp(rt, x);if (d == -1) {if (lch && rch) {int d2 = r[lch] > r[rch] ? 0 : 1;rotate(rt, d2 ^ 1);remove(ch[rt][d2 ^ 1], x);push_up(rt);}else {rt = lch ? lch : rch;}}else {remove(ch[rt][d], x);push_up(rt);}}int pred(int rt, int x) {if (!rt) return x;else if (x > v[rt]) {int res = pred(rch, x);if (res == x) return v[rt];return res;}elsereturn pred(lch, x);}int succ(int rt, int x) {if (!rt) return x;else if (x < v[rt]) {int res = succ(lch, x);if (res == x) return v[rt];return res;}elsereturn succ(rch, x);}void mergeto(int ot, int& rt) {if (!ot) return;mergeto(ch[ot][0], rt);mergeto(ch[ot][1], rt);ch[ot][0] = ch[ot][1] = 0;insert(rt, ot, 1);}int find(int u) {return fa[u] == u ? u : fa[u] = find(fa[u]);}void connect(int u, int v) {int fu = find(u), fv = find(v);if (fu != fv) {if (sz[root[fu]] < sz[root[fv]])mergeto(root[fu], root[fv]), fa[fu] = fv;elsemergeto(root[fv], root[fu]), fa[fv] = fu;}}char ss[100];int main() {int x, u, id, cas = 1;while (scanf("%d%d", &n, &m), n + m) {init();for (int i = 1; i <= n; i++)scanf("%d", W + i);for (int i = 1; i <= m; i++)scanf("%d%d", &edge[i].fi, &edge[i].se);int tot = 0, cnt = 0;while (1) {scanf("%s", ss);if (ss[0] == 'E') break;else if (ss[0] == 'D') {scanf("%d", &id);typ[tot] = 1;arg[tot++] = id;done[id] = 1;}else if (ss[0] == 'C') {scanf("%d%d", &u, &x);typ[tot] = 2, V[tot] = u;arg[tot] = W[u];tot++;W[u] = x;}else {cnt++;scanf("%d%d", &u, &x);typ[tot] = 3, V[tot] = u, arg[tot] = x;tot++;}}for (int i = 1; i <= n; i++) {insert(root[i], W[i], 0);}for (int i = 1; i <= m; i++)if (!done[i])connect(edge[i].fi, edge[i].se);double res = 0;for (int i = tot - 1; i >= 0; i--) {if (typ[i] == 3) {res += kth(root[find(V[i])], arg[i]);}else if (typ[i] == 2) {int t = find(V[i]);if (W[V[i]] == arg[i]) continue;remove(root[t], W[V[i]]);insert(root[t], arg[i], 0);W[V[i]] = arg[i];}else {connect(edge[arg[i]].fi, edge[arg[i]].se);}}printf("Case %d: %.6lf\n", cas++, res / cnt);}return 0;}