zoukankan      html  css  js  c++  java
  • 【bzoj】2733: [HNOI2012]永无乡

    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

    第k大+合并,一眼就是BST合并啊,裸题练treap板子。
     
    当然也可以线段树合并,由于线段树具有可二分性,所以一个log就可以解决问题。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<algorithm>
      4 #include<vector>
      5 #include<cstdlib>
      6 #include<cmath>
      7 #include<cstring>
      8 using namespace std;
      9 #define maxn 100100
     10 #define llg long long 
     11 #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
     12 llg n,m,Q;
     13 char ch;
     14 
     15 struct TREAP
     16 {
     17     llg size;
     18     struct 
     19     {
     20         llg l,r,rnd,val,size,fa;
     21     }po[maxn];
     22 
     23     void update(llg x){po[x].size=po[po[x].l].size+po[po[x].r].size;}
     24     
     25     llg right_(llg x) 
     26     {
     27         llg t=po[x].l; 
     28         po[x].l=po[t].r; po[po[t].r].fa=x;
     29         po[t].r=x;  po[x].fa=t;
     30         po[t].size=po[x].size;
     31         update(x);
     32         return t;
     33     }
     34 
     35     llg left_(llg x)
     36     {
     37         llg t=po[x].r; 
     38         po[x].r=po[t].l;   po[po[t].l].fa=x;
     39         po[t].l=x;         po[x].fa=t;
     40         po[t].size=po[x].size;
     41         update(x);
     42         return t;
     43     }
     44 
     45     void init() 
     46     {
     47         size=0; memset(po,0,sizeof(po)); 
     48         for (llg i=1;i<=n;i++) scanf("%lld",&po[i].val),po[i].fa=i;
     49     }
     50 
     51     llg insert(llg x,llg wz,llg lx)
     52     {
     53         if (x==0)
     54         {
     55             po[wz].size=1; po[wz].fa=lx;
     56             return wz;
     57         }
     58         po[x].size++;
     59         if (po[x].val<po[wz].val)
     60         {
     61             po[x].r=insert(po[x].r,wz,x);
     62             if (po[po[x].r].rnd<po[x].rnd) x=left_(x);
     63         }
     64         else
     65         {
     66             po[x].l=insert(po[x].l,wz,x);
     67             if (po[po[x].l].rnd<po[x].rnd) x=right_(x);
     68         }
     69         po[x].fa=lx;
     70         return x;
     71     }
     72 
     73     llg find_root(llg x)
     74     {
     75         while (po[x].fa!=x) 
     76             x=po[x].fa;
     77         return x;
     78     }
     79     
     80     void dfs(llg x,llg y)
     81     {
     82         if (x==0) return ;
     83         dfs(po[x].l,y);
     84         dfs(po[x].r,y);
     85         insert(y,x,y);
     86     }
     87 
     88     void union_(llg x,llg y)
     89     {
     90         llg f1=find_root(x),f2=find_root(y);
     91         if (f1==f2) return ;
     92         if (po[f1].size<po[f2].size) swap(f1,f2);
     93         dfs(f2,f1);
     94     }
     95 
     96     llg rank(llg x,llg res)
     97     {
     98         if (res==po[po[x].l].size+1) return x;
     99         if (res<=po[po[x].l].size) return rank(po[x].l,res);
    100         else return rank(po[x].r,res-po[po[x].l].size-1);
    101     }
    102 }tree;
    103 
    104 int main()
    105 {
    106     yyj("a");
    107     cin>>n>>m;
    108     tree.init();
    109     for (llg i=1;i<=m;i++)
    110     {
    111         llg x,y;
    112         scanf("%lld%lld",&x,&y);
    113         tree.union_(x,y);
    114     }
    115     cin>>Q;
    116     while (Q--)
    117     {
    118         llg x,y;
    119         ch=getchar();
    120         while (ch!='Q' && ch!='B') ch=getchar();
    121         scanf("%lld%lld",&x,&y);
    122         if (ch=='Q')
    123         {             
    124             llg ans=tree.rank(tree.find_root(x),y);
    125             if (ans==0) ans=-1;
    126             printf("%lld
    ",ans);
    127         }
    128         else
    129         {
    130             tree.union_(x,y);
    131         }
    132     }
    133     return 0;
    134 }
    本文作者:xrdog 作者博客:http://www.cnblogs.com/Dragon-Light/ 转载请注明出处,侵权必究,保留最终解释权!
  • 相关阅读:
    Linux 下 MQ 的安装
    云计算的三种服务模式:IaaS,PaaS和SaaS
    Mac下安装Maven
    JDK Mac 安装
    Mac OS 终端利器 iTerm2
    单元测试用例规范
    boolean 属性的定义规范
    2019-12-9号 终于入职 阿里巴巴
    远程调试方法
    系统提测及上线规范(系统上线必读!)
  • 原文地址:https://www.cnblogs.com/Dragon-Light/p/6341251.html
Copyright © 2011-2022 走看看