Submission #817080

# Submission time Handle Problem Language Result Execution time Memory
817080 2023-08-09T09:16:23 Z finn__ Radio Towers (IOI22_towers) C++17
41 / 100
4000 ms 19272 KB
#include "towers.h"
#include <bits/stdc++.h>
using namespace std;

template <typename T, size_t L>
struct segtree_max
{
    T t[L << 1];

    void update(size_t i, T x)
    {
        i += L;
        t[i] = max(t[i], x);
        while (i >>= 1)
            t[i] = max(t[i << 1], t[i << 1 | 1]);
    }

    T range_max(size_t i, size_t j)
    {
        i += L, j += L;
        T x = numeric_limits<T>::min();

        while (i <= j)
        {
            if (i & 1)
                x = max(x, t[i++]);
            if (!(j & 1))
                x = max(x, t[j--]);
            i >>= 1;
            j >>= 1;
        }

        return x;
    }

    void reset() { memset(t, 0, sizeof t); }
};

template <typename T, size_t L>
struct fenwick_tree
{
    T t[L];

    void update(int64_t i, T x)
    {
        ++i;
        while (i <= L)
            t[i - 1] += x, i += i & -i;
    }

    T range_sum(int64_t i, int64_t j)
    {
        ++j;
        T x = 0;
        while (j)
            x += t[j - 1], j -= j & -j;
        while (i)
            x -= t[i - 1], i -= i & -i;
        return x;
    }
};

constexpr size_t N = 100000;

bool subtask1;
int h[N], coords[3 * N];
size_t n, peak_pos;

segtree_max<int64_t, 1 << 19> towers, intermediate;
fenwick_tree<int, N> peaks;

void init(int n_, vector<int> h_)
{
    n = n_;
    copy(h_.begin(), h_.end(), h);
    bool increasing = 1;
    subtask1 = 1;
    for (size_t i = 0; i + 1 < n; ++i)
    {
        if (h[i] < h[i + 1] && !increasing)
        {
            subtask1 = 0;
            break;
        }
        if (h[i] > h[i + 1])
            increasing = 0;
    }

    if (subtask1)
    {
        while (peak_pos + 1 < n && h[peak_pos + 1] > h[peak_pos])
            ++peak_pos;
        return;
    }

    for (size_t i = 0; i < n; ++i)
        if ((!i || h[i - 1] < h[i]) && (i + 1 == n || h[i + 1] < h[i]))
            peaks.update(i, 1);
}

size_t ind(int64_t coord) { return lower_bound(coords, coords + 3 * n, coord) - coords; }

int max_towers(int l, int r, int d)
{
    if (subtask1)
    {
        if (l >= peak_pos || r <= peak_pos)
            return 1;
        return (h[peak_pos] >= h[l] + d && h[peak_pos] >= h[r] + d) + 1;
    }
    else if (d == 1)
    {
        if (r - l <= 1)
            return 1;
        return peaks.range_sum(l + 1, r - 1) + 1;
    }
    else
    {
        memcpy(coords, h, sizeof h);
        for (size_t i = 0; i < n; ++i)
            coords[n + i] = coords[i] + d, coords[2 * n + i] = coords[i] - d;
        sort(coords, coords + 3 * n);

        towers.reset();
        intermediate.reset();

        towers.update(ind(h[l]), 1);
        int ans = 1;
        for (size_t i = l + 1; i <= r; ++i)
        {
            // as intermediate
            int64_t v = towers.range_max(0, ind(h[i] - d));
            intermediate.update(ind(h[i]), v);

            // as tower
            v = intermediate.range_max(ind(h[i] + d), 3 * n - 1) + 1;
            towers.update(ind(h[i]), v);
            ans = max<int>(ans, v);
        }

        return ans;
    }
}

Compilation message

towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:107:15: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
  107 |         if (l >= peak_pos || r <= peak_pos)
      |             ~~^~~~~~~~~~~
towers.cpp:107:32: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
  107 |         if (l >= peak_pos || r <= peak_pos)
      |                              ~~^~~~~~~~~~~
towers.cpp:129:34: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  129 |         for (size_t i = l + 1; i <= r; ++i)
      |                                ~~^~~~
