传送门

由于困难值小于等于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;
}

  

最新文章

  1. web项目知识整理
  2. 08章 分组查询、子查询、原生SQL
  3. Qt出现cannot find -IGL错误
  4. 【经典】C++&amp;RPG对战游戏
  5. codeforces mysterious present 最长上升子序列+倒序打印路径
  6. sencha gridpanel 单元格编辑
  7. Windows IP安全策略。
  8. static,interface and final
  9. 调用java的webservice返回null
  10. Java的一点内容(2)
  11. Linux 上搭建 git 的服务器
  12. CodeForces 820C
  13. (编辑距离问题 线性DP) nyoj1431-DNA基因鉴定
  14. IDEA15 创建javaweb 并配置Tomcat(转)
  15. HTML页面本地正常,部署到服务器稍微异常解决方案
  16. wheel
  17. jackson用法
  18. 服务消费和负载(Feign)
  19. mysql 常用指令集合
  20. 《经久不衰的Spring框架:@ResponseBody 中文乱码》(转)

热门文章

  1. java 序列化Serializable 详解
  2. 一个具体的例子学习Java volatile关键字
  3. htmlunit爬取js异步加载后的页面
  4. 特别困的学生 UVa12108(模拟题)
  5. Open Cascade:AIS_InteractiveContext如何调用函数选择AIS对象
  6. [已解决]gitee初次使用git clone报错
  7. Python协程函数
  8. PAT (Basic Level) Practise (中文)-1030. 完美数列(25)
  9. Bootstrap历练实例:导航中的表单
  10. Git学习之路