Submission #939523

# Submission time Handle Problem Language Result Execution time Memory
939523 2024-03-06T12:50:51 Z boris_mihov Treatment Project (JOI20_treatment) C++17
100 / 100
991 ms 301276 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
#include <set>

typedef long long llong;
const int MAXN = 100000 + 10;
const llong INF = 1e18;

int n, m;
struct Interval
{
    int t, l, r, c;
    friend bool operator < (const Interval &a, const Interval &b)
    {
        return a.t < b.t;
    }
};

Interval a[MAXN];
std::vector <std::pair <int,llong>> g[MAXN];
std::pair <int,llong> gTree[40 * MAXN][2];
int nodeCounter;

struct PersistentSegmentTree
{
    struct Node
    {
        int val;
        int left;
        int right;
        llong edgeValue;
    };

    Node tree[MAXN * 20];
    int root;
    int cnt;

    void build(int l, int r, int node, int order[])
    {
        if (l == r)
        {
            tree[node].val = order[l];
            tree[node].edgeValue = INF;
            return;
        }

        int mid = (l + r) / 2;
        tree[node].left = ++cnt;
        tree[node].right = ++cnt;
        assert(cnt < 20 * MAXN);
        build(l, mid, tree[node].left, order);
        build(mid + 1, r, tree[node].right, order);
        tree[node].val = ++nodeCounter;
        assert(nodeCounter < 40 * MAXN);
        tree[node].edgeValue = 0;

        gTree[tree[node].val][0] = {tree[tree[node].left].val, tree[tree[node].left].edgeValue};
        gTree[tree[node].val][1] = {tree[tree[node].right].val, tree[tree[node].right].edgeValue};        
    }

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

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

        tree[newNode].val = ++nodeCounter;
        assert(nodeCounter < 40 * MAXN);
        gTree[tree[newNode].val][0] = {tree[tree[newNode].left].val, tree[tree[newNode].left].edgeValue};
        gTree[tree[newNode].val][1] = {tree[tree[newNode].right].val, tree[tree[newNode].right].edgeValue};
    }

    void addEdges(int l, int r, int node, int queryL, int queryR, int from)
    {
        if (queryL <= l && r <= queryR)
        {
            g[from].push_back({tree[node].val, tree[node].edgeValue});
            return;
        }

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

    void build(int order[])
    {
        root = 1;
        cnt = 1;
        build(1, n, root, order);
    }

    void update(int pos, int val)
    {
        int newRoot = ++cnt;
        update(1, n, newRoot, root, pos, val);
        root = newRoot;
    }

    void addEdges(int l, int r, int from)
    {
        addEdges(1, n, root, l, r, from);
    }
};

bool in[40 * MAXN];
bool vis[40 * MAXN];
llong dist[40 * MAXN];
PersistentSegmentTree treePlus;
PersistentSegmentTree treeMinus;
std::set <std::pair <llong,int>> s;
int plusOrder[MAXN];
int minusOrder[MAXN];
int inPlusOrder[MAXN];
int inMinusOrder[MAXN];

