Submission #1080321

# Submission time Handle Problem Language Result Execution time Memory
1080321 2024-08-29T08:50:10 Z Thanhs Road Construction (JOI21_road_construction) C++14
100 / 100
4742 ms 36116 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
 
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define fi first
#define se second
#define aint(x) x.begin(), x.end()
 
// mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
// int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}

const int NM = 1e6 + 5;

int n;

struct BIT
{
    int dt[NM];
    void reset()
    {
        memset(dt, 0, sizeof dt);
    }
    void upd(int i, int v)
    {
        for (; i < NM; i += i & -i)
            dt[i] += v;
    }
    int get(int i)
    {
        if (i < 0)
            return 0;
        int res = 0;
        for (; i; i -= i & -i)
            res += dt[i];
        return res;
    }
    int get(int l, int r)
    {
        return get(r) - get(l - 1);
    }
}bit;

int k;
vector<int> nen;
pair<int, int> a[NM];

int find(int x)
{
    return lower_bound(nen.begin(), nen.end(), x) - nen.begin() + 1;
}

int check(int x)
{
    bit.reset();
    int pt = 1, res = 0;
    for (int i = 1; i <= n; i++)
    {
        while (pt < i && a[pt].fi < a[i].fi - x)
            bit.upd(find(a[pt++].se), -1);
        res += bit.get(find(a[i].se - x), find(a[i].se + x + 1) - 1);
        bit.upd(find(a[i].se), 1);
    }
    return res;
}

map<int, int> mp;
set<pair<int, int>> s;

int dist(int i, int j)
{
    return max(abs(a[i].fi - a[j].fi), abs(a[i].se - a[j].se));
}

void trace(int x)
{
    int pt = 1;
    for (int i = 1; i <= n; i++)
    {
        while (pt < i && a[pt].fi < a[i].fi - x)
            s.erase({a[pt].se, pt++});
        auto it = s.lower_bound({a[i].se - x, 0});
        while (it != s.end() && (*it).fi <= a[i].se + x)
        {
            mp[dist((*it).se, i)]++;
            it = next(it);
        }
        s.insert({a[i].se, i});
    }
    for (auto p : mp)
    {
        for (int i = 0; i < p.se; i++)
            cout << p.fi << endl;
        k -= p.se;
    }
}

void solve()
{
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].fi >> a[i].se;
        a[i].fi += a[i].se;
        a[i].se = a[i].fi - 2 * a[i].se;
        nen.push_back(a[i].se);
    }
    sort(nen.begin(), nen.end());
    nen.erase(unique(nen.begin(), nen.end()), nen.end());
    sort(a + 1, a + 1 + n);
    int l = -1, r = 5e9;
    while (l < r - 1)
    {
        int m = l + r >> 1;
        if (check(m) >= k)
            r = m;
        else
            l = m;
    }
    trace(r - 1);
    for (int i = 1; i <= k; i++)
        cout << r << endl;
}

signed main()
{
    fastIO
    if (fopen("in.txt", "r")) 
    {
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    }
    int tc = 1; // cin >> tc;
    while (tc--)
        solve();

}

Compilation message

road_construction.cpp: In function 'void trace(long long int)':
road_construction.cpp:86:34: warning: operation on 'pt' may be undefined [-Wsequence-point]
   86 |             s.erase({a[pt].se, pt++});
      |                                ~~^~
road_construction.cpp:86:34: warning: operation on 'pt' may be undefined [-Wsequence-point]
road_construction.cpp: In function 'void solve()':
road_construction.cpp:119:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  119 |         int m = l + r >> 1;
      |                 ~~^~~
