Submission #434705

# Submission time Handle Problem Language Result Execution time Memory
434705 2021-06-21T15:12:45 Z blue Road Construction (JOI21_road_construction) C++17
5 / 100
10000 ms 55236 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;

/*
1. Normalize Y coordinates
Binary search for K'th smallest distance:

WLOG x[1] <= ... <= x[N]
The cost for the point i and the point j is (x[j] - x[i]) + |y[j] - y[i]|.
                    y[i] < y[j]             (x[j] + y[j]) - (x[i] + y[i])    Segtree S
                    y[i] >= y[j]            (x[j] - y[j]) - (x[i] - y[i])    Segtree T

Maintain two segment trees: In one store the x[i] + y[i] values, in the other store the x[i] - y[i]
values.
For each point compute the normalized Y coordinate.
*/

const long long INF = 1'000'000'000'000;
const int maxN = 250000;

struct segtree
{
    int l;
    int r;
    pair<int, long long> v; //range max

    segtree* left = NULL;
    segtree* right = NULL;

    segtree()
    {
        ;
    }

    segtree(int L, int R)
    {
        l = L;
        r = R;
        v = make_pair(l, -INF);
        if(l == r) return;
        int m = (l+r)/2;
        left = new segtree(l, m);
        right = new segtree(m+1, r);
    }

    void update(int I, long long V)
    {
        if(I < l || r < I) return;
        else if(l == r) v = make_pair(l, V);
        else
        {
            left->update(I, V);
            right->update(I, V);

            pair<int, long long> LP = left->v;
            pair<int, long long> RP = right->v;

            if(LP.second > RP.second) v = LP;
            else v = RP;
        }
    }

    pair<int, long long> rangemax(int L, int R)
    {
        if(R < l || r < L) return make_pair(-1, -INF);
        else if(L <= l && r <= R) return v;
        else
        {
            pair<int, long long> LP = left->rangemax(L, R);
            pair<int, long long> RP = right->rangemax(L, R);

            if(LP.second > RP.second) return LP;
            else return RP;
        }
    }
};

int N, K;

struct point
{
    long long X;
    long long Y;
    int Yindex;
};

vector<point> P;

int main()
{
    cin >> N >> K;

    P = vector<point>(N);

    for(int i = 0; i < N; i++) cin >> P[i].X >> P[i].Y;

    sort(P.begin(), P.end(), [] (point A, point B)
    {
        return A.Y < B.Y;
    });

    for(int i = 0; i < N; i++) P[i].Yindex = i+1;

    sort(P.begin(), P.end(), [] (point A, point B)
    {
        return A.X < B.X;
    });

    segtree S(1, N), T(1, N);

    long long lo = 0, hi = 4'000'000'000LL;
    vector<long long> res;
    while(1)
    {
        long long Kval = (lo+hi)/2;
        res.clear();
        for(int i = 1; i <= N; i++)
        {
            S.update(i, -INF);
            T.update(i, -INF);
        }

        // cerr << "searching " << lo << ' ' << hi << '\n';


        for(point p:P)
        {
            vector< pair<int, long long> > updates;

            while(res.size() < K+1)
            {
                pair<int, long long> Q = S.rangemax(1, p.Yindex);
                if(Q.second <= -INF) break;

                if((p.X + p.Y) - Q.second > Kval) break;

                res.push_back((p.X + p.Y) - Q.second);

                S.update(Q.first, -INF);

                updates.push_back(Q);
            }

            for(pair<int, long long> u: updates)
                S.update(u.first, u.second);

            updates.clear();






            while(res.size() < K+1)
            {
                pair<int, long long> Q = T.rangemax(p.Yindex+1, N);
                if(Q.second <= -INF) break;

                if((p.X - p.Y) - Q.second > Kval) break;

                res.push_back((p.X - p.Y) - Q.second);

                T.update(Q.first, -INF);

                updates.push_back(Q);
            }

            for(pair<int, long long> u: updates)
                T.update(u.first, u.second);

            updates.clear();






            S.update(p.Yindex, p.X + p.Y);
            T.update(p.Yindex, p.X - p.Y);


        }

        sort(res.begin(), res.end());
        if(lo == hi)
        {
            while(res.size() > K) res.pop_back();
            break;
        }
        else if(res.size() >= K) hi = Kval;
        else lo = Kval + 1;
    }
    for(long long r:res) cout << r << '\n';
}

Compilation message

road_construction.cpp: In function 'int main()':
road_construction.cpp:134:30: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  134 |             while(res.size() < K+1)
      |                   ~~~~~~~~~~~^~~~~
road_construction.cpp:158:30: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  158 |             while(res.size() < K+1)
      |                   ~~~~~~~~~~~^~~~~
road_construction.cpp:191:30: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  191 |             while(res.size() > K) res.pop_back();
      |                   ~~~~~~~~~~~^~~
road_construction.cpp:194:28: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  194 |         else if(res.size() >= K) hi = Kval;
      |                 ~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 3250 ms 5144 KB Output is correct
2 Correct 3277 ms 5172 KB Output is correct
3 Correct 3081 ms 5092 KB Output is correct
4 Correct 2851 ms 5168 KB Output is correct
5 Correct 3130 ms 4020 KB Output is correct
6 Correct 38 ms 544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 10041 ms 55236 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 10053 ms 53000 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 10053 ms 53000 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3250 ms 5144 KB Output is correct
2 Correct 3277 ms 5172 KB Output is correct
3 Correct 3081 ms 5092 KB Output is correct
4 Correct 2851 ms 5168 KB Output is correct
5 Correct 3130 ms 4020 KB Output is correct
6 Correct 38 ms 544 KB Output is correct
7 Execution timed out 10022 ms 25672 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3250 ms 5144 KB Output is correct
2 Correct 3277 ms 5172 KB Output is correct
3 Correct 3081 ms 5092 KB Output is correct
4 Correct 2851 ms 5168 KB Output is correct
5 Correct 3130 ms 4020 KB Output is correct
6 Correct 38 ms 544 KB Output is correct
7 Execution timed out 10041 ms 55236 KB Time limit exceeded
8 Halted 0 ms 0 KB -