답안 #956163

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
956163 2024-04-01T08:16:36 Z vjudge1 Road Construction (JOI21_road_construction) C++17
6 / 100
3658 ms 20540 KB
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #include <x86intrin.h>

#include <bits/stdc++.h>
#include <chrono>
#include <random>

// @author: Vlapos

namespace operators
{
    template <typename T1, typename T2>
    std::istream &operator>>(std::istream &in, std::pair<T1, T2> &x)
    {
        in >> x.first >> x.second;
        return in;
    }

    template <typename T1, typename T2>
    std::ostream &operator<<(std::ostream &out, std::pair<T1, T2> x)
    {
        out << x.first << " " << x.second;
        return out;
    }

    template <typename T1>
    std::istream &operator>>(std::istream &in, std::vector<T1> &x)
    {
        for (auto &i : x)
            in >> i;
        return in;
    }

    template <typename T1>
    std::ostream &operator<<(std::ostream &out, std::vector<T1> &x)
    {
        for (auto &i : x)
            out << i << " ";
        return out;
    }

    template <typename T1>
    std::ostream &operator<<(std::ostream &out, std::set<T1> &x)
    {
        for (auto &i : x)
            out << i << " ";
        return out;
    }

    template <typename T1>
    std::ostream &operator<<(std::ostream &out, std::multiset<T1> &x)
    {
        for (auto &i : x)
            out << i << " ";
        return out;
    }
}

// name spaces
using namespace std;
using namespace operators;
// end of name spaces

// defines
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
#define uint unsigned int
#define all(vc) vc.begin(), vc.end()
// end of defines

// usefull stuff

