传送门

由于困难值小于等于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. HDU 3586 Information Disturbing (树形DP,二分)

    题意: 给定一个敌人的通信系统,是一棵树形,每个节点是一个敌人士兵,根节点是commander,叶子是前线,我们的目的是使得敌人的前线无法将消息传到commander,需要切断一些边,切断每条边需要一 ...

  2. codevs 1487 大批整数排序(水题日常)

     时间限制: 3 s  空间限制: 16000 KB  题目等级 : 黄金 Gold 题目描述 Description !!!CodeVS开发者有话说: codevs自从换了评测机,新评测机的内存计算 ...

  3. 交互干货必收 | App界面交互设计规范

    原文地址:http://www.woshipm.com/ucd/193776.html

  4. 获取CPU相关信息

    实现效果: 知识运用:  WMI管理类中的ManagementObjectCollection类    ManagementObjectSearcher类的Get方法  和ManagementObje ...

  5. PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)

    PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)  http://www.patest.cn/contests/pat-b-practise/1034 ...

  6. HTML网页的浏览器标题栏显示小图标的方法

    HTML网页的浏览器标题栏显示小图标的方法   就像这种效果,方法其实很简单,就是 在head头部里写: <link rel='icon' href='pic.ico ' type='image ...

  7. javaEE(4)_response、request对象

    一.简介 Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应 ...

  8. 计算机应用第七次作业 html制作个人音乐播放站点

    计算机应用第七次作业 html制作个人音乐播放站点 请访问下边网址查看具体操作: http://www.cnblogs.com/qingyundian/p/7878892.html

  9. Dev-Cpp 5.11 c++编译器下载

    Dev-Cpp 5.11 c++编译器下载地址: 链接: https://pan.baidu.com/s/1jHMAf1k 密码: i6nw

  10. Xcode 6 创建 Objective-C category

    1. Command + N 2. 选择 iOS - Source - Objective-C File 3.File Type 选择 Category,Class 填基于的类名,File填扩展的名