void solve()
{
    std::sort(a + 1, a + 1 + n);
    std::iota(plusOrder + 1, plusOrder + 1 + n, 1);
    std::iota(minusOrder + 1, minusOrder + 1 + n, 1);
    std::sort(plusOrder + 1, plusOrder + 1 + n, [&](int x, int y)
    {
        return a[x].l + a[x].t < a[y].l + a[y].t;
    });

    std::sort(minusOrder + 1, minusOrder + 1 + n, [&](int x, int y)
    {
        return a[x].l - a[x].t < a[y].l - a[y].t;
    });

    for (int i = 1 ; i <= n ; ++i)
    {
        inPlusOrder[plusOrder[i]] = i;
        inMinusOrder[minusOrder[i]] = i;
    }

    nodeCounter = n;
    treePlus.build(plusOrder);
    for (int i = n ; i >= 1 ; --i)
    {
        int l = 0, r = n + 1, mid;
        while (l < r - 1)
        {
            mid = (l + r) / 2;
            if (a[plusOrder[mid]].l + a[plusOrder[mid]].t <= a[i].r + a[i].t + 1) l = mid;
            else r = mid;
        }

        if (l != 0)
        {
            treePlus.addEdges(1, l, i);
        }

        treePlus.update(inPlusOrder[i], a[i].c);
    }

    treeMinus.build(minusOrder);
    for (int i = 1 ; i <= n ; ++i)
    {
        int l = 0, r = n + 1, mid;
        while (l < r - 1)
        {
            mid = (l + r) / 2;
            if (a[minusOrder[mid]].l - a[minusOrder[mid]].t <= a[i].r - a[i].t + 1) l = mid;
            else r = mid;
        }

        if (l != 0)
        {
            treeMinus.addEdges(1, l, i);
        }

        treeMinus.update(inMinusOrder[i], a[i].c);
    }

    llong ans = INF;
    assert(nodeCounter < 40 * MAXN);
    std::fill(dist + 1, dist + 1 + nodeCounter, INF);
    for (int i = 1 ; i <= n ; ++i)
    {
        if (a[i].l == 1)
        {
            dist[i] = a[i].c;
            s.insert({dist[i], i});
            in[i] = true;
        }
    }

    // if (n > 16)
    // {
    //     return;
    // }

    while (s.size())
    {
        auto [currDist, node] = (*s.begin());
        s.erase(s.begin());

        if (node <= n && a[node].r == m)
        {
            ans = dist[node];
            break;
        }

        if (vis[node])
        {
            assert(false);
        }

        vis[node] = true;
        in[node] = true;
        if (node <= n)
        {
            for (const auto &[u, w] : g[node])
            {
                if (dist[u] > dist[node] + w)
                {
                    if (in[u])
                    {
                        s.erase(s.find({dist[u], u}));
                    }

                    dist[u] = dist[node] + w;
                    s.insert({dist[u], u});
                }
            }
        } else
        {
            for (const auto &[u, w] : gTree[node])
            {
                if (dist[u] > dist[node] + w)
                {
                    if (in[u])
                    {
                        s.erase(s.find({dist[u], u}));
                    }

                    dist[u] = dist[node] + w;
                    s.insert({dist[u], u});
                }
            }
        }
    }

    if (ans == INF) std::cout << -1 << '\n';
    else std::cout << ans << '\n';
}

