Submission #890507

# Submission time Handle Problem Language Result Execution time Memory
890507 2023-12-21T10:59:02 Z LucaIlie Road Construction (JOI21_road_construction) C++17
100 / 100
4747 ms 17252 KB
#include <bits/stdc++.h>

#define int long long

using namespace std;

struct point {
    int x, y;
};

struct aa {
    int i, val;

    bool operator < ( const aa &a ) const {
        if ( val == a.val )
            return i < a.i;
        return val < a.val;
    }
};

const int MAX_N = 2.5e5;
const int INF = 2e9 + 1;
point v[MAX_N];

struct AIB {
    vector<int> pos;
    vector<int> aib;

    void init() {
        pos.clear();
        aib.clear();
    }

    void add( int i ) {
        pos.push_back( i );
    }

    void prep() {
        pos.push_back( -INF );
        sort( pos.begin(), pos.end() );
        pos.resize( unique( pos.begin(), pos.end() ) - pos.begin() );
        aib.resize( pos.size() );
    }

    void clear() {
        for ( int i = 0; i < aib.size(); i++ )
            aib[i] = 0;
    }

    void update( int i, int x ) {
        int l = 0, r = pos.size();
        while ( r - l > 1 ) {
            int p = (l + r) / 2;
            if ( pos[p] > i )
                r = p;
            else
                l = p;
        }

        i = l;
        while ( i < aib.size() ) {
            aib[i] += x;
            i += (i & -i);
        }
    }

    int query( int i ) {
        int l = 0, r = pos.size();
        while ( r - l > 1 ) {
            int p = (l + r) / 2;
            if ( pos[p] > i )
                r = p;
            else
                l = p;
        }
        i = l;
        int s = 0;
        while ( i > 0 ) {
            s += aib[i];
            i -= (i & -i);
        }
        return s;
    }
} points;

set<aa> s;

signed main() {
    ios_base::sync_with_stdio( false );
    cin.tie( NULL );
    cout.tie( NULL );

    int n, k;

    cin >> n >> k;
    for ( int i = 0; i < n; i++ )
        cin >> v[i].x >> v[i].y;

    sort( v, v + n, []( point a, point b ) {
        if ( a.x + a.y == b.x + b.y )
            return a.x - a.y < b.x - b.y;
        return a.x + a.y < b.x + b.y;
    } );


    points.init();
    for ( int i = 0; i < n; i++ )
        points.add( v[i].x - v[i].y );
    points.prep();

    long long ld = 0, rd = 2 * INF;
    while ( rd - ld > 1 ) {
        long long d = (ld + rd) / 2;

        long long pairs = 0;
        points.clear();
        int j = 0;
        for ( int i = 0; i < n; i++ ) {
            while ( (v[i].x + v[i].y) - (v[j].x + v[j].y) > d ) {
                points.update( v[j].x - v[j].y, -1 );
                j++;
            }

            pairs += points.query( v[i].x - v[i].y + d ) - points.query( v[i].x - v[i].y - d - 1 );

            points.update( v[i].x - v[i].y, 1 );
        }

        if ( pairs < k )
            ld = d;
        else
            rd = d;
    }
    long long d = ld;

    vector<long long> sol;

    int j = 0;
    for ( int i = 0; i < n; i++ ) {
        while ( (v[i].x + v[i].y) - (v[j].x + v[j].y) > d ) {
            s.erase( { j, v[j].x - v[j].y } );
            j++;
        }

        auto it = s.lower_bound( { -1, v[i].x - v[i].y - d } );
        while ( it != s.end() && it->val <= v[i].x - v[i].y + d ) {
            int j = it->i;
            sol.push_back( abs( v[i].x - v[j].x ) + abs( v[i].y - v[j].y ) );
            it++;
        }

        s.insert( { i, v[i].x - v[i].y } );
    }

    while ( sol.size() < k )
        sol.push_back( d + 1 );
    sort( sol.begin(), sol.end() );
    for( long long x: sol )
        cout << x << "\n";

    return 0;
}

Compilation message

