[ONTAK2010] Peaks 加强版
[ONTAK2010] Peaks 加强版
题目大意:原题变为强制在线查询
Solution
- 读入山高,排序后依然建立树链,初始化并查集,初始化重构树新节点标号为\(n+1\)
- 读入边,按照边权从小到大排序
- \(kruskal\)重构树
- 如果是在同一并查集里,\(continue\)
- 如果不在,建立新节点,两个节点的\(a\)都是新节点,并且记录在重构树上的\(fa\),合并树链
- 倍增处理重构树
- 读入询问,如果上一次询问的答案不为\(-1\),\(xor\)上一次的答案,在重构树上跳,查询答案
Code
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define pf(x) printf("%d\n", x);
using std::sort;
const int X = 3400000 + 5;
const int M = 5e5 + 5;
const int N = 1e5 + 10;
int n, m, q, node, cnt, lastans = 0;
int sum[X], lson[X], rson[X], kfa[N << 1], ju[N << 1][18], val[N], fa[N << 1], root[N << 1];
struct Hill{
int id, hi;
bool operator < (const Hill &cmp) const {
return hi < cmp.hi;
}
}h[N];
struct Aha{
int a, b, c;
bool operator < (const Aha &cmp) const {
return c < cmp.c;
}
}a[M];
int find(int x){
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void insert(int &cur, int l, int r, int p){
if(!cur) cur = ++cnt;
sum[cur]++;
if(l == r) return;
int mid = l + ((r - l) >> 1);
if(p <= mid) insert(lson[cur], l, mid, p);
else insert(rson[cur], mid + 1, r, p);
}
void merge(int &cur, int x, int y, int l, int r){
if(!cur) cur = ++cnt;
sum[cur] = sum[x] + sum[y];
if(l == r) return;
int mid = l + ((r - l) >> 1);
if(lson[x] && lson[y]) merge(lson[cur], lson[x], lson[y], l, mid);
else if(lson[x]) lson[cur] = lson[x];
else if(lson[y]) lson[cur] = lson[y];
if(rson[x] && rson[y]) merge(rson[cur], rson[x], rson[y], mid + 1, r);//1, mid
else if(rson[x]) rson[cur] = rson[x];
else if(rson[y]) rson[cur] = rson[y];
}
int query(int cur, int l, int r, int p){
if(l == r){
return h[l].hi;//
}
int mid = l + ((r - l) >> 1);
if(p <= sum[lson[cur]]) return query(lson[cur], l, mid, p);
else return query(rson[cur], mid + 1, r, p - sum[lson[cur]]);
}
inline int jump(int &x, int y){
for(int i = 17; i >= 0; --i)
if(ju[x][i] && val[ju[x][i] - n] <= y){
x = ju[x][i];
}
return x;
}
int main(){
scanf("%d %d %d", &n, &m, &q);
for(int i = 1; i <= n; ++i)
scanf("%d", &h[i].hi), h[i].id = i, fa[i] = i;
sort(h + 1, h + n + 1);
for(int i = 1; i <= n; ++i)
insert(root[h[i].id], 1, n, i);
node = n;
for(int i = 1; i <= m; ++i)
scanf("%d %d %d", &a[i].a, &a[i].b, &a[i].c);
sort(a + 1, a + 1 + m);
for(int i = 1, fx, fy; i <= m; ++i){
fx = find(a[i].a);
fy = find(a[i].b);
if(fx == fy) continue;
fa[++node] = fa[fx] = fa[fy] = kfa[fx] = kfa[fy] = node;
val[node - n] = a[i].c;
merge(root[node], root[fx], root[fy], 1, n);
}
for(int i = 1; i <= node; ++i) ju[i][0] = kfa[i];
for(int j = 1; j <= 17; ++j)
for(int i = 1; i <= node; ++i)// i <= n 以及顺序
ju[i][j] = ju[ju[i][j - 1]][j - 1];
int la, lb, lc;
while(q--){
scanf("%d %d %d", &la, &lb, &lc);
if(lastans != -1) la ^= lastans, lb ^= lastans, lc ^= lastans;
int u = jump(la, lb);
if(sum[root[u]] < lc) lastans = -1;
else lastans = query(root[u], 1, n, sum[root[u]] - lc + 1);
printf("%d\n", lastans);
}
return 0;
}
[ONTAK2010] Peaks 加强版的更多相关文章
- BZOJ 3551: [ONTAK2010]Peaks加强版 [Kruskal重构树 dfs序 主席树]
3551: [ONTAK2010]Peaks加强版 题意:带权图,多组询问与一个点通过边权\(\le lim\)的边连通的点中点权k大值,强制在线 PoPoQQQ大爷题解传送门 说一下感受: 容易发现 ...
- 【BZOJ3551】 [ONTAK2010]Peaks加强版
BZOJ3551 [ONTAK2010]Peaks加强版 Solution Kruscal重构树后发现可以对于小于的离散化然后倍增+主席树找到上一个的可行解. 然后就可以了. 如果数组开的不好,容易在 ...
- 【BZOJ3551】[ONTAK2010]Peaks加强版 最小生成树+DFS序+主席树
[BZOJ3545][ONTAK2010]Peaks Description 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困 ...
- 3551: [ONTAK2010]Peaks加强版
3551: [ONTAK2010]Peaks加强版 https://www.lydsy.com/JudgeOnline/problem.php?id=3551 分析: kruskal重构树 + 倍增 ...
- [BZOJ3551][ONTAK2010]Peaks(加强版)(Kruskal重构树,主席树)
3551: [ONTAK2010]Peaks加强版 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2438 Solved: 763[Submit][ ...
- bzoj 3545&&3551: [ONTAK2010]Peaks &&加强版 平衡树&&并查集合并树&&主席树
3545: [ONTAK2010]Peaks Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 635 Solved: 177[Submit][Stat ...
- bzoj 3551: [ONTAK2010]Peaks加强版
Description [题目描述]同3545 Input 第一行三个数N,M,Q. 第二行N个数,第i个数为h_i 接下来M行,每行3个数a b c,表示从a到b有一条困难值为c的双向路径. 接下来 ...
- ONTAK2010 Peaks加强版(离线&在线)
题面 弱化版:luogu 强制在线版:bzoj 题解 本题有两种解法 离线算法:线段树合并 先看一道简单题[USACO18JAN]MooTube 本题就是在此基础上求第\(k\)高的点 首先把询问和路 ...
- BZOJ 3551/3545: [ONTAK2010]Peaks加强版 (Kruskal树+dfs序上的主席树+倍增)
Orz PoPoQQQ 学到了维护子树信息的时候用dfsdfsdfs序套主席树节省线段树空间. 学到了怎么用指针写可持久化线段树-emmm- CODE 只贴上3551加强版带强制在线的代码 #incl ...
随机推荐
- 在kubernetes集群里集成Apollo配置中心(5)之dubbo服务消费者连接apollo实战
1.在Apollo的portal创建dubbo消费者项目 (1)添加dubbo消费者项目 (2)在dubbo消费者项目中添加配置项 (3)发布 2.通过jenkins构建dubbo消费者镜像 3.登录 ...
- vscode remote Development ssh targets 连接腾讯云
配置 本地安装 插件 remote development 点击左侧,选择ssh 点击设置按钮,选择ssh配置文件 配置举例 ``` Host tencentcloud HostName ...
- leetcode 周赛 205 1576-5508-5509-5510
第四题比较难,看题解用并查集做比较简单,但是我觉得难度在想到用并查集,可能是最近做题少所以想不到吧. 1 替换所有的问号 class Solution { public: string modifyS ...
- JavaScript DOM操作之查找元素节点
概要: 编程接口 可通过 JavaScript 对 HTML DOM 进行访问. 所有 HTML 元素被定义为对象,而编程接口则是对象方法和对象属性. 方法是您能够执行的动作(比如添加或修改元素). ...
- CodeForces 1047C Enlarge GCD(数论)题解
题意:n个数的gcd是k,要你删掉最少的数使得删完后的数组的gcd > k 思路:先求出k,然后每个数除以k.然后找出出现次数最多的质因数即可. 代码: #include<cmath> ...
- GitHub new features 2020 All In One
GitHub new features 2020 All In One Discussions Discussions is the space for your community to have ...
- CSS clip-path in action
CSS clip-path in action <!DOCTYPE html> <html lang="en"> <head> <meta ...
- 最新 Apple iPhone 12 价格 All In One
最新 Apple iPhone 12 价格 All In One 美版价格 Apple iPhone 12 mini $699 Apple iPhone 12 $799 Apple iPhone 12 ...
- web components & publish custom element & npm
web components & publish custom element & npm https://www.webcomponents.org/publish Polymer ...
- news of javascript
news of javascript https://javascriptweekly.com/ https://javascriptweekly.com/issues https://www.inf ...