BZOJ2733 [HNOI2012] 永无乡
2016-01-21 20:53
253 查看
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2733
对于 100%的数据 n≤100000,m≤n,q≤300000
Treap启发式合并,对每一个点建一棵Treap,连通性用并查集维护。所谓启发式合并就是把小的那棵暴力insert进大的那棵,如此而已……
调了很久很久才发现少了两个引用!我勒个去……
感谢教主提供的数据!
View Code
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进大的那棵,如此而已……
调了很久很久才发现少了两个引用!我勒个去……
感谢教主提供的数据!
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define rep(i,l,r) for(int i=l; i<=r; i++) #define clr(x,y) memset(x,y,sizeof(x)) #define travel(x) for(Edge *p=last[x]; p; p=p->pre) using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 100010; inline int read(){ int ans = 0, f = 1; char c = getchar(); for(; !isdigit(c); c = getchar()) if (c == '-') f = -1; for(; isdigit(c); c = getchar()) ans = ans * 10 + c - '0'; return ans * f; } int n,m,q,x,y,fa[maxn]; char ch[10]; struct Node{ Node *ch[2]; int s,v,r,n; inline void maintain(){ s = ch[0]->s + ch[1]->s + 1; } }treap[250010]; Node *pt = treap, *null = pt++, *root[maxn]; inline Node *newnode(int x,int y){ pt->s = 1; pt->v = x; pt->n = y; pt->r = rand(); pt->ch[0] = pt->ch[1] = null; return pt++; } inline void rotate(Node *&o,int d){ Node *k = o->ch[d^1]; o->ch[d^1] = k->ch[d]; k->ch[d] = o; o->maintain(); k->maintain(); o = k; } void ins(int x,int y,Node *&p){ if (p == null) p = newnode(x,y); else{ int d = x > p->v; ins(x,y,p->ch[d]); if (p->ch[d]->r < p->r) rotate(p,d^1); } p->maintain(); } int select(int x,Node *p){ for(; ;){ int t = p->ch[0]->s; if (x == t + 1) return p->n; else if (x <= t) p = p->ch[0]; else x -= t + 1, p = p->ch[1]; } } void merge(Node *&x,Node *&y){ if (x == null) return; merge(x->ch[0],y); merge(x->ch[1],y); ins(x->v,x->n,y); } int getfa(int x){ return x == fa[x] ? x : fa[x] = getfa(fa[x]); } void join(int x,int y){ int a = getfa(x), b = getfa(y); if (a == b) return; if (root[a]->s > root[b]->s) swap(a,b); fa[a] = b; merge(root[a],root[b]); } int query(int x,int y){ int a = getfa(x); if (y > root[a]->s) return -1; return select(y,root[a]); } int main(){ n = read(); m = read(); null->s = 0; rep(i,1,n) root[i] = null; rep(i,1,n) fa[i] = i; rep(i,1,n){ x = read(); ins(x,i,root[i]); } rep(i,1,m){ x = read(); y = read(); join(x,y); } q = read(); rep(i,1,q){ scanf("%s",ch); x = read(); y = read(); if (ch[0] == 'B') join(x,y); else printf("%d\n",query(x,y)); } return 0; }
View Code
相关文章推荐
- 简单的web实现
- 我在JavaWeb开发中的SSH整合步骤
- WebView加载Domain=WebKitErrorDomain错误
- MFC定义全局变量
- 文章标题
- 构造函数 explicit 关键字的作用, from stackoverflow
- 蓝懿ios技术交流和心得分享2016年01月21日
- 蓝懿ios技术交流和心得分享2016年01月21日
- js中form的onsubmit不管用
- hihocoder #1176 : 欧拉路·一
- 79. Word Search
- linux内核中的C语言常规算法(前提:你的编译器要支持typeof和type)
- linux内核中的C语言常规算法(前提:你的编译器要支持typeof和type)
- linux内核中的C语言常规算法(前提:你的编译器要支持typeof和type)
- 管理公钥git
- 前缀表达式转函数表达式
- hdu 4842(NOIP 2005 过河)之 动态规划(距离压缩)
- Android属性动画欣赏——ObjectAnimator与动画监听事件
- 如何用Maven创建web项目(具体步骤)
- 几种基本的数字正则表达式[转]