Submission #797058

# Submission time Handle Problem Language Result Execution time Memory
797058 2023-07-29T05:30:11 Z vjudge1 Road Construction (JOI21_road_construction) C++17
100 / 100
3179 ms 359420 KB
#include <bits/stdc++.h>

#define fi first
#define se second

const int N = 500500;
const int mod = 1e9 + 7;

using namespace std;

int n;
int id[N];
int t[N];
pair<long long, long long> a[N];
vector<pair<long long, int>> v;

void upd(int x, int g)
{
        while(x < N) {
                t[x] += g;
                x += x & -x;
        }
}

int get_c(int x)
{
        int res = 0;
        while(x > 0) {
                res += t[x];
                x -= x & -x;
        }
        return res;
}

int get_f(int k)
{
        int res = 0;
        for (int i = 20; i >= 0; i--) {
                if (res + (1 << i) >= N) {
                        continue;
                } else if (t[res + (1 << i)] < k) {
                        res += (1 << i);
                        k -= t[res];
                }
        }
        return res + 1;
}

long long get(long long D)
{
        long long res = 0;
        for(int i = 0; i < N; i++) t[i] = 0;

        for(int i = 1, j = 1; i <= n; i++) {
                if(i > 1){
                        upd(id[i - 1], 1);
                }
                while(j < i && a[i].fi - a[j].fi > D) {
                        upd(id[j], -1);
                        j += 1;
                }
                int l = lower_bound(v.begin(), v.end(), make_pair(a[i].se - D, 0)) - v.begin();
                int r = lower_bound(v.begin(), v.end(), make_pair(a[i].se + D + 1, 0)) - v.begin() - 1;
                if(l <= r) {
                        res += get_c(v[r].se) - get_c(v[l].se - 1);
                }
        }
        return res;
}

queue<int> Q[N];

void add_res(vector<long long> &res, queue<int> q, int i)
{
        while (!q.empty()) {
                int j = q.front();
                q.pop();
                res.push_back(max(abs(a[i].fi - a[j].fi), abs(a[i].se - a[j].se)));
        }
}

vector<long long> solve(long long D)
{
        vector<long long> res;
        for(int i = 0; i < N; i++) t[i] = 0;

        for(int i = 1, j = 1; i <= n; i++) {
                if(i > 1){
                        upd(id[i - 1], 1);
                        Q[id[i - 1]].push(i - 1);
                }
                while(j < i && a[i].fi - a[j].fi > D) {
                        upd(id[j], -1);
                        Q[id[j]].pop();
                        j += 1;
                }
                int l = lower_bound(v.begin(), v.end(), make_pair(a[i].se - D, 0)) - v.begin();
                int r = lower_bound(v.begin(), v.end(), make_pair(a[i].se + D + 1, 0)) - v.begin() - 1;
                if(l <= r) {
                        int tr = get_c(v[r].se);
                        int tl = get_c(v[l].se - 1);
                        while (tl < tr) {
                                int pos = get_f(tl + 1);
                                add_res(res, Q[pos], i);
                                tl += Q[pos].size();
                                assert(pos <= v[r].se);
                        }
                }
        }
        return res;
}

int main() {
        #ifdef zxc
            freopen("input.txt", "r", stdin);
            freopen("output.txt", "w", stdout);
        #endif
        ios_base::sync_with_stdio(0);

        long long k;
        cin >> n >> k;
        for(int i = 1; i <= n; i++) {
                long long x, y;
                cin >> x >> y;

                a[i] = {x + y, x - y};
        }
        sort(a + 1, a + n + 1);

        v.push_back({-1e11, 0});
        v.push_back({1e11, 0});

        for(int i = 1; i <= n; i++) {
                v.push_back({a[i].se, i});
        }
        sort(v.begin(), v.end());
        for(int i = 0, j = 0; i < v.size(); i++) {
                if(i > 0 && v[i].fi > v[i - 1].fi) {
                        j += 1;
                }
                id[v[i].se] = j;
                v[i].se = j;
        }

        long long l = 0, r = 1e10;
        while(l < r) {
                long long m = (l + r) / 2;
                if(get(m + 1) < k) {
                        l = m + 1;
                } else {
                        r = m;
                }
        }

        auto res = solve(l);
        while (res.size() < k) {
                res.push_back(l + 1);
        }
        sort(res.begin(), res.end());
        for (auto x: res) {
                cout << x << "\n";
        }
}

Compilation message