road_construction.cpp: In member function 'void AIB::clear()':
road_construction.cpp:46:28: 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]
   46 |         for ( int i = 0; i < aib.size(); i++ )
      |                          ~~^~~~~~~~~~~~
road_construction.cpp: In member function 'void AIB::update(long long int, long long int)':
road_construction.cpp:61:19: 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]
   61 |         while ( i < aib.size() ) {
      |                 ~~^~~~~~~~~~~~
road_construction.cpp: In function 'int main()':
road_construction.cpp:155:24: 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]
  155 |     while ( sol.size() < k )
      |             ~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 38 ms 4976 KB Output is correct
2 Correct 38 ms 5160 KB Output is correct
3 Correct 34 ms 5068 KB Output is correct
4 Correct 34 ms 5076 KB Output is correct
5 Correct 36 ms 4016 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2713 ms 11592 KB Output is correct
2 Correct 2715 ms 14388 KB Output is correct
3 Correct 34 ms 5072 KB Output is correct
4 Correct 2721 ms 14284 KB Output is correct
5 Correct 2631 ms 14376 KB Output is correct
6 Correct 2632 ms 14644 KB Output is correct
7 Correct 2726 ms 14380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3976 ms 8412 KB Output is correct
2 Correct 4010 ms 8412 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2609 ms 8412 KB Output is correct
5 Correct 1237 ms 6496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3976 ms 8412 KB Output is correct
2 Correct 4010 ms 8412 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2609 ms 8412 KB Output is correct
5 Correct 1237 ms 6496 KB Output is correct
6 Correct 4016 ms 8408 KB Output is correct
7 Correct 4016 ms 13508 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 3965 ms 13348 KB Output is correct
11 Correct 2598 ms 11508 KB Output is correct
12 Correct 1223 ms 11724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 4976 KB Output is correct
2 Correct 38 ms 5160 KB Output is correct
3 Correct 34 ms 5068 KB Output is correct
4 Correct 34 ms 5076 KB Output is correct
5 Correct 36 ms 4016 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 1604 ms 10400 KB Output is correct
8 Correct 1620 ms 10588 KB Output is correct
9 Correct 34 ms 5088 KB Output is correct
10 Correct 1479 ms 9544 KB Output is correct
11 Correct 1336 ms 9480 KB Output is correct
12 Correct 415 ms 9584 KB Output is correct
13 Correct 540 ms 8456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 4976 KB Output is correct
2 Correct 38 ms 5160 KB Output is correct
3 Correct 34 ms 5068 KB Output is correct
4 Correct 34 ms 5076 KB Output is correct
5 Correct 36 ms 4016 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2713 ms 11592 KB Output is correct
8 Correct 2715 ms 14388 KB Output is correct
9 Correct 34 ms 5072 KB Output is correct
10 Correct 2721 ms 14284 KB Output is correct
11 Correct 2631 ms 14376 KB Output is correct
12 Correct 2632 ms 14644 KB Output is correct
13 Correct 2726 ms 14380 KB Output is correct
14 Correct 3976 ms 8412 KB Output is correct
15 Correct 4010 ms 8412 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2609 ms 8412 KB Output is correct
18 Correct 1237 ms 6496 KB Output is correct
19 Correct 4016 ms 8408 KB Output is correct
20 Correct 4016 ms 13508 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 3965 ms 13348 KB Output is correct
24 Correct 2598 ms 11508 KB Output is correct
25 Correct 1223 ms 11724 KB Output is correct
26 Correct 1604 ms 10400 KB Output is correct
27 Correct 1620 ms 10588 KB Output is correct
28 Correct 34 ms 5088 KB Output is correct
29 Correct 1479 ms 9544 KB Output is correct
30 Correct 1336 ms 9480 KB Output is correct
31 Correct 415 ms 9584 KB Output is correct
32 Correct 540 ms 8456 KB Output is correct
33 Correct 4721 ms 17252 KB Output is correct
34 Correct 4747 ms 17248 KB Output is correct
35 Correct 4119 ms 16484 KB Output is correct
36 Correct 1054 ms 15552 KB Output is correct
37 Correct 1043 ms 15504 KB Output is correct
38 Correct 1618 ms 14720 KB Output is correct