#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define int long long int
struct Circle {
int x, y; // centre-point
int rsq, ir; // radius squared, integer radius
};
struct Edge {
int u, v; ld w;
};
struct DSU {
int n; vector<int> par, siz;
DSU(int N) {
n = N;
par.resize(n); iota(par.begin(), par.end(), 0);
siz.assign(n, 1);
}
int find(int v) {
if (par[v] == v) return v;
return par[v] = find(par[v]);
}
void unite(int a, int b) {
a = find(a); b = find(b);
if (a != b) {
if (siz[b] > siz[a]) swap(a, b);
par[b] = a;
siz[a] += siz[b];
}
}
};
signed main() {
cin.tie(0)->sync_with_stdio(0);
// read n, m, w, h and the trees ...
int n, m; cin >> n >> m;
int w, h; cin >> w >> h;
vector<Circle> trees;
for (int i = 0; i < n; i++) {
int x, y, r; cin >> x >> y >> r;
trees.push_back(Circle {x, y, r*r, r});
}
// preprocess ...
// node n is top wall, n+1 is bottom wall, n+2 is left wall, n+3 is right wall
vector<Edge> edges;
for (int i = 0; i < n; i++) {
edges.push_back(Edge {i, n, ld(h - (trees[i].y + trees[i].ir))});
edges.push_back(Edge {i, n+1, ld(trees[i].y - trees[i].ir)});
edges.push_back(Edge {i, n+2, ld(trees[i].x - trees[i].ir)});
edges.push_back(Edge {i, n+3, ld(w - (trees[i].x + trees[i].ir))});
for (int j = i + 1; j < n; j++) {
int dx = trees[i].x - trees[j].x, dy = trees[i].y - trees[j].y;
edges.push_back(Edge{i, j, sqrt(ld(dx*dx+dy*dy)) - ld(trees[i].ir) - ld(trees[j].ir)});
}
}
sort(edges.begin(), edges.end(), [](Edge &a, Edge &b) {
return a.w < b.w;
});
vector<string> answers(m);
vector<int> remap(m);
vector<pair<int, int>> queries(m); // r, e
for (int i = 0; i < m; i++) {
cin >> queries[i].first >> queries[i].second;
}
iota(remap.begin(), remap.end(), 0);
sort(remap.begin(), remap.end(), [&](int a, int b) {
return queries[a].first < queries[b].first;
});
auto dsu = DSU(n+4);
int cq = 0;
for (auto &el : edges) {
while (cq < m && el.w > 2 * queries[remap[cq]].first) {
string ans = "";
int t = dsu.find(n), b = dsu.find(n+1), l = dsu.find(n+2), r = dsu.find(n+3);
bool closed[4] = {b == l, b == r, t == r, t == l};
for (int i = 0; i < 4; i++) {
if (i == queries[remap[cq]].second - 1) {
ans = ans + to_string(i+1);
continue;
}
if (!closed[queries[remap[cq]].second - 1] && !closed[i]) {
ans = ans + to_string(i+1);
}
}
answers[remap[cq]] = ans;
cq++;
}
dsu.unite(el.u, el.v);
}
while (cq < m) {
string ans = "";
int t = dsu.find(n), b = dsu.find(n+1), l = dsu.find(n+2), r = dsu.find(n+3);
bool closed[4] = {b == l, b == r, t == r, t == l};
for (int i = 0; i < 4; i++) {
if (i == queries[remap[cq]].second - 1) {
ans = ans + to_string(i+1);
continue;
}
if (!closed[queries[remap[cq]].second - 1] && !closed[i]) {
ans = ans + to_string(i+1);
}
}
answers[remap[cq]] = ans;
cq++;
}
// answer queries ...
for (int i = 0; i < m; i++) {
cout << answers[i] << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |