Submission #1052883

# Submission time Handle Problem Language Result Execution time Memory
1052883 2024-08-11T05:28:31 Z caterpillow Park (BOI16_park) C++17
100 / 100
222 ms 102044 KB
#include <bits/stdc++.h>
#include <ratio>

using namespace std;

using db = long double;
using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end() 
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;

/*

answer queries in increasing radius of visitors
we can consider increasing the radius of visitors as expanding trees and pushing inwards the sides of the rectangle
this way, visitors are always points
for each pair of trees, there will be some radius such that the "gap" between them is filled

if we consider the walls and trees as nodes, if two opposing walls belong to the same connected component, then you cant go between them
we can do dsu and just spam stuff to check

*/

#define sq(x) ((x) * (x))

struct DSU {
    vt<int> e;
    void init(int n) {
        e.resize(n, -1);
    }
    int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
    void unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y) return;
        if (e[x] < e[y]) swap(x, y);
        e[y] += e[x];
        e[x] = y;
    }
    bool same_set(int u, int v) {
        return find(u) == find(v);
    }
};

ll floor_sqrt(ll x) {
    ll y = sqrt(x);
    while (y * y < x) y++;
    while (y * y > x) y--;
    return y;
}

struct Tree {
    ll x, y, r;
};

struct Event {
    ll k;
    int t, f, s;

    bool operator<(const Event& rhs) const {
        return k == rhs.k ? t < rhs.t : k < rhs.k;
    }
};

int n, q;
ll w, h; 

vt<Event> evs;
vt<int> ans;
vt<Tree> trees;

// neighbour walls of each thingy
// walls are indexed starting from right, anticlockwise
vt<vt<int>> adj {
    {2, 3},
    {0, 3},
    {0, 1},
    {1, 2}
};

main() {
    cin.tie(0)->sync_with_stdio(0);
    
    cin >> n >> q >> w >> h;
    ans.resize(q);
    trees.resize(n);

    F0R (i, n) {
        cin >> trees[i].x >> trees[i].y >> trees[i].r;
    }

    F0R (i, q) {
        ll r; int s; cin >> r >> s;
        evs.pb({2 * r, 1, s - 1, i});
    }

    F0R (i, n) {
        FOR (j, i + 1, n) {
            ll dist = floor_sqrt(sq(trees[i].x - trees[j].x) + sq(trees[i].y - trees[j].y)) - trees[i].r - trees[j].r;
            evs.pb({dist + 1, 0, i, j});
        }
    }
    F0R (i, n) {
        // right
        evs.pb({w - trees[i].x - trees[i].r + 1, 0, i, n});
        // top
        evs.pb({h - trees[i].y - trees[i].r + 1, 0, i, n + 1});
        // left
        evs.pb({trees[i].x - trees[i].r + 1, 0, i, n + 2});
        // bot
        evs.pb({trees[i].y - trees[i].r + 1, 0, i, n + 3});
    }

    sort(all(evs));

    DSU dsu;
    dsu.init(n + 4);

    for (auto& ev : evs) {
        if (ev.t == 0) {
            dsu.unite(ev.f, ev.s);
        } else {
            int s = ev.f, qi = ev.s;

            int num = 0;

            F0R (i, 4) {
                bool good = true;

                if (s != i) {
                    if ((i - s + 4) % 4 == 2) {
                        // opposite
                        for (int x : adj[(s + 1) % 4]) {
                            for (int y : adj[(i + 1) % 4]) {
                                good &= !dsu.same_set(n + x, n + y);
                            }
                        }
                    } else {
                        // adjacent
                        int com = -1; // common wall
                        for (int x : adj[s]) for (int y : adj[i]) if (x == y) com = x; 

                        F0R (j, 4) {
                            if (j != com) good &= !dsu.same_set(n + j, n + com);
                        }
                    }
                }

                if (good) num = num * 10 + i + 1;
            }

            ans[qi] = num;
        }
    }

    for (int x : ans) cout << x << endl;
}

Compilation message

park.cpp:91:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   91 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 180 ms 51392 KB Output is correct
2 Correct 177 ms 49840 KB Output is correct
3 Correct 183 ms 51904 KB Output is correct
4 Correct 179 ms 49856 KB Output is correct
5 Correct 177 ms 51388 KB Output is correct
6 Correct 177 ms 50112 KB Output is correct
7 Correct 158 ms 49856 KB Output is correct
8 Correct 161 ms 51132 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 4096 KB Output is correct
2 Correct 25 ms 4820 KB Output is correct
3 Correct 24 ms 4048 KB Output is correct
4 Correct 25 ms 4048 KB Output is correct
5 Correct 25 ms 4048 KB Output is correct
6 Correct 25 ms 4052 KB Output is correct
7 Correct 23 ms 4820 KB Output is correct
8 Correct 28 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 180 ms 51392 KB Output is correct
2 Correct 177 ms 49840 KB Output is correct
3 Correct 183 ms 51904 KB Output is correct
4 Correct 179 ms 49856 KB Output is correct
5 Correct 177 ms 51388 KB Output is correct
6 Correct 177 ms 50112 KB Output is correct
7 Correct 158 ms 49856 KB Output is correct
8 Correct 161 ms 51132 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 25 ms 4096 KB Output is correct
12 Correct 25 ms 4820 KB Output is correct
13 Correct 24 ms 4048 KB Output is correct
14 Correct 25 ms 4048 KB Output is correct
15 Correct 25 ms 4048 KB Output is correct
16 Correct 25 ms 4052 KB Output is correct
17 Correct 23 ms 4820 KB Output is correct
18 Correct 28 ms 5332 KB Output is correct
19 Correct 207 ms 101796 KB Output is correct
20 Correct 210 ms 100380 KB Output is correct
21 Correct 222 ms 100888 KB Output is correct
22 Correct 208 ms 100532 KB Output is correct
23 Correct 205 ms 101328 KB Output is correct
24 Correct 208 ms 102044 KB Output is correct