Submission #998155

#TimeUsernameProblemLanguageResultExecution timeMemory
998155green_gold_dogIzvanzemaljci (COI21_izvanzemaljci)C++17
56 / 100
527 ms8412 KiB
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;

constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;

random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());

template<typename T>
bool assign_max(T& a, T b) {
        if (b > a) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
bool assign_min(T& a, T b) {
        if (b < a) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
T square(T a) {
        return a * a;
}

template<>
struct std::hash<pair<ll, ll>> {
        ll operator() (pair<ll, ll> p) const {
                return ((__int128)p.first * MOD + p.second) % INF64;
        }
};

ll gmaxpreff(vector<pair<ll, ll>>& all, ll s) {
        ll nx = all.begin()->first;
        ll miny = INF32, maxy = -INF32;
        auto now = all.begin();
        while (now != all.end()) {
                auto[x, y] = *now;
                if (x - nx > s) {
                        return x;
                }
                assign_min(miny, y);
                assign_max(maxy, y);
                if (maxy - miny > s) {
                        return x;
                }
                now++;
        }
        return INF32;
}

ll gmaxsufff(vector<pair<ll, ll>>& all, ll s) {
        ll nx = all.rbegin()->first;
        ll miny = INF32, maxy = -INF32;
        auto now = all.rbegin();
        while (now != all.rend()) {
                auto[x, y] = *now;
                if (nx - x > s) {
                        return x;
                }
                assign_min(miny, y);
                assign_max(maxy, y);
                if (maxy - miny > s) {
                        return x;
                }
                now++;
        }
        return -INF32;
}

ll gmaxprefs(vector<pair<ll, ll>>& all, ll s) {
        ll nx = all.begin()->second;
        ll miny = INF32, maxy = -INF32;
        auto now = all.begin();
        while (now != all.end()) {
                auto[y, x] = *now;
                if (x - nx > s) {
                        return x;
                }
                assign_min(miny, y);
                assign_max(maxy, y);
                if (maxy - miny > s) {
                        return x;
                }
                now++;
        }
        return INF32;
}

ll gmaxsuffs(vector<pair<ll, ll>>& all, ll s) {
        ll nx = all.rbegin()->second;
        ll miny = INF32, maxy = -INF32;
        auto now = all.rbegin();
        while (now != all.rend()) {
                auto[y, x] = *now;
                if (nx - x > s) {
                        return x;
                }
                assign_min(miny, y);
                assign_max(maxy, y);
                if (maxy - miny > s) {
                        return x;
                }
                now++;
        }
        return -INF32;
}

pair<bool, vector<tuple<ll, ll, ll>>> s2h(ll s, vector<pair<ll, ll>>& arr, bool b = true) {
        vector<tuple<ll, ll, ll>> ans;
        if (arr.empty()) {
                ans.emplace_back(2'500'000'000, 2'500'000'000, 1);
                ans.emplace_back(3'000'000'000, 3'000'000'000, 1);
                return make_pair(true, ans);
        }
        sort(arr.begin(), arr.end());
        ll mpref = gmaxpreff(arr, s), msuff = gmaxsufff(arr, s);
        if (mpref <= msuff) {
                return make_pair(false, ans);
        }
        ll maxx1 = -INF32, minx1 = INF32;
        ll maxy1 = -INF32, miny1 = INF32;
        ll maxx2 = -INF32, minx2 = INF32;
        ll maxy2 = -INF32, miny2 = INF32;
        for (auto[x, y] : arr) {
                if (x < mpref) {
                        assign_max(maxx1, x);
                        assign_min(minx1, x);
                        assign_max(maxy1, y);
                        assign_min(miny1, y);
                } else {
                        assign_max(maxx2, x);
                        assign_min(minx2, x);
                        assign_max(maxy2, y);
                        assign_min(miny2, y);
                }
        }
        if (b) {
                ans.emplace_back(maxx1 - s, miny1, s);
                ans.emplace_back(minx2, miny2, s);
        } else {
                ans.emplace_back(maxx1 - s, maxy1 - s, s);
                ans.emplace_back(minx2, maxy2 - s, s);
        }
        return make_pair(true, ans);
}

bool cmp(pair<ll, ll> p1, pair<ll, ll> p2) {
        return p1.second < p2.second;
}

pair<bool, vector<tuple<ll, ll, ll>>> s2v(ll s, vector<pair<ll, ll>>& arr, bool b = true) {
        vector<tuple<ll, ll, ll>> ans;
        if (arr.empty()) {
                ans.emplace_back(2'500'000'000, 2'500'000'000, 1);
                ans.emplace_back(3'000'000'000, 3'000'000'000, 1);
                return make_pair(true, ans);
        }
        sort(arr.begin(), arr.end(), cmp);
        ll mpref = gmaxprefs(arr, s), msuff = gmaxsuffs(arr, s);
        if (mpref <= msuff) {
                return make_pair(false, ans);
        }
        ll maxx1 = -INF32, minx1 = INF32;
        ll maxy1 = -INF32, miny1 = INF32;
        ll maxx2 = -INF32, minx2 = INF32;
        ll maxy2 = -INF32, miny2 = INF32;
        for (auto[x, y] : arr) {
                if (y < mpref) {
                        assign_max(maxx1, x);
                        assign_min(minx1, x);
                        assign_max(maxy1, y);
                        assign_min(miny1, y);
                } else {
                        assign_max(maxx2, x);
                        assign_min(minx2, x);
                        assign_max(maxy2, y);
                        assign_min(miny2, y);
                }
        }
        if (b) {
                ans.emplace_back(minx1, maxy1 - s, s);
                ans.emplace_back(minx2, miny2, s);
        } else {
                ans.emplace_back(maxx1 - s, maxy1 - s, s);
                ans.emplace_back(maxx2 - s, miny2, s);
        }
        return make_pair(true, ans);
}

pair<bool, vector<tuple<ll, ll, ll>>> s3hl(ll s, vector<pair<ll, ll>>& arr) {
        sort(arr.begin(), arr.end());
        ll mpref = gmaxpreff(arr, s);
        vector<pair<ll, ll>> narr;
        ll maxx = -INF32, minx = INF32;
        ll maxy = -INF32, miny = INF32;
        for (auto[x, y] : arr) {
                if (x < mpref) {
                        assign_max(maxx, x);
                        assign_min(minx, x);
                        assign_max(maxy, y);
                        assign_min(miny, y);
                } else {
                        narr.emplace_back(x, y);
                }
        }
        vector<tuple<ll, ll, ll>> ans;
        ans.emplace_back(maxx - s, maxy - s, s);
        auto[f, nans] = s2v(s, narr, true);
        if (!f) {
                return make_pair(false, nans);
        }
        for (auto i : nans) {
                ans.push_back(i);
        }
        return make_pair(true, ans);
}

pair<bool, vector<tuple<ll, ll, ll>>> s3hr(ll s, vector<pair<ll, ll>>& arr) {
        sort(arr.begin(), arr.end());
        ll msuff = gmaxsufff(arr, s);
        vector<pair<ll, ll>> narr;
        ll maxx = -INF32, minx = INF32;
        ll maxy = -INF32, miny = INF32;
        for (auto[x, y] : arr) {
                if (x > msuff) {
                        assign_max(maxx, x);
                        assign_min(minx, x);
                        assign_max(maxy, y);
                        assign_min(miny, y);
                } else {
                        narr.emplace_back(x, y);
                }
        }
        vector<tuple<ll, ll, ll>> ans;
        ans.emplace_back(minx, miny, s);
        auto[f, nans] = s2v(s, narr, false);
        if (!f) {
                return make_pair(false, nans);
        }
        for (auto i : nans) {
                ans.push_back(i);
        }
        return make_pair(true, ans);
}

pair<bool, vector<tuple<ll, ll, ll>>> s3vl(ll s, vector<pair<ll, ll>>& arr) {
        sort(arr.begin(), arr.end(), cmp);
        ll mpref = gmaxprefs(arr, s);
        vector<pair<ll, ll>> narr;
        ll maxx = -INF32, minx = INF32;
        ll maxy = -INF32, miny = INF32;
        for (auto[x, y] : arr) {
                if (y < mpref) {
                        assign_max(maxx, x);
                        assign_min(minx, x);
                        assign_max(maxy, y);
                        assign_min(miny, y);
                } else {
                        narr.emplace_back(x, y);
                }
        }
        vector<tuple<ll, ll, ll>> ans;
        ans.emplace_back(maxx - s, maxy - s, s);
        auto[f, nans] = s2h(s, narr, true);
        if (!f) {
                return make_pair(false, nans);
        }
        for (auto i : nans) {
                ans.push_back(i);
        }
        return make_pair(true, ans);
}

pair<bool, vector<tuple<ll, ll, ll>>> s3vr(ll s, vector<pair<ll, ll>>& arr) {
        sort(arr.begin(), arr.end(), cmp);
        ll msuff = gmaxsuffs(arr, s);
        vector<pair<ll, ll>> narr;
        ll maxx = -INF32, minx = INF32;
        ll maxy = -INF32, miny = INF32;
        for (auto[x, y] : arr) {
                if (y > msuff) {
                        assign_max(maxx, x);
                        assign_min(minx, x);
                        assign_max(maxy, y);
                        assign_min(miny, y);
                } else {
                        narr.emplace_back(x, y);
                }
        }
        vector<tuple<ll, ll, ll>> ans;
        ans.emplace_back(minx, miny, s);
        auto[f, nans] = s2h(s, narr, false);
        if (!f) {
                return make_pair(false, nans);
        }
        for (auto i : nans) {
                ans.push_back(i);
        }
        return make_pair(true, ans);
}

struct segment_tree {
        vector<ll> tree, m;
        ll size;
        segment_tree(ll n) {
                size = 1;
                while (size < n) {
                        size *= 2;
                }
                tree.resize(size * 2, 0);
                m.resize(size * 2, 0);
        };
        void add(ll v, ll x) {
                tree[v] += x;
                m[v] += x;
        }
        void push(ll v) {
                add(v * 2, m[v]);
                add(v * 2 + 1, m[v]);
                m[v] = 0;
        }
        pair<ll, ll> get() {
                return get(1, 0, size);
        }
        pair<ll, ll> get(ll v, ll l, ll r) {
                if (r - l == 1) {
                        return make_pair(l, tree[v]);
                }
                ll mid = (l + r) / 2;
                if (tree[v * 2] >= 0) {
                        return get(v * 2, l, mid);
                } else {
                        return get(v * 2 + 1, mid, r);
                }
        }
        void add(ll l, ll r, ll x) {
                add(1, 0, size, l, r, x);
        }
        void add(ll v, ll l, ll r, ll ql, ll qr, ll x) {
                if (ql <= l && r <= qr) {
                        add(v, x);
                        return;
                }
                if (qr <= l || r <= ql) {
                        return;
                }
                ll mid = (l + r) / 2;
                add(v * 2, l, mid, ql, qr, x);
                add(v * 2 + 1, mid, r, ql, qr, x);
                tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
        }
};

pair<bool, vector<tuple<ll, ll, ll>>> s3h(ll s, vector<pair<ll, ll>>& arr) {
        auto[bbbb, nnans] = s2h(s, arr);
        if (bbbb) {
                nnans.emplace_back(3'000'000'000, 3'000'000'000, 1);
                return make_pair(true, nnans);
        }
        sort(arr.begin(), arr.end());
        ll mpref = gmaxpreff(arr, s), msuff = gmaxsufff(arr, s);
        deque<pair<ll, ll>> b, m, e;
        for (auto[x, y] : arr) {
                if (x < mpref) {
                        b.emplace_back(x, y);
                }
                if (mpref <= x && x <= msuff) {
                        m.emplace_back(x, y);
                }
                if (x > msuff) {
                        e.emplace_back(x, y);
                }
        }
        deque<pair<ll, ll>> se = e;
        vector<ll> nums(1, (e.front().first - 1) - (b.back().first + 1));
        vector<ll> a = nums;
        deque<tuple<ll, ll, ll>> nmaxy, nminy;
        ll maxy = -INF32, miny = INF32;
        ll maxx = -INF32, minx = INF32;
        for (auto[x, y] : m) {
                assign_max(maxy, y);
                assign_min(miny, y);
                assign_max(maxx, x);
                assign_min(minx, x);
        }
        vector<tuple<ll, ll, ll>> ans;
        if (maxy - miny > s || maxx - minx > s) {
                return make_pair(false, ans);
        }
        nmaxy.emplace_back(0, 1, maxy);
        nminy.emplace_back(0, 1, miny);
        a[0] -= maxy - miny;
        while (!e.empty()) {
                ll nx = e.front().first;
                while (!e.empty() && e.front().first == nx) {
                        assign_max(maxy, e.front().second);
                        assign_min(miny, e.front().second);
                        e.pop_front();
                }
                nums.push_back(nums.back() + (e.empty() ? INF32 : e.front().first - 1) - (nx - 1));
                a.push_back(nums.back() - (maxy - miny));
                if (maxy > get<2>(nmaxy.back())) {
                        nmaxy.emplace_back((ll)nmaxy.size(), (ll)nmaxy.size(), maxy);
                }
                if (miny < get<2>(nminy.back())) {
                        nminy.emplace_back((ll)nminy.size(), (ll)nminy.size(), miny);
                }
                get<1>(nmaxy.back())++;
                get<1>(nminy.back())++;
        }
        segment_tree st(a.size());
        for (ll i = 0; i < a.size(); i++) {
                st.add(i, i + 1, a[i]);
        }
        bool bbb = true;
        ll sadd = 0;
        while (!b.empty()) {
                if (!bbb) {
                        ll nx = b.back().first;
                        ll nnmaxy = -INF32, nnminy = INF32;
                        while (!b.empty() && b.back().first == nx) {
                                m.push_back(b.back());
                                assign_max(nnmaxy, b.back().second);
                                assign_min(nnminy, b.back().second);
                                b.pop_back();
                        }
                        if (b.empty()) {
                                break;
                        }
                        ll nadd = (nx + 1) - (b.back().second + 1);
                        sadd += nadd;
                        st.add(0, a.size(), nadd);
                        while (get<2>(nmaxy.front()) < nnmaxy) {
                                auto[nl, nr, ny] = nmaxy.front();
                                nmaxy.pop_front();
                                if (nmaxy.empty() || get<2>(nmaxy.front()) > nnmaxy) {
                                        st.add(nl, nr, ny - nnmaxy);
                                        nmaxy.emplace_front(nl, nr, nnmaxy);
                                } else {
                                        auto[nnl, nnr, nny] = nmaxy.front();
                                        nmaxy.pop_front();
                                        st.add(nl, nr, ny - nny);
                                        nmaxy.emplace_front(nl, nnr, nny);
                                }
                        }
                        while (get<2>(nminy.front()) > nnminy) {
                                auto[nl, nr, ny] = nminy.front();
                                nminy.pop_front();
                                if (nminy.empty() || get<2>(nminy.front()) < nnminy) {
                                        st.add(nl, nr, nnminy - ny);
                                        nminy.emplace_front(nl, nr, nnminy);
                                } else {
                                        auto[nnl, nnr, nny] = nminy.front();
                                        nminy.pop_front();
                                        st.add(nl, nr, nny - ny);
                                        nminy.emplace_front(nl, nnr, nny);
                                }
                        }
                }
                bbb = false;
                auto[xx, nnum] = st.get();
                bool bb = false;
                if (nums[xx] + sadd == 0) {
                        st.add(0, 1, -1);
                        auto[nxx, nnnum] = st.get();
                        nnum = nnnum;
                        nxx = xx;
                        bb = true;
                }
                if (nums[xx] + sadd - nnum <= s) {
                        while (xx) {
                                ll nx = se.front().first;
                                while (!se.empty() && se.front().first == nx) {
                                        m.push_back(se.front());
                                        se.pop_front();
                                }
                                xx--;
                        }
                        ll minxb = INF32, maxxb = -INF32;
                        ll minyb = INF32, maxyb = -INF32;
                        ll minxm = INF32, maxxm = -INF32;
                        ll minym = INF32, maxym = -INF32;
                        ll minxe = INF32, maxxe = -INF32;
                        ll minye = INF32, maxye = -INF32;
                        for (auto[x, y] : b) {
                                assign_min(minxb, x);
                                assign_max(maxxb, x);
                                assign_min(minyb, y);
                                assign_max(maxyb, y);
                        }
                        for (auto[x, y] : m) {
                                assign_min(minxm, x);
                                assign_max(maxxm, x);
                                assign_min(minym, y);
                                assign_max(maxym, y);
                        }
                        for (auto[x, y] : e) {
                                assign_min(minxe, x);
                                assign_max(maxxe, x);
                                assign_min(minye, y);
                                assign_max(maxye, y);
                        }
                        ans.emplace_back(maxxb - s, maxyb - s, s);
                        ans.emplace_back(minxe, minye, s);
                        ll nadd = (maxym - minym) - (maxxm - minxm);
                        if (nadd < 0) {
                                cout << "YA DOLBOYOB" << endl;
                                exit(1);
                        }
                        ll caddst = minxm - (maxxb + 1);
                        ll na = min(nadd, caddst);
                        minxm -= na;
                        nadd -= na;
                        ll cadde = (minxe - 1) - maxxm;
                        na = min(nadd, caddst);
                        nadd -= na;
                        maxxm += na;
                        if (nadd > 0) {
                                cout << "YA EBLAN" << endl;
                                exit(1);
                        }
                        ans.emplace_back(minxm, minym, maxxm - minxm);
                        return make_pair(true, ans);
                }
                if (bb) {
                        st.add(0, 1, 1);
                }
        }
        return make_pair(false, ans);
}

pair<bool, vector<tuple<ll, ll, ll>>> s3v(ll s, vector<pair<ll, ll>>& arr) {
        for (auto&[x, y] : arr) {
                swap(x, y);
        }
        auto[b, ans] = s3h(s, arr);
        for (auto&[x, y] : arr) {
                swap(x, y);
        }
        if (b) {
                for (auto&[a, b, c] : ans) {
                        swap(a, b);
                }
        }
        return make_pair(b, ans);
}

void solve() {
        ll n, k;
        cin >> n >> k;
        vector<pair<ll, ll>> arr(n);
        for (ll i = 0; i < n; i++) {
                cin >> arr[i].first >> arr[i].second;
        }
        if (k == 1) {
                ll maxx = -INF32, minx = INF32;
                ll maxy = -INF32, miny = INF32;
                for (auto[x, y] : arr) {
                        assign_max(maxx, x);
                        assign_min(minx, x);
                        assign_max(maxy, y);
                        assign_min(miny, y);
                }
                cout << minx << ' ' << miny << ' ' << max(1ll, max(maxx - minx, maxy - miny)) << '\n';
                return;
        }
        if (k == 2) {
                ll l = 0, r = INF32;
                while (r - l > 1) {
                        ll mid = (l + r) / 2;
                        if (s2h(mid, arr).first || s2v(mid, arr).first) {
                                r = mid;
                        } else {
                                l = mid;
                        }
                }
                if (s2h(r, arr).first) {
                        for (auto[a, b, c] : s2h(r, arr).second) {
                                cout << a << ' ' << b << ' ' << c << '\n';
                        }
                } else {
                        for (auto[a, b, c] : s2v(r, arr).second) {
                                cout << a << ' ' << b << ' ' << c << '\n';
                        }
                }
                return;
        }
        ll l = 0, r = INF32;
        while (r - l > 1) {
                ll mid = (l + r) / 2;
                if (s3hl(mid, arr).first || s3hr(mid, arr).first || s3vl(mid, arr).first || s3vr(mid, arr).first || s3h(mid, arr).first || s3v(mid, arr).first) {
                        r = mid;
                } else {
                        l = mid;
                }
        }
        if (s3hl(r, arr).first) {
                for (auto[a, b, c] : s3hl(r, arr).second) {
                        cout << a << ' ' << b << ' ' << c << '\n';
                }
                return;
        }
        if (s3hr(r, arr).first) {
                for (auto[a, b, c] : s3hr(r, arr).second) {
                        cout << a << ' ' << b << ' ' << c << '\n';
                }
                return;
        }
        if (s3vl(r, arr).first) {
                for (auto[a, b, c] : s3vl(r, arr).second) {
                        cout << a << ' ' << b << ' ' << c << '\n';
                }
                return;
        }
        if (s3vr(r, arr).first) {
                for (auto[a, b, c] : s3vr(r, arr).second) {
                        cout << a << ' ' << b << ' ' << c << '\n';
                }
                return;
        }
        if (s3h(r, arr).first) {
                for (auto[a, b, c] : s3h(r, arr).second) {
                        cout << a << ' ' << b << ' ' << c << '\n';
                }
                return;
        }
        if (s3v(r, arr).first) {
                for (auto[a, b, c] : s3v(r, arr).second) {
                        cout << a << ' ' << b << ' ' << c << '\n';
                }
                return;
        }
}

int main() {
        if (IS_FILE) {
                freopen("", "r", stdin);
                freopen("", "w", stdout);
        }
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        ll t = 1;
        if (IS_TEST_CASES) {
                cin >> t;
        }
        for (ll i = 0; i < t; i++) {
                solve();
        }
}

Compilation message (stderr)

izvanzemaljci.cpp: In function 'std::pair<bool, std::vector<std::tuple<long long int, long long int, long long int> > > s3h(ll, std::vector<std::pair<long long int, long long int> >&)':
izvanzemaljci.cpp:430:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  430 |         for (ll i = 0; i < a.size(); i++) {
      |                        ~~^~~~~~~~~~
izvanzemaljci.cpp:532:28: warning: unused variable 'cadde' [-Wunused-variable]
  532 |                         ll cadde = (minxe - 1) - maxxm;
      |                            ^~~~~
izvanzemaljci.cpp: In function 'int main()':
izvanzemaljci.cpp:655:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  655 |                 freopen("", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~
izvanzemaljci.cpp:656:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  656 |                 freopen("", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...