G. Vlad and the Mountains
1.牛客周赛 Round 45 F 小橙的圈圈2.T461430 「Daily OI Round 4」Mine
3.G. Vlad and the Mountains
4.F. Mice and HolesG. Vlad and the Mountains
https://codeforces.com/contest/1851/problem/G
离线+并查集
首先观察路径
不难发现我们的路径最后的变化量只和起点和终点的高度有关。
那么要以
容易想到考虑点权的并查集的 trick,想到离线处理,把询问按
Code
#include <bits/stdc++.h>
using LL = long long;
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<std::vector<int>> g(n + 1);
std::vector<std::array<int, 2>> p(n + 1);
for (int i = 1; i <= n; i++) {
std::cin >> p[i][0];
p[i][1] = i;
}
while (m--) {
int u, v;
std::cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int q;
std::cin >> q;
std::vector<std::array<int, 4>> qry(q + 1);
std::vector<int> ans(q + 1);
for (int i = 1; i <= q; i++) {
int a, b, e;
std::cin >> a >> b >> e;
qry[i][0] = p[a][0] + e, qry[i][1] = a, qry[i][2] = b, qry[i][3] = i;
}
std::sort(qry.begin() + 1, qry.end());
std::sort(p.begin() + 1, p.end());
std::vector<int> f(n + 1);
for (int i = 1; i <= n; i++) {
f[i] = i;
}
std::function<int(int)> find = [&](int x) -> int {
return x == f[x] ? x : f[x] = find(f[x]);
};
std::vector<bool> st(n + 1, false);
auto add = [&](int u) {
st[u] = true;
for (auto v : g[u]) {
if (st[v]) {
f[find(v)] = find(u);
}
}
};
for (int i = 1, j = 1; i <= q; i++) {
while (j <= n && p[j][0] <= qry[i][0]) {
add(p[j++][1]);
}
ans[qry[i][3]] = find(qry[i][1]) == find(qry[i][2]);
}
for (int i = 1; i <= q; i++) {
std::cout << (ans[i] ? "YES" : "NO") << '\n';
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
合集:
好题集锦
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】