Submission #890476

# Submission time Handle Problem Language Result Execution time Memory
890476 2023-12-21T10:01:53 Z LucaIlie Road Construction (JOI21_road_construction) C++17
0 / 100
10000 ms 257084 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;

struct point {
    int x, y;
};

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

struct AIB {
    vector<int> pos;
    vector<ordered_set> 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].clear();
    }

    void update( int i, long long 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].insert( x );
            i += (i & -i);
        }
    }

    int query( int i, long long j ) {
        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].order_of_key( j + 1 );
            i -= (i & -i);
        }
        return s;
    }
} leftPoints, rightPoints;

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 == b.x )
            return a.y < b.y;
        return a.x < b.x;
    } );


    leftPoints.init();
    rightPoints.init();
    for ( int i = 0; i < n; i++ ) {
        leftPoints.add( v[i].y );
        rightPoints.add( v[i].y );
    }
    leftPoints.prep();
    rightPoints.prep();

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

        long long pairs = 0;
        leftPoints.clear();
        rightPoints.clear();
        for ( int i = 0; i < n; i++ ) {
            pairs += leftPoints.query( v[i].y, d - (v[i].x + v[i].y) );
            pairs += rightPoints.query( MAX_X, d - (v[i].x - v[i].y) ) - rightPoints.query( v[i].y, d - (v[i].x - v[i].y) );

            leftPoints.update( v[i].y, -v[i].x - v[i].y );
            rightPoints.update( v[i].y, -v[i].x + v[i].y );
        }

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


    vector<long long> sol;
    for ( int i = 0; i < n; i++ ) {
        for ( int j = i + 1; j < n; j++ ) {
            if ( abs( v[i].x - v[j].x ) + abs( v[i].y - v[j].y ) < d )
                sol.push_back( abs( v[i].x - v[j].x ) + abs( v[i].y - v[j].y ) );
        }
    }

    while ( sol.size() < k )
        sol.push_back( d );
    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:40:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<__gnu_pbds::tree<int, __gnu_pbds::null_type, std::less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         for ( int i = 0; i < aib.size(); i++ )
      |                          ~~^~~~~~~~~~~~
road_construction.cpp: In member function 'void AIB::update(int, long long int)':
road_construction.cpp:55:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<__gnu_pbds::tree<int, __gnu_pbds::null_type, std::less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |         while ( i < aib.size() ) {
      |                 ~~^~~~~~~~~~~~
road_construction.cpp: In function 'int main()':
road_construction.cpp:138:24: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  138 |     while ( sol.size() < k )
      |             ~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 101 ms 7796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 10073 ms 27912 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 10038 ms 257084 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 10038 ms 257084 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 101 ms 7796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 101 ms 7796 KB Output isn't correct
2 Halted 0 ms 0 KB -