답안 #890470

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
890470 2023-12-21T09:53:22 Z LucaIlie Road Construction (JOI21_road_construction) C++17
0 / 100
572 ms 112280 KB
#include <bits/stdc++.h>

using namespace std;

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<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() );
        for ( int i = 0; i < pos.size(); i++ )
            aib.push_back( 0 );
    }

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

    void update( long long 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;
        }
        if ( pos[l] != i )
            exit( 1 );
        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;
    }
};

struct AIB2D {
    vector<int> pos;
    vector<AIB> aib;
    vector<point> pts;
    AIB emptyAIB;

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

    void add( int i, int j ) {
        pts.push_back( { i, j } );
        pos.push_back( i );
        emptyAIB.init();
    }

    void prep() {
        pos.push_back( -INF );
        sort( pos.begin(), pos.end() );
        pos.resize( unique( pos.begin(), pos.end() ) - pos.begin() );
        for ( int i = 0; i < pos.size(); i++ )
            aib.push_back( emptyAIB );
        for ( point q: pts ) {
            int l = 0, r = pos.size();
            while ( r - l > 1 ) {
                int p = (l + r) / 2;
                if ( pos[p] > q.x )
                    r = p;
                else
                    l = p;
            }
            int i = l;
            while ( i < aib.size() ) {
                aib[i].add( q.y );
                i += (i & -i);
            }
        }
        for ( int i = 0; i < pos.size(); i++ )
            aib[i].prep();
    }

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

    void update( int i, int j, 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].update( j, 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].query( j );
            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, -v[i].x - v[i].y );
        rightPoints.add( v[i].y, -v[i].x + v[i].y );
    }
    leftPoints.prep();
    rightPoints.prep();

    long long ld = 0, rd = 2 * 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, 1 );
            rightPoints.update( v[i].y, -v[i].x + v[i].y, 1 );
        }

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

    vector<long long> sol;
    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::prep()':
road_construction.cpp:31:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |         for ( int i = 0; i < pos.size(); i++ )
      |                          ~~^~~~~~~~~~~~
road_construction.cpp: In member function 'void AIB::clear()':
road_construction.cpp:36:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |         for ( int i = 0; i < aib.size(); i++ )
      |                          ~~^~~~~~~~~~~~
road_construction.cpp: In member function 'void AIB::update(long long int, int)':
road_construction.cpp:52:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |         while ( i < aib.size() ) {
      |                 ~~^~~~~~~~~~~~
road_construction.cpp: In member function 'void AIB2D::prep()':
road_construction.cpp:98:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |         for ( int i = 0; i < pos.size(); i++ )
      |                          ~~^~~~~~~~~~~~
road_construction.cpp:110:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<AIB>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |             while ( i < aib.size() ) {
      |                     ~~^~~~~~~~~~~~
road_construction.cpp:115:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |         for ( int i = 0; i < pos.size(); i++ )
      |                          ~~^~~~~~~~~~~~
road_construction.cpp: In member function 'void AIB2D::clear()':
road_construction.cpp:120:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<AIB>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |         for ( int i = 0; i < aib.size(); i++ )
      |                          ~~^~~~~~~~~~~~
road_construction.cpp: In member function 'void AIB2D::update(int, int, int)':
road_construction.cpp:134:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<AIB>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |         while ( i < aib.size() ) {
      |                 ~~^~~~~~~~~~~~
road_construction.cpp: In function 'int main()':
road_construction.cpp:186:30: warning: integer overflow in expression of type 'int' results in '-294967294' [-Woverflow]
  186 |     long long ld = 0, rd = 2 * INF;
      |                            ~~^~~~~
road_construction.cpp:209:24: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  209 |     while ( sol.size() < k )
      |             ~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 5576 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 88 ms 19008 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 572 ms 112280 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 572 ms 112280 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 5576 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 5576 KB Output isn't correct
2 Halted 0 ms 0 KB -