bzoj2733: [HNOI2012] Yong-no Township

Source: Internet
Author: User

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
2Puzzle: Open the weight segment tree of n dynamic open points, then the line tree merging can be code:
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6 #defineMAXN 1000027 using namespacestd;8 Charch;9 BOOLOK;Ten voidReadint&x) { One      for(ok=0, Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') ok=1; A      for(x=0; isdigit (ch); x=x*Ten+ch-'0', ch=GetChar ()); -     if(OK) x=-x; - } the intN,M,Q,A,B,K,X,FA[MAXN],ROOT[MAXN]; - structseg{ -     intidx,son[maxn* -][2],cnt[maxn* -],id[maxn* -]; -     voidInsertint&k,intLintRintXintnum) { +         if(!k) k=++idx; -cnt[k]++; +         if(L==R) {id[k]=num;return;} A         intM= (l+r) >>1; at         if(x<=m) Insert (son[k][0],l,m,x,num); -         ElseInsert (son[k][1],m+1, r,x,num); -     } -     intMergeintAintb) { -         if(!a| |! breturnA +b; -cnt[a]+=Cnt[b]; inson[a][0]=merge (son[a][0],son[b][0]); -son[a][1]=merge (son[a][1],son[b][1]); to         returnA; +     } -     voidQueryintAintk) { the         if(cnt[a]<k) {Puts ("-1");return;} *         intL=1, r=n,m; $          while(l!=R) {Panax NotoginsengM= (l+r) >>1; -             if(cnt[son[a][0]]&GT;=K) a=son[a][0],r=m; the             Elsek-=cnt[son[a][0]],a=son[a][1],l=m+1; +         } Aprintf"%d\n", Id[a]); the     } + }t; - intFindintx) {returnfa[x]==x?fa[x]:fa[x]=find (Fa[x]);} $ voidMergeintAintb) { $     if(Find (a) ==find (b))return; -Root[find (a)]=T.merge (Root[find (a)],root[find (b)]); -Fa[find (b)]=find (a); the } - voidQueryintAintk) {T.query (Root[find (a)],k);}Wuyi intMain () { the read (n), read (m); -      for(intI=1; i<=n;i++) Read (x), T.insert (Root[i),1, n,x,i); Wu      for(intI=1; i<=n;i++) fa[i]=i; -      for(intI=1; i<=m;i++Read (a), read (b), merge (A, a); About      for(Read (q); q;q--){ $          for(; ch!='Q'&&ch!='B'; ch=GetChar ()); -         if(ch=='Q'Read (a), read (k), query (a,k); -         ElseRead (a), read (b), merge (A, a); -     } A     return 0; +}

bzoj2733: [HNOI2012] Yong-no Township

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.