zoukankan      html  css  js  c++  java
  • BZOJ2733 [HNOI2012] 永无乡

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2733

    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。 

    Treap启发式合并,对每一个点建一棵Treap,连通性用并查集维护。所谓启发式合并就是把小的那棵暴力insert进大的那棵,如此而已……

    调了很久很久才发现少了两个引用!我勒个去……

    感谢教主提供的数据!

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #define rep(i,l,r) for(int i=l; i<=r; i++)
     6 #define clr(x,y) memset(x,y,sizeof(x))
     7 #define travel(x) for(Edge *p=last[x]; p; p=p->pre)
     8 using namespace std;
     9 const int INF = 0x3f3f3f3f;
    10 const int maxn = 100010;
    11 inline int read(){
    12     int ans = 0, f = 1;
    13     char c = getchar();
    14     for(; !isdigit(c); c = getchar())
    15     if (c == '-') f = -1;
    16     for(; isdigit(c); c = getchar())
    17     ans = ans * 10 + c - '0';
    18     return ans * f;
    19 }
    20 int n,m,q,x,y,fa[maxn];
    21 char ch[10];
    22 struct Node{
    23     Node *ch[2]; int s,v,r,n;
    24     inline void maintain(){
    25         s = ch[0]->s + ch[1]->s + 1;
    26     }
    27 }treap[250010];
    28 Node *pt = treap, *null = pt++, *root[maxn];
    29 inline Node *newnode(int x,int y){
    30     pt->s = 1; pt->v = x; pt->n = y; pt->r = rand();
    31     pt->ch[0] = pt->ch[1] = null;
    32     return pt++;
    33 }
    34 inline void rotate(Node *&o,int d){
    35     Node *k = o->ch[d^1]; o->ch[d^1] = k->ch[d]; k->ch[d] = o;
    36     o->maintain(); k->maintain(); o = k;
    37 }
    38 void ins(int x,int y,Node *&p){
    39     if (p == null) p = newnode(x,y); 
    40     else{
    41         int d = x > p->v;
    42         ins(x,y,p->ch[d]);
    43         if (p->ch[d]->r < p->r) rotate(p,d^1);
    44     }
    45     p->maintain();
    46 }
    47 int select(int x,Node *p){
    48     for(; ;){
    49         int t = p->ch[0]->s;
    50         if (x == t + 1) return p->n;
    51         else if (x <= t) p = p->ch[0];
    52         else x -= t + 1, p = p->ch[1];
    53     }
    54 }
    55 void merge(Node *&x,Node *&y){
    56     if (x == null) return;
    57     merge(x->ch[0],y); merge(x->ch[1],y);
    58     ins(x->v,x->n,y);
    59 }
    60 int getfa(int x){
    61     return x == fa[x] ? x : fa[x] = getfa(fa[x]);
    62 }
    63 void join(int x,int y){
    64     int a = getfa(x), b = getfa(y);
    65     if (a == b) return;
    66     if (root[a]->s > root[b]->s) swap(a,b);
    67     fa[a] = b;
    68     merge(root[a],root[b]);
    69 }
    70 int query(int x,int y){
    71     int a = getfa(x);
    72     if (y > root[a]->s) return -1;
    73     return select(y,root[a]);
    74 }
    75 int main(){
    76     n = read(); m = read(); null->s = 0;
    77     rep(i,1,n) root[i] = null; rep(i,1,n) fa[i] = i;
    78     rep(i,1,n){
    79         x = read(); ins(x,i,root[i]);
    80     }
    81     rep(i,1,m){
    82         x = read(); y = read();
    83         join(x,y);
    84     }
    85     q = read();
    86     rep(i,1,q){
    87         scanf("%s",ch); x = read(); y = read();
    88         if (ch[0] == 'B') join(x,y);
    89         else printf("%d
    ",query(x,y));
    90     }
    91     return 0;
    92 }
    View Code
  • 相关阅读:
    算法笔记codeup-Contest100000568
    算法笔记codeup-Contest100000567
    算法笔记codeup-Contest100000566
    人工智能各种知识点(大杂烩)
    人工智能基础知识复习:机器学习
    人工智能基础知识复习:神经计算 演化计算 模糊计算
    人工智能基础知识复习:问题求解与搜索
    人工智能基础知识复习:推理技术
    前端基础知识学习:概念篇
    MYSQL
  • 原文地址:https://www.cnblogs.com/jimzeng/p/bzoj2733.html
Copyright © 2011-2022 走看看