void boost()
{
    ios_base ::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

inline int getbit(int &x, int &bt) { return (x >> bt) & 1; }

const int dx4[4] = {-1, 0, 0, 1};
const int dy4[4] = {0, -1, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy8[8] = {-1, -0, 1, -1, 1, -1, 0, 1};

const ll INF = (1e18) + 500;
const int BIG = (1e9) * 2 + 100;
const int MAXN = (1e5) + 5;
const int MOD7 = (1e9) + 7;
const int MOD9 = (1e9) + 9;
const uint MODFFT = 998244353;

#define int ll

struct segTreeSum
{
    vector<ll> tree;
    int sz;

    void init(int n)
    {
        sz = n;
        tree.resize(2 * sz);
    }

    void build(vector<int> &a)
    {
        init(a.size());
        for (int i = 0; i < a.size(); ++i)
            tree[i + sz] = a[sz];

        for (int i = sz - 1; i > 0; --i)
            tree[i] = tree[i << 1] + tree[(i << 1) + 1];
    }

    int get(int l, int r) // [l, r)
    {
        l += sz;
        r += sz;
        int res = 0;

        while (l < r)
        {
            if (l & 1)
                res += tree[l++];
            if (r & 1)
                res += tree[--r];
            l >>= 1;
            r >>= 1;
        }

        return res;
    }

    void add(int pos, int val)
    {
        pos += sz;
        tree[pos] += val;
        pos >>= 1;
        while (pos)
        {
            tree[pos] = tree[pos << 1] + tree[(pos << 1) + 1];
            pos >>= 1;
        }
    }
};

struct test
{
    int n, k;
    vector<pii> els;
    vector<int> vals, rval;
    segTreeSum tree;

    int check(int x)
    {
        int u = 0;
        ll cnt = 0;
        for (int i = 0; i < n; ++i)
        {
            while (els[u].f <= els[i].f + x and u < n)
                tree.add(rval[u++], 1);
            tree.add(rval[i], -1);
            cnt += tree.get(0, upper_bound(all(vals), els[i].s + x) - vals.begin()) - tree.get(0, lower_bound(all(vals), els[i].s - x) - vals.begin());
        }

        for (int i = 0; i < 2 * vals.size(); ++i)
            tree.tree[i] = 0;
        return cnt;
    }

    void solve(int testcase)
    {
        boost();

        cin >> n >> k;
        for (int i = 0; i < n; ++i)
        {
            int x, y;
            cin >> x >> y;
            els.pb({x + y, x - y});
            vals.pb(x - y);
        }
        vals.pb(-BIG);
        vals.pb(BIG);
        sort(all(vals));
        sort(all(els));
        rval.resize(n);
        for (int i = 0; i < n; ++i)
            rval[i] = lower_bound(all(vals), els[i].s) - vals.begin();
        vals.erase(unique(all(vals)), vals.end());
        tree.init(vals.size());


        int L = -1, R = 4e9 + 10;
        while (R - L > 1)
        {
            int M = (L + R) >> 1;
            if (check(M) < k)
                L = M;
            else
                R = M;
        }
        vector<int> res;
        {
            multiset<pii> cur;
            int u = 0, x = L;
            for (int i = 0; i < n; ++i)
            {
                while (els[u].f <= els[i].f + x and u < n)
                {
                    cur.insert({els[u].s, els[u].f});
                    ++u;
                }
                cur.erase(cur.find({els[i].s, els[i].f}));
                auto it = cur.lower_bound({els[i].s - x, -BIG});
                while (it != cur.end() and abs(it->f - els[i].s) <= x)
                {
                    res.pb(max(abs(it->f - els[i].s), abs(it->s - els[i].f)));
                    it = next(it);
                }
            }
        }

        sort(all(res));
        cout << res;
        for (int i = 0; res.size() + i < k; ++i)
            cout << L + 1 << ' ';
    }
};

main()
{
    boost();
    int q = 1;
    // cin >> q;
    for (int i = 0; i < q; i++)
    {
        test t;
        t.solve(i);
    }
    return 0;
}
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//
//                                                                                    //
//                               Coded by Der_Vlἀpos                                  //
//                                                                                    //
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//

Compilation message

road_construction.cpp: In member function 'void segTreeSum::build(std::vector<long long int>&)':
road_construction.cpp:117:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |         for (int i = 0; i < a.size(); ++i)
      |                         ~~^~~~~~~~~~
road_construction.cpp: In member function 'long long int test::check(long long int)':
road_construction.cpp:175:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  175 |         for (int i = 0; i < 2 * vals.size(); ++i)
      |                         ~~^~~~~~~~~~~~~~~~~
road_construction.cpp: In member function 'void test::solve(long long int)':
road_construction.cpp:235:40: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'long long int' [-Wsign-compare]
  235 |         for (int i = 0; res.size() + i < k; ++i)
      |                         ~~~~~~~~~~~~~~~^~~
road_construction.cpp: At global scope:
road_construction.cpp:240:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  240 | main()
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 5052 KB Output is correct
2 Correct 37 ms 5052 KB Output is correct
3 Correct 34 ms 5312 KB Output is correct
4 Correct 35 ms 5312 KB Output is correct
5 Incorrect 33 ms 4032 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1811 ms 20444 KB Output is correct
2 Correct 1800 ms 20444 KB Output is correct
3 Correct 31 ms 5060 KB Output is correct
4 Correct 1878 ms 20180 KB Output is correct
5 Correct 2067 ms 20356 KB Output is correct
6 Correct 2032 ms 20540 KB Output is correct
7 Correct 1671 ms 20056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3658 ms 17376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3658 ms 17376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 5052 KB Output is correct
2 Correct 37 ms 5052 KB Output is correct
3 Correct 34 ms 5312 KB Output is correct
4 Correct 35 ms 5312 KB Output is correct
5 Incorrect 33 ms 4032 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 5052 KB Output is correct
2 Correct 37 ms 5052 KB Output is correct
3 Correct 34 ms 5312 KB Output is correct
4 Correct 35 ms 5312 KB Output is correct
5 Incorrect 33 ms 4032 KB Output isn't correct
6 Halted 0 ms 0 KB -