Description
Input
Output
1 #include <cstdlib> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int MAXC = 500010; 8 const int MAXN = 20010; 9 const int MAXM = 60010; 10 11 int key[MAXN], weight[MAXN], child[MAXN][2], size[MAXN]; 12 int stk[MAXN], top, poi_cnt;//not use point 13 14 inline int newNode(int k) { 15 int x = (top ? stk[top--] : ++poi_cnt); 16 key[x] = k; 17 size[x] = 1; 18 weight[x] = rand(); 19 child[x][0] = child[x][1] = 0; 20 return x; 21 } 22 23 inline void update(int &x) { 24 size[x] = size[child[x][0]] + size[child[x][1]] + 1;//size[0]=0 25 } 26 27 inline void rotate(int &x, int t) { 28 int y = child[x][t]; 29 child[x][t] = child[y][t ^ 1]; 30 child[y][t ^ 1] = x; 31 update(x); update(y); 32 x = y; 33 } 34 35 void insert(int &x, int k) { 36 if (x == 0) x = newNode(k); 37 else { 38 int t = (key[x] < k); 39 insert(child[x][t], k); 40 if (weight[child[x][t]] < weight[x]) rotate(x, t); 41 } 42 update(x); 43 } 44 45 void remove(int &x, int k) { 46 if(key[x] == k) { 47 if(child[x][0] && child[x][1]) { 48 int t = weight[child[x][0]] < weight[child[x][1]]; 49 rotate(x, t); remove(child[x][t ^ 1], k); 50 } 51 else { 52 stk[++top] = x; 53 x = child[x][0] + child[x][1]; 54 } 55 } 56 else remove(child[x][key[x] < k], k); 57 if(x > 0) update(x); 58 } 59 60 struct Command { 61 char type; 62 int x, p; 63 } commands[MAXC]; 64 65 int n, m, value[MAXN], from[MAXM], to[MAXM], removed[MAXM]; 66 67 int fa[MAXN]; 68 int getfather(int x) { 69 if(fa[x] == x) return x; 70 else return fa[x] = getfather(fa[x]); 71 } 72 73 int root[MAXN]; 74 75 void mergeto(int &x, int &y) { 76 if(child[x][0]) mergeto(child[x][0], y); 77 if(child[x][1]) mergeto(child[x][1], y); 78 insert(y, key[x]); 79 stk[++top] = x; 80 } 81 82 inline void addEdge(int x) { 83 int u = getfather(from[x]), v = getfather(to[x]); 84 if(u != v) { 85 if(size[root[u]] > size[root[v]]) swap(u, v); 86 fa[u] = v; mergeto(root[u], root[v]); 87 } 88 } 89 90 int kth(int &x, int k) { 91 if(x == 0 || k <= 0 || k > size[x]) return 0; 92 int s = 0; 93 if(child[x][1]) s = size[child[x][1]]; 94 if(k == s + 1) return key[x]; 95 if(k <= s) return kth(child[x][1], k); 96 return kth(child[x][0], k - s - 1); 97 } 98 99 int query_cnt; 100 long long query_tot; 101 102 void query(int x, int k) { 103 ++query_cnt; 104 query_tot += kth(root[getfather(x)], k); 105 } 106 107 inline void change_value(int x, int v) { 108 int u = getfather(x); 109 remove(root[u], value[x]); 110 insert(root[u], value[x] = v); 111 } 112 113 int main() { 114 int kase = 0; 115 size[0] = 0; 116 while(scanf("%d%d", &n, &m) != EOF && n) { 117 for(int i = 1; i <= n; ++i) scanf("%d", &value[i]); 118 for(int i = 1; i <= m; ++i) scanf("%d%d", &from[i], &to[i]); 119 memset(removed, 0, sizeof(removed)); 120 121 int c = 0; 122 while(true) { 123 char type; 124 int x, p = 0, v = 0; 125 scanf(" %c", &type); 126 if(type == 'E') break; 127 scanf("%d", &x); 128 if(type == 'D') removed[x] = 1; 129 if(type == 'Q') scanf("%d", &p); 130 if(type == 'C') { 131 scanf("%d", &v); 132 p = value[x]; 133 value[x] = v; 134 } 135 commands[c++] = (Command) {type, x, p}; 136 } 137 138 top = poi_cnt = 0; 139 for(int i = 1; i <= n; ++i) { 140 fa[i] = i; 141 root[i] = newNode(value[i]); 142 } 143 for(int i = 1; i <= m; ++i) if(!removed[i]) addEdge(i); 144 145 query_tot = query_cnt = 0; 146 for(int i = c - 1; i >= 0; --i) { 147 if(commands[i].type == 'D') addEdge(commands[i].x); 148 if(commands[i].type == 'Q') query(commands[i].x, commands[i].p); 149 if(commands[i].type == 'C') change_value(commands[i].x, commands[i].p); 150 } 151 printf("Case %d: %.6f\n", ++kase, query_tot/(double)query_cnt); 152 } 153 return 0; 154 }