towers.cpp: In instantiation of 'void fenwick_tree<T, L>::update(int64_t, T) [with T = int; long unsigned int L = 100000; int64_t = long int]':
towers.cpp:98:30:   required from here
towers.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'long unsigned int' [-Wsign-compare]
   47 |         while (i <= L)
      |                ~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 327 ms 932 KB Output is correct
2 Correct 694 ms 1452 KB Output is correct
3 Correct 664 ms 1464 KB Output is correct
4 Correct 726 ms 1476 KB Output is correct
5 Correct 381 ms 1444 KB Output is correct
6 Correct 762 ms 1460 KB Output is correct
7 Correct 758 ms 1488 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 17096 KB Output is correct
2 Correct 8 ms 17052 KB Output is correct
3 Correct 10 ms 17152 KB Output is correct
4 Correct 8 ms 17104 KB Output is correct
5 Correct 8 ms 17232 KB Output is correct
6 Correct 9 ms 17104 KB Output is correct
7 Correct 7 ms 17104 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 9 ms 17072 KB Output is correct
11 Correct 7 ms 17104 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 0 ms 208 KB Output is correct
14 Correct 1 ms 208 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 0 ms 208 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 7 ms 17104 KB Output is correct
26 Correct 8 ms 17104 KB Output is correct
27 Correct 8 ms 17104 KB Output is correct
28 Correct 9 ms 17160 KB Output is correct
29 Correct 8 ms 17104 KB Output is correct
30 Correct 8 ms 17076 KB Output is correct
31 Correct 8 ms 17104 KB Output is correct
32 Correct 1 ms 208 KB Output is correct
33 Correct 1 ms 208 KB Output is correct
34 Correct 8 ms 17132 KB Output is correct
35 Correct 8 ms 17076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 17096 KB Output is correct
2 Correct 8 ms 17052 KB Output is correct
3 Correct 10 ms 17152 KB Output is correct
4 Correct 8 ms 17104 KB Output is correct
5 Correct 8 ms 17232 KB Output is correct
6 Correct 9 ms 17104 KB Output is correct
7 Correct 7 ms 17104 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 9 ms 17072 KB Output is correct
11 Correct 7 ms 17104 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 0 ms 208 KB Output is correct
14 Correct 1 ms 208 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 0 ms 208 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 7 ms 17104 KB Output is correct
26 Correct 8 ms 17104 KB Output is correct
27 Correct 8 ms 17104 KB Output is correct
28 Correct 9 ms 17160 KB Output is correct
29 Correct 8 ms 17104 KB Output is correct
30 Correct 8 ms 17076 KB Output is correct
31 Correct 8 ms 17104 KB Output is correct
32 Correct 1 ms 208 KB Output is correct
33 Correct 1 ms 208 KB Output is correct
34 Correct 8 ms 17132 KB Output is correct
35 Correct 8 ms 17076 KB Output is correct
36 Correct 40 ms 18136 KB Output is correct
37 Correct 65 ms 19044 KB Output is correct
38 Correct 54 ms 18996 KB Output is correct
39 Correct 61 ms 19048 KB Output is correct
40 Correct 47 ms 19008 KB Output is correct
41 Correct 106 ms 19044 KB Output is correct
42 Correct 38 ms 18984 KB Output is correct
43 Correct 9 ms 1464 KB Output is correct
44 Correct 9 ms 1448 KB Output is correct
45 Correct 24 ms 18664 KB Output is correct
46 Correct 25 ms 18732 KB Output is correct
47 Correct 16 ms 1836 KB Output is correct
48 Correct 10 ms 1824 KB Output is correct
49 Correct 10 ms 1832 KB Output is correct
50 Correct 9 ms 1452 KB Output is correct
51 Correct 9 ms 1448 KB Output is correct
52 Correct 10 ms 1836 KB Output is correct
53 Correct 10 ms 1736 KB Output is correct
54 Correct 10 ms 1852 KB Output is correct
55 Correct 9 ms 1488 KB Output is correct
56 Correct 9 ms 1468 KB Output is correct
57 Correct 129 ms 18992 KB Output is correct
58 Correct 115 ms 18968 KB Output is correct
59 Correct 117 ms 18992 KB Output is correct
60 Correct 121 ms 19032 KB Output is correct
61 Correct 115 ms 18976 KB Output is correct
62 Correct 122 ms 19272 KB Output is correct
63 Correct 130 ms 19044 KB Output is correct
64 Correct 9 ms 1472 KB Output is correct
65 Correct 9 ms 1488 KB Output is correct
66 Correct 67 ms 18712 KB Output is correct
67 Correct 75 ms 18716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 529 ms 1736 KB Output is correct
2 Correct 673 ms 1832 KB Output is correct
3 Correct 585 ms 1828 KB Output is correct
4 Correct 706 ms 1836 KB Output is correct
5 Correct 385 ms 1828 KB Output is correct
6 Correct 628 ms 1864 KB Output is correct
7 Correct 763 ms 1872 KB Output is correct
8 Correct 592 ms 1428 KB Output is correct
9 Correct 546 ms 1448 KB Output is correct
10 Correct 676 ms 1480 KB Output is correct
11 Correct 636 ms 1480 KB Output is correct
12 Correct 772 ms 1360 KB Output is correct
13 Correct 761 ms 1452 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 208 KB Output is correct
17 Correct 11 ms 1840 KB Output is correct
18 Correct 10 ms 1820 KB Output is correct
19 Correct 10 ms 1816 KB Output is correct
20 Correct 9 ms 1444 KB Output is correct
21 Correct 9 ms 1444 KB Output is correct
22 Correct 10 ms 1840 KB Output is correct
23 Correct 10 ms 1736 KB Output is correct
24 Correct 10 ms 1828 KB Output is correct
25 Correct 9 ms 1464 KB Output is correct
26 Correct 9 ms 1488 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 0 ms 336 KB Output is correct
31 Correct 0 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 0 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 0 ms 208 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4051 ms 17368 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 17096 KB Output is correct
2 Correct 8 ms 17052 KB Output is correct
3 Correct 10 ms 17152 KB Output is correct
4 Correct 8 ms 17104 KB Output is correct
5 Correct 8 ms 17232 KB Output is correct
6 Correct 9 ms 17104 KB Output is correct
7 Correct 7 ms 17104 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 9 ms 17072 KB Output is correct
11 Correct 7 ms 17104 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 0 ms 208 KB Output is correct
14 Correct 1 ms 208 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 0 ms 208 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 7 ms 17104 KB Output is correct
26 Correct 8 ms 17104 KB Output is correct
27 Correct 8 ms 17104 KB Output is correct
28 Correct 9 ms 17160 KB Output is correct
29 Correct 8 ms 17104 KB Output is correct
30 Correct 8 ms 17076 KB Output is correct
31 Correct 8 ms 17104 KB Output is correct
32 Correct 1 ms 208 KB Output is correct
33 Correct 1 ms 208 KB Output is correct
34 Correct 8 ms 17132 KB Output is correct
35 Correct 8 ms 17076 KB Output is correct
36 Correct 40 ms 18136 KB Output is correct
37 Correct 65 ms 19044 KB Output is correct
38 Correct 54 ms 18996 KB Output is correct
39 Correct 61 ms 19048 KB Output is correct
40 Correct 47 ms 19008 KB Output is correct
41 Correct 106 ms 19044 KB Output is correct
42 Correct 38 ms 18984 KB Output is correct
43 Correct 9 ms 1464 KB Output is correct
44 Correct 9 ms 1448 KB Output is correct
45 Correct 24 ms 18664 KB Output is correct
46 Correct 25 ms 18732 KB Output is correct
47 Correct 16 ms 1836 KB Output is correct
48 Correct 10 ms 1824 KB Output is correct
49 Correct 10 ms 1832 KB Output is correct
50 Correct 9 ms 1452 KB Output is correct
51 Correct 9 ms 1448 KB Output is correct
52 Correct 10 ms 1836 KB Output is correct
53 Correct 10 ms 1736 KB Output is correct
54 Correct 10 ms 1852 KB Output is correct
55 Correct 9 ms 1488 KB Output is correct
56 Correct 9 ms 1468 KB Output is correct
57 Correct 129 ms 18992 KB Output is correct
58 Correct 115 ms 18968 KB Output is correct
59 Correct 117 ms 18992 KB Output is correct
60 Correct 121 ms 19032 KB Output is correct
61 Correct 115 ms 18976 KB Output is correct
62 Correct 122 ms 19272 KB Output is correct
63 Correct 130 ms 19044 KB Output is correct
64 Correct 9 ms 1472 KB Output is correct
65 Correct 9 ms 1488 KB Output is correct
66 Correct 67 ms 18712 KB Output is correct
67 Correct 75 ms 18716 KB Output is correct
68 Correct 529 ms 1736 KB Output is correct
69 Correct 673 ms 1832 KB Output is correct
70 Correct 585 ms 1828 KB Output is correct
71 Correct 706 ms 1836 KB Output is correct
72 Correct 385 ms 1828 KB Output is correct
73 Correct 628 ms 1864 KB Output is correct
74 Correct 763 ms 1872 KB Output is correct
75 Correct 592 ms 1428 KB Output is correct
76 Correct 546 ms 1448 KB Output is correct
77 Correct 676 ms 1480 KB Output is correct
78 Correct 636 ms 1480 KB Output is correct
79 Correct 772 ms 1360 KB Output is correct
80 Correct 761 ms 1452 KB Output is correct
81 Correct 0 ms 208 KB Output is correct
82 Correct 1 ms 336 KB Output is correct
83 Correct 1 ms 208 KB Output is correct
84 Correct 11 ms 1840 KB Output is correct
85 Correct 10 ms 1820 KB Output is correct
86 Correct 10 ms 1816 KB Output is correct
87 Correct 9 ms 1444 KB Output is correct
88 Correct 9 ms 1444 KB Output is correct
89 Correct 10 ms 1840 KB Output is correct
90 Correct 10 ms 1736 KB Output is correct
91 Correct 10 ms 1828 KB Output is correct
92 Correct 9 ms 1464 KB Output is correct
93 Correct 9 ms 1488 KB Output is correct
94 Correct 1 ms 336 KB Output is correct
95 Correct 1 ms 336 KB Output is correct
96 Correct 1 ms 336 KB Output is correct
97 Correct 0 ms 336 KB Output is correct
98 Correct 0 ms 336 KB Output is correct
99 Correct 1 ms 336 KB Output is correct
100 Correct 0 ms 336 KB Output is correct
101 Correct 1 ms 336 KB Output is correct
102 Correct 0 ms 208 KB Output is correct
103 Correct 1 ms 336 KB Output is correct
104 Execution timed out 4081 ms 18764 KB Time limit exceeded
105 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 327 ms 932 KB Output is correct
2 Correct 694 ms 1452 KB Output is correct
3 Correct 664 ms 1464 KB Output is correct
4 Correct 726 ms 1476 KB Output is correct
5 Correct 381 ms 1444 KB Output is correct
6 Correct 762 ms 1460 KB Output is correct
7 Correct 758 ms 1488 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 7 ms 17096 KB Output is correct
12 Correct 8 ms 17052 KB Output is correct
13 Correct 10 ms 17152 KB Output is correct
14 Correct 8 ms 17104 KB Output is correct
15 Correct 8 ms 17232 KB Output is correct
16 Correct 9 ms 17104 KB Output is correct
17 Correct 7 ms 17104 KB Output is correct
18 Correct 1 ms 208 KB Output is correct
19 Correct 1 ms 208 KB Output is correct
20 Correct 9 ms 17072 KB Output is correct
21 Correct 7 ms 17104 KB Output is correct
22 Correct 0 ms 208 KB Output is correct
23 Correct 0 ms 208 KB Output is correct
24 Correct 1 ms 208 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 0 ms 208 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 0 ms 208 KB Output is correct
34 Correct 0 ms 336 KB Output is correct
35 Correct 7 ms 17104 KB Output is correct
36 Correct 8 ms 17104 KB Output is correct
37 Correct 8 ms 17104 KB Output is correct
38 Correct 9 ms 17160 KB Output is correct
39 Correct 8 ms 17104 KB Output is correct
40 Correct 8 ms 17076 KB Output is correct
41 Correct 8 ms 17104 KB Output is correct
42 Correct 1 ms 208 KB Output is correct
43 Correct 1 ms 208 KB Output is correct
44 Correct 8 ms 17132 KB Output is correct
45 Correct 8 ms 17076 KB Output is correct
46 Correct 40 ms 18136 KB Output is correct
47 Correct 65 ms 19044 KB Output is correct
48 Correct 54 ms 18996 KB Output is correct
49 Correct 61 ms 19048 KB Output is correct
50 Correct 47 ms 19008 KB Output is correct
51 Correct 106 ms 19044 KB Output is correct
52 Correct 38 ms 18984 KB Output is correct
53 Correct 9 ms 1464 KB Output is correct
54 Correct 9 ms 1448 KB Output is correct
55 Correct 24 ms 18664 KB Output is correct
56 Correct 25 ms 18732 KB Output is correct
57 Correct 16 ms 1836 KB Output is correct
58 Correct 10 ms 1824 KB Output is correct
59 Correct 10 ms 1832 KB Output is correct
60 Correct 9 ms 1452 KB Output is correct
61 Correct 9 ms 1448 KB Output is correct
62 Correct 10 ms 1836 KB Output is correct
63 Correct 10 ms 1736 KB Output is correct
64 Correct 10 ms 1852 KB Output is correct
65 Correct 9 ms 1488 KB Output is correct
66 Correct 9 ms 1468 KB Output is correct
67 Correct 129 ms 18992 KB Output is correct
68 Correct 115 ms 18968 KB Output is correct
69 Correct 117 ms 18992 KB Output is correct
70 Correct 121 ms 19032 KB Output is correct
71 Correct 115 ms 18976 KB Output is correct
72 Correct 122 ms 19272 KB Output is correct
73 Correct 130 ms 19044 KB Output is correct
74 Correct 9 ms 1472 KB Output is correct
75 Correct 9 ms 1488 KB Output is correct
76 Correct 67 ms 18712 KB Output is correct
77 Correct 75 ms 18716 KB Output is correct
78 Correct 529 ms 1736 KB Output is correct
79 Correct 673 ms 1832 KB Output is correct
80 Correct 585 ms 1828 KB Output is correct
81 Correct 706 ms 1836 KB Output is correct
82 Correct 385 ms 1828 KB Output is correct
83 Correct 628 ms 1864 KB Output is correct
84 Correct 763 ms 1872 KB Output is correct
85 Correct 592 ms 1428 KB Output is correct
86 Correct 546 ms 1448 KB Output is correct
87 Correct 676 ms 1480 KB Output is correct
88 Correct 636 ms 1480 KB Output is correct
89 Correct 772 ms 1360 KB Output is correct
90 Correct 761 ms 1452 KB Output is correct
91 Correct 0 ms 208 KB Output is correct
92 Correct 1 ms 336 KB Output is correct
93 Correct 1 ms 208 KB Output is correct
94 Correct 11 ms 1840 KB Output is correct
95 Correct 10 ms 1820 KB Output is correct
96 Correct 10 ms 1816 KB Output is correct
97 Correct 9 ms 1444 KB Output is correct
98 Correct 9 ms 1444 KB Output is correct
99 Correct 10 ms 1840 KB Output is correct
100 Correct 10 ms 1736 KB Output is correct
101 Correct 10 ms 1828 KB Output is correct
102 Correct 9 ms 1464 KB Output is correct
103 Correct 9 ms 1488 KB Output is correct
104 Correct 1 ms 336 KB Output is correct
105 Correct 1 ms 336 KB Output is correct
106 Correct 1 ms 336 KB Output is correct
107 Correct 0 ms 336 KB Output is correct
108 Correct 0 ms 336 KB Output is correct
109 Correct 1 ms 336 KB Output is correct
110 Correct 0 ms 336 KB Output is correct
111 Correct 1 ms 336 KB Output is correct
112 Correct 0 ms 208 KB Output is correct
113 Correct 1 ms 336 KB Output is correct
114 Execution timed out 4051 ms 17368 KB Time limit exceeded
115 Halted 0 ms 0 KB -