답안 #931388

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
931388 2024-02-21T17:25:21 Z boris_mihov Road Construction (JOI21_road_construction) C++17
100 / 100
1527 ms 497216 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>

typedef long long llong;
const int MAXN = 250000 + 10;
const llong INF = 1e18;
const int INTINF = 1e9;
const int MAXLOG = 20;

int n, k;
struct PersistentSegmentTree
{
    struct Node
    {
        llong min;
        int minIdx;
        int left, right;

        Node()
        {
            min = INF;
            minIdx = left = right = 0;
        }

        void assign(const Node &left, const Node &right)
        {
            if (left.min < right.min)
            {
                min = left.min;
                minIdx = left.minIdx;
            } else
            {
                min = right.min;
                minIdx = right.minIdx;
            }
        }

        friend Node operator + (const Node &left, const Node &right)
        {
            if (left.min < right.min)
            {
                return left;
            } else
            {
                return right;
            }
        }
    };
    
    Node tree[2 * MAXN * MAXLOG];
    int cnt;

    void build(int l, int r, int node)
    {
        if (l == r)
        {
            tree[node].minIdx = l;
            return;
        }

        int mid = (l + r) / 2;
        tree[node].left = ++cnt;
        tree[node].right = ++cnt;
        build(l, mid, tree[node].left);
        build(mid + 1, r, tree[node].right);
        tree[node].assign(tree[tree[node].left], tree[tree[node].right]);
    }

    void update(int l, int r, int newNode, int oldNode, int queryPos, llong queryVal)
    {
        tree[newNode] = tree[oldNode];
        if (l == r)
        {
            tree[newNode].min = queryVal;
            return;
        }

        int mid = (l + r) / 2;
        if (queryPos <= mid)
        {
            tree[newNode].left = ++cnt;
            update(l, mid, tree[newNode].left, tree[oldNode].left, queryPos, queryVal);
        } else
        {
            tree[newNode].right = ++cnt;
            update(mid + 1, r, tree[newNode].right, tree[oldNode].right, queryPos, queryVal);
        }

        tree[newNode].assign(tree[tree[newNode].left], tree[tree[newNode].right]);
    }

    Node query(int l, int r, int node, int queryL, int queryR)
    {
        if (queryL <= l && r <= queryR)
        {
            return tree[node];
        }

        Node res;
        int mid = (l + r) / 2;
        if (queryL <= mid) res = query(l, mid, tree[node].left, queryL, queryR);
        if (mid + 1 <= queryR) res = res + query(mid + 1, r, tree[node].right, queryL, queryR);
        return res;
    }

    void build()
    {
        cnt = 1;
        build(1, n, 1);
    }

    int update(int oldNode, int pos, llong val)
    {
        int newNode = ++cnt;
        update(1, n, cnt, oldNode, pos, val);
        return newNode;
    }

    Node query(int node, int l, int r)
    {
        return query(1, n, node, l, r);
    }
};

struct Point
{
    llong x, y;
    friend bool operator < (const Point &a, const Point &b)
    {
        return a.x < b.x || (a.x == b.x && a.y < b.y);
    }
};

struct PQelement
{
    llong value;
    char type;
    int updIdx;
    int myIdx;

    friend bool operator < (const PQelement &a, const PQelement &b)
    {
        return a.value > b.value;
    }
};

Point p[MAXN];
int perm[MAXN];
int inPerm[MAXN];
PersistentSegmentTree treePlus;
PersistentSegmentTree treeMinus;
std::priority_queue <PQelement> pq;
int nodePlus[MAXN];
int nodeMinus[MAXN];

void solve()
{
    std::sort(p + 1, p + 1 + n);
    std::iota(perm + 1, perm + 1 + n, 1);
    std::sort(perm + 1, perm + 1 + n, [&](int a, int b)
    {
        return p[a].y < p[b].y;
    });

    for (int i = 1 ; i <= n ; ++i)
    {
        inPerm[perm[i]] = i;
    }

    treePlus.build();
    treeMinus.build();
    nodePlus[n] = nodeMinus[n] = 1;
    for (int i = n - 1 ; i >= 1 ; --i)
    {
        nodePlus[i] = treePlus.update(nodePlus[i + 1], inPerm[i + 1], p[i + 1].x + p[i + 1].y);
        nodeMinus[i] = treeMinus.update(nodeMinus[i + 1], inPerm[i + 1], p[i + 1].x - p[i + 1].y);

        PersistentSegmentTree::Node plusRes = treePlus.query(nodePlus[i], inPerm[i], n);
        if (plusRes.min != INF)
        {
            plusRes.min += -p[i].x - p[i].y;
            pq.push({plusRes.min, '+', plusRes.minIdx, i});
        }

        PersistentSegmentTree::Node minusRes = treeMinus.query(nodeMinus[i], 1, inPerm[i]);
        if (minusRes.min != INF)
        {
            minusRes.min += -p[i].x + p[i].y;
            pq.push({minusRes.min, '-', minusRes.minIdx, i});
        }
    }

    for (int time = 0 ; time < k ; ++time)
    {
        assert(pq.size());
        auto [val, type, updIdx, idx] = pq.top();
        pq.pop();

        std::cout << val << '\n';
        if (type == '+')
        {
            nodePlus[idx] = treePlus.update(nodePlus[idx], updIdx, INF);
            PersistentSegmentTree::Node plusRes = treePlus.query(nodePlus[idx], inPerm[idx], n);
            if (plusRes.min != INF)
            {
                plusRes.min += -p[idx].x - p[idx].y;
                pq.push({plusRes.min, '+', plusRes.minIdx, idx});
            }
        } else
        {
            nodeMinus[idx] = treeMinus.update(nodeMinus[idx], updIdx, INF);
            PersistentSegmentTree::Node minusRes = treeMinus.query(nodeMinus[idx], 1, inPerm[idx]);
            if (minusRes.min != INF)
            {
                minusRes.min += -p[idx].x + p[idx].y;
                pq.push({minusRes.min, '-', minusRes.minIdx, idx});
            }
        }
    }
}

