Submission #387752

# Submission time Handle Problem Language Result Execution time Memory
387752 2021-04-09T07:19:23 Z abc864197532 Road Construction (JOI21_road_construction) C++17
100 / 100
5979 ms 16240 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define test(x) cout << "Line(" << __LINE__ << ") " #x << ' ' << x << endl
#define printv(x) {\
	for (auto i : x) cout << i << ' ';\
	cout << endl;\
}
#define pii pair <int, int>
#define pll pair <lli, lli>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
template<typename A, typename B>
ostream& operator << (ostream& o, pair<A, B> a){
    return o << a.X << ' ' << a.Y;
}
template<typename A, typename B>
istream& operator >> (istream& o, pair<A, B> &a){
    return o >> a.X >> a.Y;
}
const int mod = 1e9 + 7, abc = 864197532, N = 300001, K = 111;

struct Pt {
    lli x, y, idx;
    Pt (lli _x = 0, lli _y = 0) : x(_x), y(_y) {}
    bool operator < (const Pt &o) const {
        return x < o.x;
    }
};

struct cmp {
    bool operator () (const Pt &a, const Pt &b) const {
        if (a.y == b.y) return a.idx < b.idx;
        return a.y < b.y;
    }
};

int bit[N];

void reset() {
    for (int i = 0; i < N; ++i) bit[i] = 0;
}

void add(int p, int v) {
    for (int i = p; i < N; i += i & (-i)) bit[i] += v;
}

int query(int p) {
    int ans = 0;
    for (int i = p; i > 0; i -= i & (-i)) ans += bit[i];
    return ans;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector <Pt> a;
    for (int i = 0, x, y; i < n; ++i) {
        cin >> x >> y;
        a.pb(Pt(x + y, x - y));
        a.back().idx = i;
    }
    vector <lli> y_idx;
    for (int i = 0; i < n; ++i) y_idx.pb(a[i].y);
    sort(all(y_idx));
    y_idx.resize(unique(all(y_idx)) - y_idx.begin());
    auto get = [&](lli x) {
        return lower_bound(all(y_idx), x) - y_idx.begin() + 5;
    };
    sort(all(a));
    auto chk = [&](lli d) {
        /*
         * 2 4 8 9 15
         * 4 9
         */
        reset();
        lli ans = 0;
        for (int i = 0, j = 0; i < n; ++i) {
            while (j < n && a[j].x + d < a[i].x) add(get(a[j++].y), -1);
            ans += query(get(a[i].y + d + 1) - 1) - query(get(a[i].y - d) - 1);
            add(get(a[i].y), 1);
        }
        return ans;
    };
    lli l = 0, r = 4e9 + 87;
    while (r - l > 1) {
        ((chk(l + r >> 1)) >= k ? r : l) = l + r >> 1;
    }
    lli d = l;
    vector <lli> ans;
    set <Pt, cmp> s;
    for (int i = 0, j = 0; i < n; ++i) {
        while (j < n && a[j].x + d < a[i].x) s.erase(a[j++]);
        for (auto it = s.lower_bound(Pt(0, a[i].y - d)); it != s.end() && it->y <= a[i].y + d; ++it) {
            ans.pb(max(a[i].x - it->x, abs(it->y - a[i].y)));
        }
        s.insert(a[i]);
    }
    sort(all(ans));
    for (int i = 0; i < k; ++i) {
        if (i < ans.size()) cout << ans[i] << '\n';
        else cout << r << '\n';
    }
}

Compilation message

road_construction.cpp: In function 'int main()':
road_construction.cpp:94:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   94 |         ((chk(l + r >> 1)) >= k ? r : l) = l + r >> 1;
      |               ~~^~~
road_construction.cpp:94:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   94 |         ((chk(l + r >> 1)) >= k ? r : l) = l + r >> 1;
      |                                            ~~^~~
road_construction.cpp:108:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |         if (i < ans.size()) cout << ans[i] << '\n';
      |             ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 68 ms 6144 KB Output is correct
