not hard ... treap + heuristic merge + and check the set.
----------------------------------------------------------------------------------------
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream> #define REP (i, n) for (int i = 0; i < n; ++i)#define CLR (x, C) memset (x, C, sizeof (x))using namespace std;const int MAXN = 100000 + 5;int P[MAXN];struct node *null, *pt;struct Node {node* ch[2];Int V, s, R, X;node (int _v = 0, int _x = 0): V (_v), X (_x), S (1), R (rand ()) {ch[0] = ch[1] = null;}Inline int cmp (int _v) {if (v = = _v) return-1;return _v < v 0:1;}inline Void Maintain () {s = ch[0]->s + ch[1]->s + 1;}void* operator New (size_t) {return pt++;}}; node* ROOT[MAXN], N[MAXN *;void Rotate (node* &o, int d) {node* k = o->ch[d ^ 1];o->ch[d ^ 1] = k->ch[d];k->ch[d] = o;O->maintain (); K->maintain ();o = k;}int x, V;void Insert (node* &o) {if (o = = null) o = new Node (v, x);else {int d = v < o->v? 0:1;Insert (o->ch[d]);if (o->ch[d]->r > O->r) rotate (o, D ^ 1);}O->maintain ();}void Merge (node* &o, node* &o) {if (o = = null) return;Rep (i, 2) merge (O->ch[i], O);x = o->x; v = o->v;Insert (O);}int kth (node* o, int k) {int s = o->ch[0]->s;if (k = = s + 1) return o->x;return k <= s? kth (O->ch[0], K): KTH (o->ch[1], k-s-1);}int find (int x) {return x = = P[x]? x:p[x] = Find (P[x]);}void init (int n) {pt = N;null = new (node);Rep (i, n) root[p[i] = i] = null;null->s = 0;}void B (int u, int v) {int x = Find (u), y = find (v);if (x! = y) {if (Root[x]->s > Root[y]->s) swap (x, y);p[x] = y;merge (Root[x], root[y]);}}int query (int x, int k) {int X = find (x);if (k > Root[x]->s) return-1;return kth (Root[x], k);}int main () {int n, m;scanf ("%d%d", &n, &m);init (n);Rep (i, N) {x = i + 1;scanf ("%d", &v);Insert (Root[i]);}While (m--) {int u, v;scanf ("%d%d", &u, &v);B (u-1, V-1);}scanf ("%d", &m);While (m--) {int u, v;char op;scanf ("%c%d%d", &op, &u, &v);if (op = = ' B ') b (u-1, V-1);Else printf ("%d\n", query (U-1, v));}return 0;}
----------------------------------------------------------------------------------------
2733: [HNOI2012] Yong No Xiang time limit: Sec Memory Limit: ten MB
Submit: 1455 Solved: 771
[Submit] [Status] [Discuss]
Description
Yong No township contains N Island, numbering from 1 to N, each island has its own unique importance, according to the importance of this can be ranked in the N Island, ranking with 1 to N to represent. Some of the islands are connected by huge bridges that can be reached from one island to another via a bridge. If a number of blocks (including 0-seater) can reach Island B from Island A, it is said that island A and island B are connected. There are now two operations: B x y to construct a bridge between Island X and Island Y. Q x K indicates which island is important in all the islands that are currently connected to Island X, which is the island of importance in all islands connected with island X, please output the number of that island.
Input
The first line of the input file is two positive integers, N and M, separated by a space, representing the number of islands and the number of bridges that existed at the beginning. The next line is the number of n separated by spaces, which in turn describes the importance ranking from Island 1 to Island N. Each row of the subsequent m line is a two positive integer AI and bi separated by a space, indicating that there was a bridge connecting island AI and island bi at the outset. The remainder of the following describes the operation, the first line of the section is a positive integer q, indicating that there is a total of Q operations, the next Q line describes each operation, the format of the operation as described above, starting with the capital letter Q or B, followed by two not more than n positive integers, letters and numbers and two numbers separated by a space. For 20% of data n≤1000,q≤1000
For 100% of data n≤100000,m≤n,q≤300000
Output
For each Q x K operation, one line is output, which contains an integer that represents the number of the island being queried. If the island does not exist, then output-1.
Sample Input5 1
4 3 2) 5 1
1 2
7
Q 3 2
Q 2 1
B 2 3
B 1 5
Q 2 1
Q 2 4
Q 2 3
Sample Output-1
2
5
1
2
HINT
Source
Bzoj 2733: [HNOI2012] Yong-no township (treap + heuristic merge + and search set)