bzoj 2733 永無鄉 Treap+並查集

來源:互聯網
上載者:User
E - 永無鄉 Time Limit:10000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

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 7Q 3 2 Q 2 1 B 2 3 B 1 5 Q 2 1 Q 2 4 Q 2 3 

Sample Output

-12512


用Treap維護並詢問第K大,並查集維護在哪顆Treap上,合并時數量小的暴力加到數量大的中。

Tips:最好不要再結構體建構函式中初始化priority值,因為用了並查集維護,我們要保證根節點的priority最小,使得fa數組一定指向Treap的根。


/**************************************************************    Problem: 2733    User: cabinfever    Language: C++    Result: Accepted    Time:1964 ms    Memory:3636 kb****************************************************************/ #include <cstdio>#include <cstring>#include <cmath>#include <string>#include <cstdlib>#include <algorithm>#include <iostream> using namespace std; #define N 100010 int n,m,q;struct node{    int l,r,priority,size,key;    node()    {        size = 1;        priority = l = r = 0;    }}a[N];int fa[N]; int getfa(int x){    if(x == fa[x])        return x;    fa[x] = getfa(fa[x]);    return fa[x];} void rotate_right(int &x){    int temp = a[x].l;    a[x].l = a[temp].r;    a[temp].r = x;    a[temp].size = a[x].size;    a[x].size = a[a[x].l].size + 1 + a[a[x].r].size;    x = temp;} void rotate_left(int &x){    int temp = a[x].r;    a[x].r = a[temp].l;    a[temp].l = x;    a[temp].size = a[x].size;    a[x].size = a[a[x].l].size + 1 + a[a[x].r].size;    x = temp;} void insert(int &x,int t){    if(x)    {        a[x].size++;        if(a[t].key < a[x].key)        {            insert(a[x].l,t);            if(a[a[x].l].priority < a[x].priority)                rotate_right(x);        }        else        {            insert(a[x].r,t);            if(a[a[x].r].priority < a[x].priority)                rotate_left(x);        }    }    else    {        x = t;        a[t].priority = rand();    }} void merge_node(int x,int y){    if(!y)        return;    merge_node(x,a[y].l);    merge_node(x,a[y].r);    insert(x,y);} void merge(int u,int v){    int fa_u = getfa(u);    int fa_v = getfa(v);    if(fa_u == fa_v)        return;    if(a[fa_u].size < a[fa_v].size)        swap(fa_u,fa_v);    fa[fa_v] = fa_u;    merge_node(fa_u,fa_v);} int get_Kth(int x,int k){    if(k <= a[a[x].l].size)        return get_Kth(a[x].l,k);    k -= a[a[x].l].size + 1;    if(k == 0)        return x;    return get_Kth(a[x].r,k);} int main(){    a[0].size = 0;    cin >> n >> m;    for(int i = 1; i <= n; i++)        scanf("%d",&a[i].key);    for(int i = 1; i <= n; i++)        fa[i] = i;    for(int i = 1; i <= m; i++)    {        int u,v;        scanf("%d%d",&u,&v);        merge(u,v);    }    cin >> q;    char c;    for(int i = 1; i <= q; i++)    {        scanf(" %c",&c);        if(c == 'B')        {            int u,v;            scanf("%d%d",&u,&v);            merge(u,v);        }        else        {            int x,k;            scanf("%d%d",&x,&k);            x = getfa(x);            if(a[x].size < k)                printf("-1\n");            else                printf("%d\n",get_Kth(x,k));        }    }    return 0;}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.