Submission #390997

# Submission time Handle Problem Language Result Execution time Memory
390997 2021-04-17T14:24:21 Z parsabahrami Treatment Project (JOI20_treatment) C++17
100 / 100
2611 ms 212776 KB
/* There's someone in my head but it's not me */ 
#include <bits/stdc++.h>
 
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
 
#define SZ(x)                       (int) x.size()
#define F                           first
#define S                           second
#define lc                          id << 1
#define rc                          lc | 1

const int N = 1e5 + 10; 
struct ver { int l, r, t, c; };
int n, m, M[N]; ll dp[N]; array<set<pii>, 2> seg[N << 2]; ver A[N];

void build(int id = 1, int l = 1, int r = m + 1) {
    for (int i = l; i < r; i++) {
        seg[id][0].insert({A[i].l + A[i].t, i}); 
        seg[id][1].insert({A[i].l - A[i].t, i});
    }
    if (r - l < 2) return;
    int md = (l + r) >> 1;
    build(lc, l, md), build(rc, md, r);
}

void remove(int p, int id = 1, int l = 1, int r = m + 1) {
    seg[id][0].erase({A[p].l + A[p].t, p});
    seg[id][1].erase({A[p].l - A[p].t, p});
    if (r - l < 2) return;
    int md = (l + r) >> 1;
    if (p < md) remove(p, lc, l, md);
    else remove(p, rc, md, r);
}

int get(int ql, int qr, int x, int t, int id = 1, int l = 1, int r = m + 1) {
    if (qr <= l || r <= ql || seg[id][t].empty() || seg[id][t].begin()->F > x) return -1;
    if (ql <= l && r <= qr) return seg[id][t].begin()->S;
    int md = (l + r) >> 1, y; y = get(ql, qr, x, t, lc, l, md);
    if (~y) return y;
    return get(ql, qr, x, t, rc, md, r);
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d%d%d%d", &A[i].t, &A[i].l, &A[i].r, &A[i].c); A[i].r++;
    }
    sort(A + 1, A + m + 1, [&] (const ver &a, const ver &b) { return a.t < b.t; });
    build(); 
    fill(dp, dp + N, 1e18); priority_queue<pair<ll, int>> pq;
    for (int i = 1; i <= m; i++) {
        if (A[i].l < 2) pq.push({-(dp[i] = A[i].c), i}), remove(i);
    }
    while (SZ(pq)) {
        int v = pq.top().S; pq.pop();
        if (M[v]) continue;
        M[v] = 1;
        int x = get(1, v, A[v].r - A[v].t, 1);
        while (~x) {
            if (dp[x] > dp[v] + A[x].c) 
                dp[x] = dp[v] + A[x].c, pq.emplace(-dp[x], x);
            remove(x);
            //printf("%d -> %d\n", v, x);
            x = get(1, v, A[v].r - A[v].t, 1);
        }
        x = get(v + 1, m + 1, A[v].r + A[v].t, 0);
        while (~x) {
            if (dp[x] > dp[v] + A[x].c) 
                dp[x] = dp[v] + A[x].c, pq.emplace(-dp[x], x);
            remove(x);
            //printf("%d -> %d\n", v, x);
            x = get(v + 1, m + 1, A[v].r + A[v].t, 0);
        }
    } 
    ll ret = 1e18;
    for (int i = 1; i <= m; i++) 
        if (A[i].r == n + 1) ret = min(ret, dp[i]);
    printf("%lld\n", ret < 1e18 ? ret : -1);
    return 0;
}

Compilation message