road_construction.cpp: In function 'int main()':
road_construction.cpp:135:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
road_construction.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 143 ms 26452 KB Output is correct
2 Correct 126 ms 26448 KB Output is correct
3 Correct 122 ms 26608 KB Output is correct
4 Correct 110 ms 26448 KB Output is correct
5 Correct 73 ms 12372 KB Output is correct
6 Correct 13 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1694 ms 15696 KB Output is correct
2 Correct 1645 ms 15812 KB Output is correct
3 Correct 107 ms 26320 KB Output is correct
4 Correct 1618 ms 15304 KB Output is correct
5 Correct 1643 ms 15428 KB Output is correct
6 Correct 1743 ms 15428 KB Output is correct
7 Correct 1628 ms 14536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4083 ms 14280 KB Output is correct
2 Correct 3920 ms 19264 KB Output is correct
3 Correct 8 ms 8284 KB Output is correct
4 Correct 1675 ms 17192 KB Output is correct
5 Correct 1392 ms 19396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4083 ms 14280 KB Output is correct
2 Correct 3920 ms 19264 KB Output is correct
3 Correct 8 ms 8284 KB Output is correct
4 Correct 1675 ms 17192 KB Output is correct
5 Correct 1392 ms 19396 KB Output is correct
6 Correct 4030 ms 19268 KB Output is correct
7 Correct 4025 ms 19140 KB Output is correct
8 Correct 9 ms 8284 KB Output is correct
9 Correct 11 ms 8172 KB Output is correct
10 Correct 3946 ms 19144 KB Output is correct
11 Correct 1659 ms 17092 KB Output is correct
12 Correct 1365 ms 19396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 143 ms 26452 KB Output is correct
2 Correct 126 ms 26448 KB Output is correct
3 Correct 122 ms 26608 KB Output is correct
4 Correct 110 ms 26448 KB Output is correct
5 Correct 73 ms 12372 KB Output is correct
6 Correct 13 ms 8536 KB Output is correct
7 Correct 1678 ms 27920 KB Output is correct
8 Correct 1694 ms 27924 KB Output is correct
9 Correct 113 ms 26448 KB Output is correct
10 Correct 1491 ms 12820 KB Output is correct
11 Correct 1296 ms 11724 KB Output is correct
12 Correct 349 ms 12560 KB Output is correct
13 Correct 479 ms 11020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 143 ms 26452 KB Output is correct
2 Correct 126 ms 26448 KB Output is correct
3 Correct 122 ms 26608 KB Output is correct
4 Correct 110 ms 26448 KB Output is correct
5 Correct 73 ms 12372 KB Output is correct
6 Correct 13 ms 8536 KB Output is correct
7 Correct 1694 ms 15696 KB Output is correct
8 Correct 1645 ms 15812 KB Output is correct
9 Correct 107 ms 26320 KB Output is correct
10 Correct 1618 ms 15304 KB Output is correct
11 Correct 1643 ms 15428 KB Output is correct
12 Correct 1743 ms 15428 KB Output is correct
13 Correct 1628 ms 14536 KB Output is correct
14 Correct 4083 ms 14280 KB Output is correct
15 Correct 3920 ms 19264 KB Output is correct
16 Correct 8 ms 8284 KB Output is correct
17 Correct 1675 ms 17192 KB Output is correct
18 Correct 1392 ms 19396 KB Output is correct
19 Correct 4030 ms 19268 KB Output is correct
20 Correct 4025 ms 19140 KB Output is correct
21 Correct 9 ms 8284 KB Output is correct
22 Correct 11 ms 8172 KB Output is correct
23 Correct 3946 ms 19144 KB Output is correct
24 Correct 1659 ms 17092 KB Output is correct
25 Correct 1365 ms 19396 KB Output is correct
26 Correct 1678 ms 27920 KB Output is correct
27 Correct 1694 ms 27924 KB Output is correct
28 Correct 113 ms 26448 KB Output is correct
29 Correct 1491 ms 12820 KB Output is correct
30 Correct 1296 ms 11724 KB Output is correct
31 Correct 349 ms 12560 KB Output is correct
32 Correct 479 ms 11020 KB Output is correct
33 Correct 4742 ms 36004 KB Output is correct
34 Correct 4650 ms 36116 KB Output is correct
35 Correct 3961 ms 21156 KB Output is correct
36 Correct 817 ms 21192 KB Output is correct
37 Correct 892 ms 21156 KB Output is correct
38 Correct 1417 ms 19868 KB Output is correct