bzoj2733: [HNOI2012]永无乡(splay)

2733: [HNOI2012]永无乡

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 3778  Solved: 2020

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
 

分析

可以用并查集维护连通性,这个没啥好说的。

然后 Splay 启发式合并,合并时,直接将小的暴力加入另一个。

这个调试了好久qwq。洛谷上还t了最后一个点

code

  1 #include<cstdio>
  2 #include<algorithm>
  3 
  4 using namespace std;
  5 
  6 const int MAXN = 100100;
  7 int fa[MAXN],pa[MAXN],val[MAXN],ch[MAXN][2],siz[MAXN],q[MAXN];
  8 int n,m;
  9 char opt[5];
 10 
 11 int read()
 12 {
 13     int x = 0, f = 1;char ch = getchar();
 14     while (ch<'0'||ch>'9') {if(ch=='-')f=-1; ch=getchar(); }
 15     while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar(); }
 16     return x*f;
 17 }
 18 void pushup(int x)
 19 {
 20     if (!x) return ;
 21     siz[x] = siz[ch[x][0]]+siz[ch[x][1]]+1;
 22 }
 23 int son(int x)
 24 {
 25     return ch[fa[x]][1]==x;//不是等于1,qwq
 26 }
 27 void rotate(int x)
 28 {
 29     int y = fa[x],z = fa[y],b = son(x),c = son(y),a = ch[x][!b];
 30     if (z) ch[z][c] = x;fa[x] = z;
 31     if (a) fa[a] = y;ch[y][b] = a;
 32     ch[x][!b] = y;fa[y] = x;
 33     pushup(y);
 34     
 35 } 
 36 void splay(int x,int rt)
 37 {
 38     while (fa[x]!=rt)
 39     {
 40         int y = fa[x],z = fa[y];
 41         if (z==rt) rotate(x);
 42         else
 43         {
 44             if (son(x)==son(y)) rotate(y),rotate(x);
 45             else rotate(x), rotate(x);
 46         }
 47     }
 48     pushup(x);
 49 }
 50 void insert(int &x,int pre,int id)
 51 {
 52     if (!x)
 53     {
 54         x = id;fa[x] = pre;siz[x] = 1;
 55         splay(x,0);
 56         return ;
 57     }
 58     if (val[id]<=val[x]) insert(ch[x][0],x,id);
 59     else insert(ch[x][1],x,id);
 60     pushup(x);
 61 }
 62 int getkth(int x,int k)
 63 {
 64     if (k<0 || k>siz[x]) return -1;
 65     while (k<=siz[ch[x][0]] || k>siz[ch[x][0]]+1)//x是变的,不能直接l = ch[x][0],用l代替chp[x][0]使用 
 66         if (k<=siz[ch[x][0]]) x = ch[x][0];
 67         else k -= siz[ch[x][0]]+1, x = ch[x][1];;
 68     splay(x,0);
 69     return x;
 70 }
 71 void merge(int x,int y)
 72 {
 73     splay(x,0);splay(y,0);
 74     if (siz[x]>siz[y]) swap(x,y);//启发式合并
 75     int head = 0, tail = 1;
 76     q[0] = y,q[1] = x;
 77     while (head<tail)
 78     {
 79         int t = q[++head];
 80         if (ch[t][0]) q[++tail] = ch[t][0];
 81         if (ch[t][1]) q[++tail] = ch[t][1];
 82         ch[t][0] = ch[t][1] = 0;
 83         insert(q[head-1],0,t);
 84     }
 85 }
 86 int find(int x)
 87 {
 88     return pa[x]==x?x:pa[x]=find(pa[x]);
 89 }
 90 int main()
 91 {
 92     n = read(),m = read();
 93     for (int i=1; i<=n; ++i)
 94     {
 95         val[i] = read();
 96         pa[i] = i;siz[i] = 1;
 97     }
 98     for (int x,y,i=1; i<=m; ++i)
 99     {
100         x = read();y = read();
101         if (find(x)!=find(y))
102         {
103             merge(x,y);
104             pa[find(x)] = find(y);//设为find(y),不是y 
105         }
106     }
107     int t = read();
108     while (t--)
109     {
110         scanf("%s",opt);
111         int x = read(), y = read();
112         if (opt[0]=='Q')
113         {
114             splay(x,0);
115             printf("%d\n",getkth(x,y));
116         }
117         else 
118         {
119             if (find(x)!=find(y))
120             {
121                 merge(x,y);
122                 pa[find(x)] = find(y);
123             }
124         }    
125     }
126     return 0;
127 }

 

posted @ 2017-08-12 19:35  MJT12044  阅读(215)  评论(0编辑  收藏  举报