2 Correct 67 ms 6156 KB Output is correct
3 Correct 76 ms 6240 KB Output is correct
4 Correct 60 ms 6308 KB Output is correct
5 Correct 63 ms 5040 KB Output is correct
6 Correct 13 ms 1536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2441 ms 15516 KB Output is correct
2 Correct 2444 ms 15468 KB Output is correct
3 Correct 55 ms 6188 KB Output is correct
4 Correct 2463 ms 15288 KB Output is correct
5 Correct 2377 ms 15724 KB Output is correct
6 Correct 2408 ms 15604 KB Output is correct
7 Correct 2359 ms 14744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5377 ms 11268 KB Output is correct
2 Correct 5423 ms 11372 KB Output is correct
3 Correct 7 ms 1484 KB Output is correct
4 Correct 2424 ms 11264 KB Output is correct
5 Correct 2591 ms 11300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5377 ms 11268 KB Output is correct
2 Correct 5423 ms 11372 KB Output is correct
3 Correct 7 ms 1484 KB Output is correct
4 Correct 2424 ms 11264 KB Output is correct
5 Correct 2591 ms 11300 KB Output is correct
6 Correct 5319 ms 11304 KB Output is correct
7 Correct 5440 ms 11300 KB Output is correct
8 Correct 7 ms 1484 KB Output is correct
9 Correct 7 ms 1484 KB Output is correct
10 Correct 5358 ms 11352 KB Output is correct
11 Correct 2393 ms 11352 KB Output is correct
12 Correct 2636 ms 11288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 6144 KB Output is correct
2 Correct 67 ms 6156 KB Output is correct
3 Correct 76 ms 6240 KB Output is correct
4 Correct 60 ms 6308 KB Output is correct
5 Correct 63 ms 5040 KB Output is correct
6 Correct 13 ms 1536 KB Output is correct
7 Correct 2164 ms 11240 KB Output is correct
8 Correct 2189 ms 11124 KB Output is correct
9 Correct 60 ms 6248 KB Output is correct
10 Correct 2019 ms 10428 KB Output is correct
11 Correct 1898 ms 10304 KB Output is correct
12 Correct 894 ms 7700 KB Output is correct
13 Correct 973 ms 9616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 6144 KB Output is correct
2 Correct 67 ms 6156 KB Output is correct
3 Correct 76 ms 6240 KB Output is correct
4 Correct 60 ms 6308 KB Output is correct
5 Correct 63 ms 5040 KB Output is correct
6 Correct 13 ms 1536 KB Output is correct
7 Correct 2441 ms 15516 KB Output is correct
8 Correct 2444 ms 15468 KB Output is correct
9 Correct 55 ms 6188 KB Output is correct
10 Correct 2463 ms 15288 KB Output is correct
11 Correct 2377 ms 15724 KB Output is correct
12 Correct 2408 ms 15604 KB Output is correct
13 Correct 2359 ms 14744 KB Output is correct
14 Correct 5377 ms 11268 KB Output is correct
15 Correct 5423 ms 11372 KB Output is correct
16 Correct 7 ms 1484 KB Output is correct
17 Correct 2424 ms 11264 KB Output is correct
18 Correct 2591 ms 11300 KB Output is correct
19 Correct 5319 ms 11304 KB Output is correct
20 Correct 5440 ms 11300 KB Output is correct
21 Correct 7 ms 1484 KB Output is correct
22 Correct 7 ms 1484 KB Output is correct
23 Correct 5358 ms 11352 KB Output is correct
24 Correct 2393 ms 11352 KB Output is correct
25 Correct 2636 ms 11288 KB Output is correct
26 Correct 2164 ms 11240 KB Output is correct
27 Correct 2189 ms 11124 KB Output is correct
28 Correct 60 ms 6248 KB Output is correct
29 Correct 2019 ms 10428 KB Output is correct
30 Correct 1898 ms 10304 KB Output is correct
31 Correct 894 ms 7700 KB Output is correct
32 Correct 973 ms 9616 KB Output is correct
33 Correct 5979 ms 16240 KB Output is correct
34 Correct 5912 ms 16236 KB Output is correct
35 Correct 5401 ms 15600 KB Output is correct
36 Correct 2390 ms 13036 KB Output is correct
37 Correct 2358 ms 13344 KB Output is correct
38 Correct 2761 ms 14824 KB Output is correct