road_construction.cpp: In function 'int main()':
road_construction.cpp:137:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |         for(int i = 0, j = 0; i < v.size(); i++) {
      |                               ~~^~~~~~~~~~
road_construction.cpp:156:27: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  156 |         while (res.size() < k) {
      |                ~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 239 ms 343864 KB Output is correct
2 Correct 236 ms 343864 KB Output is correct
3 Correct 230 ms 343992 KB Output is correct
4 Correct 237 ms 344028 KB Output is correct
5 Correct 236 ms 342804 KB Output is correct
6 Correct 185 ms 339308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1495 ms 353252 KB Output is correct
2 Correct 1533 ms 353188 KB Output is correct
3 Correct 229 ms 343768 KB Output is correct
4 Correct 1483 ms 353048 KB Output is correct
5 Correct 1474 ms 353276 KB Output is correct
6 Correct 1635 ms 353268 KB Output is correct
7 Correct 1454 ms 352420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2857 ms 348084 KB Output is correct
2 Correct 2894 ms 348080 KB Output is correct
3 Correct 165 ms 339200 KB Output is correct
4 Correct 1443 ms 348132 KB Output is correct
5 Correct 2450 ms 348116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2857 ms 348084 KB Output is correct
2 Correct 2894 ms 348080 KB Output is correct
3 Correct 165 ms 339200 KB Output is correct
4 Correct 1443 ms 348132 KB Output is correct
5 Correct 2450 ms 348116 KB Output is correct
6 Correct 2985 ms 348136 KB Output is correct
7 Correct 2871 ms 353184 KB Output is correct
8 Correct 166 ms 339136 KB Output is correct
9 Correct 171 ms 339164 KB Output is correct
10 Correct 2870 ms 353208 KB Output is correct
11 Correct 1445 ms 351032 KB Output is correct
12 Correct 2424 ms 353476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 239 ms 343864 KB Output is correct
2 Correct 236 ms 343864 KB Output is correct
3 Correct 230 ms 343992 KB Output is correct
4 Correct 237 ms 344028 KB Output is correct
5 Correct 236 ms 342804 KB Output is correct
6 Correct 185 ms 339308 KB Output is correct
7 Correct 1288 ms 346736 KB Output is correct
8 Correct 1273 ms 348876 KB Output is correct
9 Correct 234 ms 343948 KB Output is correct
10 Correct 1206 ms 348028 KB Output is correct
11 Correct 1181 ms 348104 KB Output is correct
12 Correct 772 ms 348816 KB Output is correct
13 Correct 758 ms 347904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 239 ms 343864 KB Output is correct
2 Correct 236 ms 343864 KB Output is correct
3 Correct 230 ms 343992 KB Output is correct
4 Correct 237 ms 344028 KB Output is correct
5 Correct 236 ms 342804 KB Output is correct
6 Correct 185 ms 339308 KB Output is correct
7 Correct 1495 ms 353252 KB Output is correct
8 Correct 1533 ms 353188 KB Output is correct
9 Correct 229 ms 343768 KB Output is correct
10 Correct 1483 ms 353048 KB Output is correct
11 Correct 1474 ms 353276 KB Output is correct
12 Correct 1635 ms 353268 KB Output is correct
13 Correct 1454 ms 352420 KB Output is correct
14 Correct 2857 ms 348084 KB Output is correct
15 Correct 2894 ms 348080 KB Output is correct
16 Correct 165 ms 339200 KB Output is correct
17 Correct 1443 ms 348132 KB Output is correct
18 Correct 2450 ms 348116 KB Output is correct
19 Correct 2985 ms 348136 KB Output is correct
20 Correct 2871 ms 353184 KB Output is correct
21 Correct 166 ms 339136 KB Output is correct
22 Correct 171 ms 339164 KB Output is correct
23 Correct 2870 ms 353208 KB Output is correct
24 Correct 1445 ms 351032 KB Output is correct
25 Correct 2424 ms 353476 KB Output is correct
26 Correct 1288 ms 346736 KB Output is correct
27 Correct 1273 ms 348876 KB Output is correct
28 Correct 234 ms 343948 KB Output is correct
29 Correct 1206 ms 348028 KB Output is correct
30 Correct 1181 ms 348104 KB Output is correct
31 Correct 772 ms 348816 KB Output is correct
32 Correct 758 ms 347904 KB Output is correct
33 Correct 3179 ms 359080 KB Output is correct
34 Correct 3172 ms 359000 KB Output is correct
35 Correct 3052 ms 358484 KB Output is correct
36 Correct 1881 ms 359420 KB Output is correct
37 Correct 1913 ms 359384 KB Output is correct
38 Correct 2090 ms 357848 KB Output is correct