BZOJ-2733 永无乡 合并线段树 并查集
2017-07-03 18:52
316 查看
大家都很强, 可与之共勉 。
733: [HNOI2012]永无乡Time Limit: 10 Sec Memory Limit: 128 MB
Description
永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示。某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛。如果从岛 a 出发经过若干座(含 0 座)桥可以到达岛 b,则称岛 a 和岛 b 是连 通的。现在有两种操作:B x y 表示在岛 x 与岛 y 之间修建一座新桥。Q x k 表示询问当前与岛 x连通的所有岛中第 k 重要的是哪座岛,即所有与岛 x 连通的岛中重要度排名第 k 小的岛是哪 座,请你输出那个岛的编号。
Input
输入文件第一行是用空格隔开的两个正整数 n 和 m,分别 表示岛的个数以及一开始存在的桥数。接下来的一行是用空格隔开的 n 个数,依次描述从岛 1 到岛 n 的重要度排名。随后的 m 行每行是用空格隔开的两个正整数 ai 和 bi,表示一开始就存 在一座连接岛 ai 和岛 bi 的桥。后面剩下的部分描述操作,该部分的第一行是一个正整数 q, 表示一共有 q 个操作,接下来的 q 行依次描述每个操作,操作的格式如上所述,以大写字母 Q 或B 开始,后面跟两个不超过 n 的正整数,字母与数字以及两个数字之间用空格隔开。 对于 20%的数据 n≤1000,q≤1000
对于 100%的数据 n≤100000,m≤n,q≤300000
Output
对于每个 Q x k 操作都要依次输出一行,其中包含一个整数,表 示所询问岛屿的编号。如果该岛屿不存在,则输出-1。
Sample Input
5 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
这道题MLE的提示是TLE啊,于是开始0ms, 0kb T了 。
不知道数据的情况下建议用 new并查集维护联通块。
/************************************************************** Problem: 2733 User: Lazer2001 Language: C++ Result: Accepted Time:1340 ms Memory:119648 kb ****************************************************************/ # include <cctype> # include <cstdio> # define By_Lazer int main ( ) { return 0 ; } const int N = 100005 ; struct IO { char buf [1 << 16], *s, *t, ch ; inline char pick ( ) { return ( s == t ) ? ( t = buf + fread ( s = buf, 1, 1 << 16, stdin ), *s ++ ) : ( *s ++ ) ; } int x ; inline operator int ( ) { while ( ! isdigit ( ch = pick ( ) ) ) ; for ( x = -48 + ch ; isdigit ( ch = pick ( ) ) ; x = x * 10 + ch - 48 ) ; return x ; } } Read ; class UFS { public : int fa ; inline void Init ( int n ) { for ( register int i = 1 ; i <= n ; ++ i ) fa [i] = i ; } inline int find ( int x ) { while ( x ^ fa [x] ) x = fa [x] = fa [fa [x]] ; return x ; } } Ts ; int n, m, q ; int rank , seq ; struct node { int cnt ; node *ls, *rs ; inline void update ( ) { cnt = ls -> cnt + rs -> cnt ; } } pool [N * 100], *root , *tail = pool, *null ; inline node* newnode ( ) { node* nd = ++ tail ; nd -> ls = nd -> rs = null ; nd -> cnt = 0 ; return nd ; } inline void Modify ( node* &nd, int l, int r, int pos, int delta ) { if ( nd == null ) nd = newnode ( ) ; if ( l == r ) { nd -> cnt += delta ; return ; } int mid = ( l >> 1 ) + ( r >> 1 ) + ( l & r & 1 ) ; ( pos <= mid ) ? Modify ( nd -> ls, l, mid, pos, delta ) : Modify ( nd -> rs, mid + 1, r, pos, delta ) ; nd -> update ( ) ; } inline int Query ( node* &nd, int l, int r, int k ) { if ( l == r ) return l ; int mid = ( l >> 1 ) + ( r >> 1 ) + ( l & r & 1 ) ; int cnt = nd -> ls -> cnt ; return ( cnt >= k ) ? Query ( nd -> ls, l, mid, k ) : Query ( nd -> rs, mid + 1, r, k - cnt ) ; } inline node* Merge ( node* &x, node* &y ) { if ( x == null ) return y ; if ( y == null ) return x ; x -> ls = Merge ( x -> ls, y -> ls ) ; x -> rs = Merge ( x -> rs, y -> rs ) ; x -> update ( ) ; return x ; } class Main { public : Main ( ) { n = Read, m = Read ; Ts.Init ( n ) ; null = tail ; null -> ls = null -> rs = null ; null -> cnt = 0 ; for ( register int i = 1 ; i <= n ; ++ i ) rank [i] = Read ; for ( register int i = 1 ; i <= n ; ++ i ) seq [rank [i]] = i ; for ( register int i = 1 ; i <= n ; ++ i ) root [i] = newnode ( ) ; while ( m -- ) { int u = Read, v = Read ; u = Ts.find ( u ), v = Ts.find ( v ) ; if ( u ^ v ) Ts.fa [u] = v ; } for ( register int i = 1 ; i <= n ; ++ i ) Modify ( root [Ts.find ( i )], 1, n, rank [i], +1 ) ; q = Read ; while ( q -- ) { static char opt ; while ( ( opt = Read.pick ( ) ) == ' ' || opt == '\n' ) ; if ( opt == 'B' ) { int u = Read, v = Read ; u = Ts.find ( u ), v = Ts.find ( v ) ; if ( u ^ v ) Ts.fa [u] = v ; else continue ; root [v] = Merge ( root [u], root [v] ) ; // must be root [v] ; } else { int u = Read, k = Read ; int anc = Ts.find ( u ) ; // printf ( "ans -> cnt = %d\n", root [anc] -> cnt ) ; if ( root [anc] -> cnt < k ) { puts ( "-1" ) ; continue ; } printf ( "%d\n", seq [Query ( root [anc], 1, n, k )] ) ; } } } } Z ; By_Lazer
相关文章推荐
- BZOJ 2733 永无乡 [线段树合并]
- BZOJ 2733 [HNOI2012]永无乡 可持久化线段树合并
- BZOJ[2733][HNOI2012]永无乡 线段树合并+并查集
- 【并查集套可持久化线段树】【bzoj 2733】: [HNOI2012]永无乡
- [线段树 启发式合并] BZOJ 2733 永无乡
- BZOJ 2733 [HNOI2012]永无乡 ——线段树 并查集
- bzoj 2733: [HNOI2012]永无乡(线段树启发式合并)
- bzoj 2733 永无乡 - 并查集 - 线段树
- bzoj 2733 永无乡 线段树启发式合并
- BZOJ 2733 线段树的合并 并查集
- BZOJ 2733 线段树的合并 并查集
- bzoj 2733: [HNOI2012]永无乡(线段树启发式合并)
- bzoj 2733: [HNOI2012]永无乡 (线段树合并)[省选计划系列]
- [BZOJ] 2733: [HNOI2012]永无乡 #线段树合并+并查集
- BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)
- BZOJ 2733 [HNOI2012] 永无乡 [splay+启发式合并做法]
- [并查集+启发式合并]BZOJ 2733——[HNOI2012]永无乡
- BZOJ 2733: [HNOI2012]永无乡 (Treap+启发式合并)
- [bzoj2733][HNOI2012]永无乡 线段树合并+并查集
- bzoj 2733 [HNOI2012]永无乡 splay启发式合并