treatment.cpp: In function 'int main()':
treatment.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   47 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
treatment.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   49 |         scanf("%d%d%d%d", &A[i].t, &A[i].l, &A[i].r, &A[i].c); A[i].r++;
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1945 ms 210040 KB Output is correct
2 Correct 1305 ms 209972 KB Output is correct
3 Correct 1579 ms 210136 KB Output is correct
4 Correct 1578 ms 209688 KB Output is correct
5 Correct 1669 ms 212036 KB Output is correct
6 Correct 1199 ms 210156 KB Output is correct
7 Correct 975 ms 209908 KB Output is correct
8 Correct 621 ms 209936 KB Output is correct
9 Correct 604 ms 209684 KB Output is correct
10 Correct 566 ms 209588 KB Output is correct
11 Correct 1718 ms 211668 KB Output is correct
12 Correct 1734 ms 211816 KB Output is correct
13 Correct 1908 ms 212032 KB Output is correct
14 Correct 1899 ms 212040 KB Output is correct
15 Correct 1915 ms 210008 KB Output is correct
16 Correct 1879 ms 209988 KB Output is correct
17 Correct 1921 ms 209292 KB Output is correct
18 Correct 1772 ms 211020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 38552 KB Output is correct
2 Correct 23 ms 38656 KB Output is correct
3 Correct 22 ms 38672 KB Output is correct
4 Correct 23 ms 38568 KB Output is correct
5 Correct 24 ms 38560 KB Output is correct
6 Correct 24 ms 38544 KB Output is correct
7 Correct 23 ms 38640 KB Output is correct
8 Correct 23 ms 38640 KB Output is correct
9 Correct 25 ms 38656 KB Output is correct
10 Correct 23 ms 38680 KB Output is correct
11 Correct 25 ms 38616 KB Output is correct
12 Correct 25 ms 38680 KB Output is correct
13 Correct 24 ms 38616 KB Output is correct
14 Correct 24 ms 38604 KB Output is correct
15 Correct 25 ms 38620 KB Output is correct
16 Correct 24 ms 38656 KB Output is correct
17 Correct 23 ms 38604 KB Output is correct
18 Correct 23 ms 38640 KB Output is correct
19 Correct 23 ms 38668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 38552 KB Output is correct
2 Correct 23 ms 38656 KB Output is correct
3 Correct 22 ms 38672 KB Output is correct
4 Correct 23 ms 38568 KB Output is correct
5 Correct 24 ms 38560 KB Output is correct
6 Correct 24 ms 38544 KB Output is correct
7 Correct 23 ms 38640 KB Output is correct
8 Correct 23 ms 38640 KB Output is correct
9 Correct 25 ms 38656 KB Output is correct
10 Correct 23 ms 38680 KB Output is correct
11 Correct 25 ms 38616 KB Output is correct
12 Correct 25 ms 38680 KB Output is correct
13 Correct 24 ms 38616 KB Output is correct
14 Correct 24 ms 38604 KB Output is correct
15 Correct 25 ms 38620 KB Output is correct
16 Correct 24 ms 38656 KB Output is correct
17 Correct 23 ms 38604 KB Output is correct
18 Correct 23 ms 38640 KB Output is correct
19 Correct 23 ms 38668 KB Output is correct
20 Correct 55 ms 45124 KB Output is correct
21 Correct 55 ms 45192 KB Output is correct
22 Correct 65 ms 45196 KB Output is correct
23 Correct 62 ms 45196 KB Output is correct
24 Correct 66 ms 45316 KB Output is correct
25 Correct 66 ms 45112 KB Output is correct
26 Correct 63 ms 45124 KB Output is correct
27 Correct 61 ms 45144 KB Output is correct
28 Correct 64 ms 45252 KB Output is correct
29 Correct 66 ms 45216 KB Output is correct
30 Correct 45 ms 45188 KB Output is correct
31 Correct 46 ms 45180 KB Output is correct
32 Correct 63 ms 45356 KB Output is correct
33 Correct 63 ms 45252 KB Output is correct
34 Correct 63 ms 45204 KB Output is correct
35 Correct 66 ms 45252 KB Output is correct
36 Correct 59 ms 45320 KB Output is correct
37 Correct 64 ms 45156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1945 ms 210040 KB Output is correct
2 Correct 1305 ms 209972 KB Output is correct
3 Correct 1579 ms 210136 KB Output is correct
4 Correct 1578 ms 209688 KB Output is correct
5 Correct 1669 ms 212036 KB Output is correct
6 Correct 1199 ms 210156 KB Output is correct
7 Correct 975 ms 209908 KB Output is correct
8 Correct 621 ms 209936 KB Output is correct
9 Correct 604 ms 209684 KB Output is correct
10 Correct 566 ms 209588 KB Output is correct
11 Correct 1718 ms 211668 KB Output is correct
12 Correct 1734 ms 211816 KB Output is correct
13 Correct 1908 ms 212032 KB Output is correct
14 Correct 1899 ms 212040 KB Output is correct
15 Correct 1915 ms 210008 KB Output is correct
16 Correct 1879 ms 209988 KB Output is correct
17 Correct 1921 ms 209292 KB Output is correct
18 Correct 1772 ms 211020 KB Output is correct
19 Correct 22 ms 38552 KB Output is correct
20 Correct 23 ms 38656 KB Output is correct
21 Correct 22 ms 38672 KB Output is correct
22 Correct 23 ms 38568 KB Output is correct
23 Correct 24 ms 38560 KB Output is correct
24 Correct 24 ms 38544 KB Output is correct
25 Correct 23 ms 38640 KB Output is correct
26 Correct 23 ms 38640 KB Output is correct
27 Correct 25 ms 38656 KB Output is correct
28 Correct 23 ms 38680 KB Output is correct
29 Correct 25 ms 38616 KB Output is correct
30 Correct 25 ms 38680 KB Output is correct
31 Correct 24 ms 38616 KB Output is correct
32 Correct 24 ms 38604 KB Output is correct
33 Correct 25 ms 38620 KB Output is correct
34 Correct 24 ms 38656 KB Output is correct
35 Correct 23 ms 38604 KB Output is correct
36 Correct 23 ms 38640 KB Output is correct
37 Correct 23 ms 38668 KB Output is correct
38 Correct 55 ms 45124 KB Output is correct
39 Correct 55 ms 45192 KB Output is correct
40 Correct 65 ms 45196 KB Output is correct
41 Correct 62 ms 45196 KB Output is correct
42 Correct 66 ms 45316 KB Output is correct
43 Correct 66 ms 45112 KB Output is correct
44 Correct 63 ms 45124 KB Output is correct
45 Correct 61 ms 45144 KB Output is correct
46 Correct 64 ms 45252 KB Output is correct
47 Correct 66 ms 45216 KB Output is correct
48 Correct 45 ms 45188 KB Output is correct
49 Correct 46 ms 45180 KB Output is correct
50 Correct 63 ms 45356 KB Output is correct
51 Correct 63 ms 45252 KB Output is correct
52 Correct 63 ms 45204 KB Output is correct
53 Correct 66 ms 45252 KB Output is correct
54 Correct 59 ms 45320 KB Output is correct
55 Correct 64 ms 45156 KB Output is correct
56 Correct 1097 ms 210212 KB Output is correct
57 Correct 1076 ms 210108 KB Output is correct
58 Correct 1114 ms 209104 KB Output is correct
59 Correct 1115 ms 209460 KB Output is correct
60 Correct 1732 ms 210200 KB Output is correct
61 Correct 1111 ms 209056 KB Output is correct
62 Correct 1082 ms 210272 KB Output is correct
63 Correct 948 ms 210012 KB Output is correct
64 Correct 993 ms 209960 KB Output is correct
65 Correct 1885 ms 210152 KB Output is correct
66 Correct 1603 ms 210220 KB Output is correct
67 Correct 2504 ms 210768 KB Output is correct
68 Correct 2325 ms 210636 KB Output is correct
69 Correct 1895 ms 210620 KB Output is correct
70 Correct 2567 ms 210788 KB Output is correct
71 Correct 2406 ms 210640 KB Output is correct
72 Correct 1808 ms 210740 KB Output is correct
73 Correct 2589 ms 210920 KB Output is correct
74 Correct 767 ms 210468 KB Output is correct
75 Correct 743 ms 210300 KB Output is correct
76 Correct 1764 ms 212184 KB Output is correct
77 Correct 2169 ms 212508 KB Output is correct
78 Correct 1918 ms 212324 KB Output is correct
79 Correct 2585 ms 210680 KB Output is correct
80 Correct 2017 ms 210220 KB Output is correct
81 Correct 810 ms 210292 KB Output is correct
82 Correct 2093 ms 209680 KB Output is correct
83 Correct 2125 ms 209988 KB Output is correct
84 Correct 2611 ms 210012 KB Output is correct
85 Correct 1787 ms 210628 KB Output is correct
86 Correct 1692 ms 210648 KB Output is correct
87 Correct 1911 ms 210684 KB Output is correct
88 Correct 1431 ms 210616 KB Output is correct
89 Correct 1820 ms 210568 KB Output is correct
90 Correct 1909 ms 212508 KB Output is correct
91 Correct 2058 ms 211400 KB Output is correct
92 Correct 1862 ms 210756 KB Output is correct
93 Correct 2603 ms 210552 KB Output is correct
94 Correct 1939 ms 210824 KB Output is correct
95 Correct 2543 ms 210500 KB Output is correct
96 Correct 2175 ms 212468 KB Output is correct
97 Correct 2070 ms 212456 KB Output is correct
98 Correct 2037 ms 212492 KB Output is correct
99 Correct 2016 ms 212696 KB Output is correct
100 Correct 1420 ms 212380 KB Output is correct
101 Correct 2016 ms 212776 KB Output is correct
102 Correct 2123 ms 211512 KB Output is correct
103 Correct 1533 ms 210904 KB Output is correct