void input()
{
    std::cin >> m >> n;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> a[i].t >> a[i].l >> a[i].r >> a[i].c;
    }   
}

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

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 767 ms 288688 KB Output is correct
2 Correct 517 ms 291644 KB Output is correct
3 Correct 779 ms 277584 KB Output is correct
4 Correct 808 ms 277844 KB Output is correct
5 Correct 668 ms 292176 KB Output is correct
6 Correct 586 ms 291020 KB Output is correct
7 Correct 445 ms 290896 KB Output is correct
8 Correct 294 ms 287824 KB Output is correct
9 Correct 305 ms 286416 KB Output is correct
10 Correct 291 ms 284744 KB Output is correct
11 Correct 410 ms 277564 KB Output is correct
12 Correct 419 ms 279636 KB Output is correct
13 Correct 854 ms 293688 KB Output is correct
14 Correct 919 ms 293668 KB Output is correct
15 Correct 863 ms 291904 KB Output is correct
16 Correct 833 ms 291588 KB Output is correct
17 Correct 857 ms 291000 KB Output is correct
18 Correct 350 ms 275888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 2 ms 14680 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14684 KB Output is correct
7 Correct 2 ms 14784 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 2 ms 14684 KB Output is correct
10 Correct 2 ms 14684 KB Output is correct
11 Correct 2 ms 14824 KB Output is correct
12 Correct 3 ms 14680 KB Output is correct
13 Correct 2 ms 14684 KB Output is correct
14 Correct 2 ms 14680 KB Output is correct
15 Correct 2 ms 14684 KB Output is correct
16 Correct 3 ms 14684 KB Output is correct
17 Correct 2 ms 14680 KB Output is correct
18 Correct 3 ms 14684 KB Output is correct
19 Correct 2 ms 14684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 2 ms 14680 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14684 KB Output is correct
7 Correct 2 ms 14784 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 2 ms 14684 KB Output is correct
10 Correct 2 ms 14684 KB Output is correct
11 Correct 2 ms 14824 KB Output is correct
12 Correct 3 ms 14680 KB Output is correct
13 Correct 2 ms 14684 KB Output is correct
14 Correct 2 ms 14680 KB Output is correct
15 Correct 2 ms 14684 KB Output is correct
16 Correct 3 ms 14684 KB Output is correct
17 Correct 2 ms 14680 KB Output is correct
18 Correct 3 ms 14684 KB Output is correct
19 Correct 2 ms 14684 KB Output is correct
20 Correct 19 ms 24388 KB Output is correct
21 Correct 15 ms 24408 KB Output is correct
22 Correct 14 ms 24668 KB Output is correct
23 Correct 13 ms 24668 KB Output is correct
24 Correct 15 ms 24988 KB Output is correct
25 Correct 19 ms 24836 KB Output is correct
26 Correct 19 ms 24668 KB Output is correct
27 Correct 16 ms 24668 KB Output is correct
28 Correct 15 ms 24960 KB Output is correct
29 Correct 18 ms 24668 KB Output is correct
30 Correct 11 ms 24664 KB Output is correct
31 Correct 10 ms 24668 KB Output is correct
32 Correct 14 ms 24924 KB Output is correct
33 Correct 13 ms 25052 KB Output is correct
34 Correct 23 ms 24536 KB Output is correct
35 Correct 11 ms 24668 KB Output is correct
36 Correct 11 ms 24664 KB Output is correct
37 Correct 23 ms 24668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 767 ms 288688 KB Output is correct
2 Correct 517 ms 291644 KB Output is correct
3 Correct 779 ms 277584 KB Output is correct
4 Correct 808 ms 277844 KB Output is correct
5 Correct 668 ms 292176 KB Output is correct
6 Correct 586 ms 291020 KB Output is correct
7 Correct 445 ms 290896 KB Output is correct
8 Correct 294 ms 287824 KB Output is correct
9 Correct 305 ms 286416 KB Output is correct
10 Correct 291 ms 284744 KB Output is correct
11 Correct 410 ms 277564 KB Output is correct
12 Correct 419 ms 279636 KB Output is correct
13 Correct 854 ms 293688 KB Output is correct
14 Correct 919 ms 293668 KB Output is correct
15 Correct 863 ms 291904 KB Output is correct
16 Correct 833 ms 291588 KB Output is correct
17 Correct 857 ms 291000 KB Output is correct
18 Correct 350 ms 275888 KB Output is correct
19 Correct 2 ms 14680 KB Output is correct
20 Correct 2 ms 14684 KB Output is correct
21 Correct 2 ms 14684 KB Output is correct
22 Correct 2 ms 14680 KB Output is correct
23 Correct 2 ms 14684 KB Output is correct
24 Correct 2 ms 14684 KB Output is correct
25 Correct 2 ms 14784 KB Output is correct
26 Correct 3 ms 14684 KB Output is correct
27 Correct 2 ms 14684 KB Output is correct
28 Correct 2 ms 14684 KB Output is correct
29 Correct 2 ms 14824 KB Output is correct
30 Correct 3 ms 14680 KB Output is correct
31 Correct 2 ms 14684 KB Output is correct
32 Correct 2 ms 14680 KB Output is correct
33 Correct 2 ms 14684 KB Output is correct
34 Correct 3 ms 14684 KB Output is correct
35 Correct 2 ms 14680 KB Output is correct
36 Correct 3 ms 14684 KB Output is correct
37 Correct 2 ms 14684 KB Output is correct
38 Correct 19 ms 24388 KB Output is correct
39 Correct 15 ms 24408 KB Output is correct
40 Correct 14 ms 24668 KB Output is correct
41 Correct 13 ms 24668 KB Output is correct
42 Correct 15 ms 24988 KB Output is correct
43 Correct 19 ms 24836 KB Output is correct
44 Correct 19 ms 24668 KB Output is correct
45 Correct 16 ms 24668 KB Output is correct
46 Correct 15 ms 24960 KB Output is correct
47 Correct 18 ms 24668 KB Output is correct
48 Correct 11 ms 24664 KB Output is correct
49 Correct 10 ms 24668 KB Output is correct
50 Correct 14 ms 24924 KB Output is correct
51 Correct 13 ms 25052 KB Output is correct
52 Correct 23 ms 24536 KB Output is correct
53 Correct 11 ms 24668 KB Output is correct
54 Correct 11 ms 24664 KB Output is correct
55 Correct 23 ms 24668 KB Output is correct
56 Correct 465 ms 286060 KB Output is correct
57 Correct 482 ms 286168 KB Output is correct
58 Correct 467 ms 291932 KB Output is correct
59 Correct 460 ms 292480 KB Output is correct
60 Correct 409 ms 293632 KB Output is correct
61 Correct 460 ms 292296 KB Output is correct
62 Correct 472 ms 286160 KB Output is correct
63 Correct 272 ms 296532 KB Output is correct
64 Correct 270 ms 296532 KB Output is correct
65 Correct 750 ms 292044 KB Output is correct
66 Correct 378 ms 293704 KB Output is correct
67 Correct 763 ms 294768 KB Output is correct
68 Correct 619 ms 293972 KB Output is correct
69 Correct 678 ms 293868 KB Output is correct
70 Correct 818 ms 294860 KB Output is correct
71 Correct 712 ms 294156 KB Output is correct
72 Correct 615 ms 294088 KB Output is correct
73 Correct 814 ms 294864 KB Output is correct
74 Correct 321 ms 288312 KB Output is correct
75 Correct 309 ms 287976 KB Output is correct
76 Correct 430 ms 295752 KB Output is correct
77 Correct 402 ms 299976 KB Output is correct
78 Correct 935 ms 295784 KB Output is correct
79 Correct 800 ms 294996 KB Output is correct
80 Correct 938 ms 293704 KB Output is correct
81 Correct 351 ms 288856 KB Output is correct
82 Correct 942 ms 293460 KB Output is correct
83 Correct 991 ms 293344 KB Output is correct
84 Correct 843 ms 293992 KB Output is correct
85 Correct 521 ms 294224 KB Output is correct
86 Correct 505 ms 294436 KB Output is correct
87 Correct 655 ms 294640 KB Output is correct
88 Correct 525 ms 294428 KB Output is correct
89 Correct 556 ms 293864 KB Output is correct
90 Correct 471 ms 296804 KB Output is correct
91 Correct 351 ms 288340 KB Output is correct
92 Correct 614 ms 294856 KB Output is correct
93 Correct 746 ms 295188 KB Output is correct
94 Correct 738 ms 295628 KB Output is correct
95 Correct 586 ms 294776 KB Output is correct
96 Correct 393 ms 293460 KB Output is correct
97 Correct 552 ms 299728 KB Output is correct
98 Correct 594 ms 300880 KB Output is correct
99 Correct 482 ms 298872 KB Output is correct
100 Correct 404 ms 292108 KB Output is correct
101 Correct 792 ms 301276 KB Output is correct
102 Correct 421 ms 296788 KB Output is correct
103 Correct 510 ms 293856 KB Output is correct