This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define popcount(n) (__builtin_popcountll((n)))
#define clz(n) (__builtin_clzll((n)))
#define ctz(n) (__builtin_ctzll((n)))
#define lg(n) (63 - __builtin_clzll((n)))
#define BIT(n, i) (((n) >> (i)) & 1ll)
#define MASK(i) (1ll << (i))
#define FLIP(n, i) ((n) ^ (1ll << (i)))
#define ON(n, i) ((n) | MASK(i))
#define OFF(n, i) ((n) & ~MASK(i))
#define Int __int128
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<long long, int>> vli;
typedef vector<pair<int, long long>> vil;
template <class T1, class T2>
bool maximize(T1 &x, T2 y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
template <class T1, class T2>
bool minimize(T1 &x, T2 y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <class T>
void remove_duplicate(vector<T> &ve) {
    sort (ve.begin(), ve.end());
    ve.resize(unique(ve.begin(), ve.end()) - ve.begin());
}
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
template <class T> T random(T l, T r) {
    return uniform_int_distribution<T>(l, r)(rng);
}
template <class T> T random(T r) {
    return rng() % r;
}
const int N = 2000 + 5, M = 1e5 + 5;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
const vii TNK[3] = {
    {{0, 0}, {2, 0}, {3, 0}},
    {{0, 0}, {2, 2}, {0, 1}, {3, 0}},
    {{0, 0}, {0, 2}, {0, 1}}
};
int n, m, w, h;
int ans[M];
struct Circle {
    int x, y, r;
    Circle(int _x = 0, int _y = 0, int _r = 0) {
        x = _x, y = _y, r = _r;
    }
} a[N];
struct Info {
    int dist, u, v;
    Info(int _dist = 0, int _u = 0, int _v = 0) {
        dist = _dist, u = _u, v = _v;
    }
};
vector<Info> all;
ll square(int x) {
    return 1ll * x * x;
}
int floor_dist(Circle A, Circle B) {
    return sqrt(square(A.x - B.x) + square(A.y - B.y)) - A.r - B.r;
}
struct DSU {
    vector<int> lab;
    DSU() {}
    void resize(int n) {
        lab.assign(n + 1, -1);
    }
    int find_set(int p) {
        return lab[p] < 0 ? p : lab[p] = find_set(lab[p]);
    }
    bool same_set(int u, int v) {
        return find_set(u) == find_set(v);
    }
    bool join(int u, int v) {
        u = find_set(u), v = find_set(v);
        if (u != v) {
            if (lab[u] > lab[v]) swap(u, v);
            lab[u] += lab[v], lab[v] = u;
            return true;
        }
        return false;
    }
} dsu;
signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    cin >> n >> m >> w >> h;
    for (int i = 0; i < n; ++i) {
        cin >> a[i].x >> a[i].y >> a[i].r;
    }
    dsu.resize(n + 3);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            all.emplace_back(floor_dist(a[i], a[j]), i, j);
        }
        all.emplace_back(floor_dist(a[i], Circle(0, a[i].y, 0)), i, n);
        all.emplace_back(floor_dist(a[i], Circle(a[i].x, 0, 0)), i, n + 1);
        all.emplace_back(floor_dist(a[i], Circle(w, a[i].y, 0)), i, n + 2);
        all.emplace_back(floor_dist(a[i], Circle(a[i].x, h, 0)), i, n + 3);
    }
    for (int i = 1; i <= m; ++i) {
        int r, e; cin >> r >> e; --e;
        all.emplace_back(2 * r, e, -i);
    }
    sort (all.begin(), all.end(), [&](const Info &x, const Info &y) {
        if (x.dist == y.dist) return x.v < y.v;
        return x.dist < y.dist;
    });
    for (auto it : all) {
        // cerr << it.dist << ' ' << it.u << ' ' << it.v << '\n';
        if (it.v < 0) {
            int e = it.u, id = -it.v; ans[id] = MASK(4) - 1;
            // cerr << id << '\n';
            for (int i = 0; i < 3; ++i) {
                for (auto bruh : TNK[i]) {
                    int x = (e + bruh.fi) % 4, y = (e + 1 + bruh.se) % 4;
                    // cout << e << ' ' << i << ' ' << x << ' ' << y << '\n';
                    if (dsu.same_set(n + x, n + y)) ans[id] = OFF(ans[id], (e + i + 1) % 4);
                }
            }
        }
        else dsu.join(it.u, it.v);
    }
    for (int i = 1; i <= m; ++i) {
        for (int j = 0; j < 4; ++j) if (BIT(ans[i], j)) {
            cout << j + 1;
        }
        cout << '\n';
    }
    cerr << '\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... |