void input()
{
    std::cin >> n >> k;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> p[i].x >> p[i].y;
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 263 ms 475028 KB Output is correct
2 Correct 258 ms 475176 KB Output is correct
3 Correct 239 ms 474964 KB Output is correct
4 Correct 221 ms 475192 KB Output is correct
5 Correct 235 ms 474008 KB Output is correct
6 Correct 112 ms 472388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 855 ms 491344 KB Output is correct
2 Correct 790 ms 494156 KB Output is correct
3 Correct 199 ms 474912 KB Output is correct
4 Correct 482 ms 493728 KB Output is correct
5 Correct 511 ms 493912 KB Output is correct
6 Correct 496 ms 494244 KB Output is correct
7 Correct 536 ms 493236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 674 ms 490060 KB Output is correct
2 Correct 705 ms 490376 KB Output is correct
3 Correct 109 ms 472260 KB Output is correct
4 Correct 319 ms 490176 KB Output is correct
5 Correct 630 ms 490160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 674 ms 490060 KB Output is correct
2 Correct 705 ms 490376 KB Output is correct
3 Correct 109 ms 472260 KB Output is correct
4 Correct 319 ms 490176 KB Output is correct
5 Correct 630 ms 490160 KB Output is correct
6 Correct 680 ms 490160 KB Output is correct
7 Correct 688 ms 490136 KB Output is correct
8 Correct 108 ms 472228 KB Output is correct
9 Correct 112 ms 472224 KB Output is correct
10 Correct 667 ms 490232 KB Output is correct
11 Correct 311 ms 489996 KB Output is correct
12 Correct 640 ms 490160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 263 ms 475028 KB Output is correct
2 Correct 258 ms 475176 KB Output is correct
3 Correct 239 ms 474964 KB Output is correct
4 Correct 221 ms 475192 KB Output is correct
5 Correct 235 ms 474008 KB Output is correct
6 Correct 112 ms 472388 KB Output is correct
7 Correct 946 ms 481324 KB Output is correct
8 Correct 964 ms 481528 KB Output is correct
9 Correct 217 ms 475124 KB Output is correct
10 Correct 466 ms 480588 KB Output is correct
11 Correct 556 ms 480944 KB Output is correct
12 Correct 497 ms 481236 KB Output is correct
13 Correct 620 ms 480568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 263 ms 475028 KB Output is correct
2 Correct 258 ms 475176 KB Output is correct
3 Correct 239 ms 474964 KB Output is correct
4 Correct 221 ms 475192 KB Output is correct
5 Correct 235 ms 474008 KB Output is correct
6 Correct 112 ms 472388 KB Output is correct
7 Correct 855 ms 491344 KB Output is correct
8 Correct 790 ms 494156 KB Output is correct
9 Correct 199 ms 474912 KB Output is correct
10 Correct 482 ms 493728 KB Output is correct
11 Correct 511 ms 493912 KB Output is correct
12 Correct 496 ms 494244 KB Output is correct
13 Correct 536 ms 493236 KB Output is correct
14 Correct 674 ms 490060 KB Output is correct
15 Correct 705 ms 490376 KB Output is correct
16 Correct 109 ms 472260 KB Output is correct
17 Correct 319 ms 490176 KB Output is correct
18 Correct 630 ms 490160 KB Output is correct
19 Correct 680 ms 490160 KB Output is correct
20 Correct 688 ms 490136 KB Output is correct
21 Correct 108 ms 472228 KB Output is correct
22 Correct 112 ms 472224 KB Output is correct
23 Correct 667 ms 490232 KB Output is correct
24 Correct 311 ms 489996 KB Output is correct
25 Correct 640 ms 490160 KB Output is correct
26 Correct 946 ms 481324 KB Output is correct
27 Correct 964 ms 481528 KB Output is correct
28 Correct 217 ms 475124 KB Output is correct
29 Correct 466 ms 480588 KB Output is correct
30 Correct 556 ms 480944 KB Output is correct
31 Correct 497 ms 481236 KB Output is correct
32 Correct 620 ms 480568 KB Output is correct
33 Correct 1495 ms 497216 KB Output is correct
34 Correct 1527 ms 496988 KB Output is correct
35 Correct 880 ms 496132 KB Output is correct
36 Correct 883 ms 496736 KB Output is correct
37 Correct 898 ms 496784 KB Output is correct
38 Correct 1051 ms 496288 KB Output is correct