传送门

由于困难值小于等于x这个很恶心,可以离线处理,将边权,和询问时的x排序。

每到一个询问的时候,将边权小于等于x的都合并起来再询问。

。。

有重复元素的线段树合并的时间复杂度是nlog^2n

#include <cstdio>
#include <iostream>
#include <algorithm>
#define N 500001 int n, m, q, cnt, tot, size;
int sum[N * 10], ls[N * 10], rs[N * 10], a[N], b[N], f[N], root[N], ans[N], c[N << 1]; struct node
{
int x, y, z, id;
node(int x = 0, int y = 0, int z = 0, int id = 0) : x(x), y(y), z(z), id(id) {}
}p[N], ask[N]; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline bool cmp1(node x, node y)
{
return x.z < y.z;
} inline bool cmp2(node x, node y)
{
return x.y < y.y;
} inline void merge(int &x, int y)
{
if(!x || !y)
{
x += y;
return;
}
sum[x] += sum[y];
merge(ls[x], ls[y]);
merge(rs[x], rs[y]);
} inline void insert(int &now, int l, int r, int x)
{
now = ++size;
if(l == r)
{
sum[now] = 1;
return;
}
int mid = (l + r) >> 1;
if(x <= mid) insert(ls[now], l, mid, x);
else insert(rs[now], mid + 1, r, x);
sum[now] = sum[ls[now]] + sum[rs[now]];
} inline int query(int now, int l, int r, int x)
{
if(l == r) return l;
int mid = (l + r) >> 1;
if(x <= sum[ls[now]])
return query(ls[now], l, mid, x);
else
return query(rs[now], mid + 1, r, x - sum[ls[now]]);
} inline int find(int x)
{
return x == f[x] ? x : f[x] = find(f[x]);
} int main()
{
int i, j, x, y, z;
n = read();
m = read();
q = read();
for(i = 1; i <= n; i++) a[i] = b[i] = read();
std::sort(b + 1, b + n + 1);
cnt = std::unique(b + 1, b + n + 1) - b - 1;
for(i = 1; i <= n; i++)
{
a[i] = std::lower_bound(b + 1, b + cnt + 1, a[i]) - b;
f[i] = i;
insert(root[i], 1, cnt, a[i]);
}
for(i = 1; i <= m; i++)
{
x = read();
y = read();
c[i] = z = read();
p[i] = node(x, y, z, 0);
}
for(i = 1; i <= q; i++)
{
x = read();
c[i + m] = y = read();
z = read();
ask[i] = node(x, y, z, i);
}
std::sort(c + 1, c + m + q + 1);
tot = std::unique(c + 1, c + m + q + 1) - c - 1;
for(i = 1; i <= m; i++)
p[i].z = std::lower_bound(c + 1, c + tot + 1, p[i].z) - c;
for(i = 1; i <= q; i++)
ask[i].y = std::lower_bound(c + 1, c + tot + 1, ask[i].y) - c;
std::sort(p + 1, p + m + 1, cmp1);
std::sort(ask + 1, ask + q + 1, cmp2);
j = 1;
for(i = 1; i <= q; i++)
{
while(j <= m && p[j].z <= ask[i].y)
{
x = find(p[j].x);
y = find(p[j].y);
if(x ^ y)
{
f[y] = x;
merge(root[x], root[y]);
}
j++;
}
x = find(ask[i].x);
if(ask[i].z > sum[root[x]]) ans[ask[i].id] = -1;
else ans[ask[i].id] = b[query(root[x], 1, cnt, sum[root[x]] - ask[i].z + 1)];
}
for(i = 1; i <= q; i++) printf("%d\n", ans[i]);
return 0;
}

  

[BZOJ3545] [ONTAK2010]Peaks(线段树合并 + 离散化)的更多相关文章

  1. 【bzoj3545】[ONTAK2010]Peaks 线段树合并

    [bzoj3545][ONTAK2010]Peaks 2014年8月26日3,1512 Description 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路 ...

  2. BZOJ.3545.[ONTAK2010]Peaks(线段树合并)

    题目链接 \(Description\) 有n个座山,其高度为hi.有m条带权双向边连接某些山.多次询问,每次询问从v出发 只经过边权<=x的边 所能到达的山中,第K高的是多少. \(Solut ...

  3. Peaks 线段树合并

    Peaks 线段树合并 \(n\)个带权值\(h_i\)山峰,有\(m\)条山峰间双向道路,\(q\)组询问,问从\(v_i\)开始只经过\(h_i\le x\)的路径所能到达的山峰中第\(k\)高的 ...

  4. bzoj3545 Peaks 线段树合并

    离线乱搞... 也就是一个线段树合并没什么 #include<algorithm> #include<iostream> #include<cstring> #in ...

  5. 【线段树合并】bzoj3545: [ONTAK2010]Peaks

    1A还行 Description 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问, ...

  6. BZOJ3545 Peaks 离线处理+线段树合并

    题意: 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询问从点v开始只经 ...

  7. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

  8. bzoj3545: [ONTAK2010]Peaks 重构树 主席树

    题目链接 bzoj3545: [ONTAK2010]Peaks 题解 套路重构树上主席树 代码 #include<cstdio> #include<algorithm> #de ...

  9. 线段树合并&&启发式合并笔记

    这俩东西听起来很高端,实际上很好写,应用也很多~ 线段树合并 线段树合并,顾名思义,就是建立一棵新的线段树保存原有的两颗线段树的信息. 考虑如何合并,对于一个结点,如果两颗线段树都有此位置的结点,则直 ...

随机推荐

  1. Android程序初体验

    第一个程序的实现的最终功能是: 点击"正确"或者"错误"会得到一个是否正确的提示. 直接上效果图.     此次涉及代码编写的文件有4个: package co ...

  2. SQL server 数据库基础语句

    上篇介绍的是鼠标操作 遗漏两个知识: 主外键 两个列 数据类型 必须一致    //int类型不能约束nvarchar 类型      varchar类型不能约束nvarchar类型 varchar( ...

  3. App Store上的开源应用汇总

    以下是互联网上主要的开源iOS应用的列表,在学习的时候,多看看完成的功能代码可以给我们带来很多经验,但是除了Apple官方提供的Sample Code之外,我们很难找到优质的开源项目代码,所以我搜集了 ...

  4. 作用域插槽 向父组件传递 <template slot-scope="{ row, index }" slot="dateNo">

    作用域插槽 向父组件传递 <template slot-scope="{ row, index }"  slot="dateNo"> slotTes ...

  5. Robotium实践之路基于APK创建测试项目

    1.重新对包进行签名操作 .启动re-sign.jar文件 .找到相应的APK,拖拽置resigner中 2.创建基于APK测试的测试工程 .新建一个安卓测试项目 .选择this project

  6. GYM 101604 || 20181010

    看着前面咕咕咕的国庆集训 难受 十月十日要萌一天哇www A.字符串 题意:给定一个字符串 问能否交换两个字符或者不交换字符,使其成为回文串 之前写的太丑 重写一遍加一堆 if 竟然过了w 思路:求出 ...

  7. PAT (Advanced Level) Practise - 1096. Consecutive Factors (20)

    http://www.patest.cn/contests/pat-a-practise/1096 Among all the factors of a positive integer N, the ...

  8. rsync文档

    rsync文档 1.rsync filter过滤 参考http://share.blog.51cto.com/278008/567578/

  9. 830. Positions of Large Groups@python

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

  10. python 多线程 压测 mysql

    #!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油 #@file: sthread.py #@time: 2018/9/17 17:07 im ...