Submission #701206

# Submission time Handle Problem Language Result Execution time Memory
701206 2023-02-20T14:19:45 Z boris_mihov Radio Towers (IOI22_towers) C++17
41 / 100
4000 ms 8812 KB
#include "towers.h"
#include <algorithm>
#include <iostream>
#include <cassert>
#include <numeric>
#include <vector>

typedef long long llong;
const int MAXN = 100000 + 10;
const int MAXLOG = 20;
const int INF  = 1e9;

int n;
int h[MAXN];
int perm[MAXN];
int getLog[MAXN];

struct BIT
{
    int tree[MAXN];
    inline void update(int pos, int val)
    {
        for (int idx = pos ; idx <= n ; idx += idx & (-idx))
        {
            tree[idx] += val;
        }
    }

    inline int query(int pos)
    {
        int ans = 0;
        for (int idx = pos ; idx >= 1 ; idx -= idx & (-idx))
        {
            ans += tree[idx];
        }

        return ans;
    }

    inline int find(int k)
    {
        int pos = 0;
        for (int log = MAXLOG - 1 ; log >= 0 ; --log)
        {
            if (pos + (1 << log) > n) continue;
            if (tree[pos + (1 << log)] < k)
            {
                pos += (1 << log);
                k -= tree[pos];
            }
        }

        return pos + 1;
    }
};

BIT tree;

struct SparseMAX
{
    int sparse[MAXLOG][MAXN];
    void build()
    {
        for (int i = 1 ; i <= n ; ++i)
        {
            sparse[0][i] = h[i];
        }

        for (int log = 1 ; (1 << log) <= n ; ++log)
        {
            for (int i = 1 ; i + (1 << log) - 1 <= n ; ++i)
            {
                sparse[log][i] = std::max(sparse[log - 1][i], sparse[log - 1][i + (1 << log - 1)]);
            }
        }

        for (int i = 1 ; i <= n ; ++i)
        {
            getLog[i] = getLog[i - 1];
            if ((1 << getLog[i] + 1) < i) getLog[i]++;
        }
    }

    inline int findMAX(int l, int r)
    {
        int log = getLog[r - l + 1];
        return std::max(sparse[log][l], sparse[log][r - (1 << log) + 1]);
    }
};

int firstPos;
bool first = true;
int prefix[MAXN];
SparseMAX sparse;
void init(int N, std::vector <int> H) 
{   
    n = N;
    for (int i = 0 ; i < n ; ++i)
    {
        h[i + 1] = H[i];
    }

    sparse.build();
    for (int i = 1 ; i <= n ; ++i)
    {
        prefix[i] = prefix[i - 1] + (i != 1 && i != n && h[i] < h[i - 1] && h[i] < h[i + 1]);
    }

    int where = 0;
    for (int i = 2 ; i <= n ; ++i)
    {
        if (h[i - 1] > h[i] && where == 0)
        {
            firstPos = i - 1;
            where = 1;
        }

        if (h[i - 1] < h[i] && where == 1)
        {
            first = false;
            break;
        }
    }

    if (firstPos == 0)
    {
        firstPos = n;
    }

    if (first)
    {
        for (int i = firstPos - 1 ; i >= 1 ; --i)
        {
            assert(h[i + 1] > h[i]);
        }

        for (int i = firstPos ; i <= n - 1 ; ++i)
        {
            assert(h[i] > h[i + 1]);
        }
    }
}

int max_towers(int l, int r, int d) 
{
    l++; r++;
    if (first)
    {
        if (l >= firstPos || r <= firstPos) return 1;
        if (std::max(h[l], h[r]) <= h[firstPos] - d) return 2;
        return 1;
    }

    if (d == 1)
    {
        if (r - l < 2) return 1;
        return prefix[r - 1] - prefix[l] + (h[l] < h[l + 1]) + (h[r] < h[r - 1]); 
    }

    std::iota(perm + l, perm + r + 1, l);
    std::sort(perm + l, perm + r + 1, [&](int x, int y)
    {
        return h[x] < h[y];
    });

    int ans = 0;
    for (int i = l ; i <= r ; ++i)
    {
        bool can = true;
        int pos = perm[i];
        
        if (tree.query(pos) != 0)
        {
            int left = tree.find(tree.query(pos));
            if (left == pos - 1 || sparse.findMAX(left + 1, pos - 1) - d < std::max(h[left], h[pos]))
            {
                can = false;
            } 
        }

        if (tree.query(pos) != ans)
        {
            int right = tree.find(tree.query(pos) + 1);
            if (right == pos + 1 || sparse.findMAX(pos + 1, right - 1) - d < std::max(h[pos], h[right]))
            {
                can = false;
            } 
        }

        if (can)
        {
            ans++;
            tree.update(pos, 1);
        }
    }

    return ans;
}

Compilation message

towers.cpp: In member function 'void SparseMAX::build()':
towers.cpp:73:93: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   73 |                 sparse[log][i] = std::max(sparse[log - 1][i], sparse[log - 1][i + (1 << log - 1)]);
      |                                                                                         ~~~~^~~
towers.cpp:80:33: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   80 |             if ((1 << getLog[i] + 1) < i) getLog[i]++;
      |                       ~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 377 ms 4944 KB Output is correct
2 Correct 895 ms 8356 KB Output is correct
3 Correct 877 ms 8356 KB Output is correct
4 Correct 909 ms 8392 KB Output is correct
5 Correct 784 ms 8392 KB Output is correct
6 Correct 565 ms 8344 KB Output is correct
7 Correct 815 ms 8352 KB Output is correct
8 Correct 0 ms 336 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 1 ms 464 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 464 KB Output is correct
22 Correct 1 ms 464 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 1 ms 464 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 1 ms 464 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 464 KB Output is correct
22 Correct 1 ms 464 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 1 ms 464 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
36 Correct 17 ms 5432 KB Output is correct
37 Correct 30 ms 8400 KB Output is correct
38 Correct 23 ms 8392 KB Output is correct
39 Correct 29 ms 8392 KB Output is correct
40 Correct 22 ms 8372 KB Output is correct
41 Correct 52 ms 8728 KB Output is correct
42 Correct 20 ms 8492 KB Output is correct
43 Correct 18 ms 8400 KB Output is correct
44 Correct 18 ms 8368 KB Output is correct
45 Correct 17 ms 8360 KB Output is correct
46 Correct 17 ms 8364 KB Output is correct
47 Correct 18 ms 8392 KB Output is correct
48 Correct 23 ms 8356 KB Output is correct
49 Correct 17 ms 8392 KB Output is correct
50 Correct 17 ms 8392 KB Output is correct
51 Correct 17 ms 8360 KB Output is correct
52 Correct 17 ms 8372 KB Output is correct
53 Correct 24 ms 8392 KB Output is correct
54 Correct 17 ms 8372 KB Output is correct
55 Correct 17 ms 8376 KB Output is correct
56 Correct 19 ms 8340 KB Output is correct
57 Correct 62 ms 8556 KB Output is correct
58 Correct 54 ms 8812 KB Output is correct
59 Correct 56 ms 8732 KB Output is correct
60 Correct 53 ms 8808 KB Output is correct
61 Correct 54 ms 8732 KB Output is correct
62 Correct 54 ms 8812 KB Output is correct
63 Correct 52 ms 8768 KB Output is correct
64 Correct 17 ms 8356 KB Output is correct
65 Correct 18 ms 8344 KB Output is correct
66 Correct 27 ms 8400 KB Output is correct
67 Correct 29 ms 8392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 548 ms 8492 KB Output is correct
2 Correct 942 ms 8360 KB Output is correct
3 Correct 775 ms 8368 KB Output is correct
4 Correct 843 ms 8392 KB Output is correct
5 Correct 873 ms 8400 KB Output is correct
6 Correct 790 ms 8392 KB Output is correct
7 Correct 874 ms 8476 KB Output is correct
8 Correct 703 ms 8352 KB Output is correct
9 Correct 953 ms 8396 KB Output is correct
10 Correct 708 ms 8344 KB Output is correct
11 Correct 884 ms 8356 KB Output is correct
12 Correct 701 ms 8356 KB Output is correct
13 Correct 948 ms 8372 KB Output is correct
14 Correct 0 ms 336 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 18 ms 8356 KB Output is correct
18 Correct 17 ms 8392 KB Output is correct
19 Correct 24 ms 8360 KB Output is correct
20 Correct 17 ms 8356 KB Output is correct
21 Correct 23 ms 8308 KB Output is correct
22 Correct 18 ms 8356 KB Output is correct
23 Correct 18 ms 8392 KB Output is correct
24 Correct 17 ms 8364 KB Output is correct
25 Correct 17 ms 8364 KB Output is correct
26 Correct 16 ms 8372 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 416 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4059 ms 2256 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 1 ms 464 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 464 KB Output is correct
22 Correct 1 ms 464 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 1 ms 464 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
36 Correct 17 ms 5432 KB Output is correct
37 Correct 30 ms 8400 KB Output is correct
38 Correct 23 ms 8392 KB Output is correct
39 Correct 29 ms 8392 KB Output is correct
40 Correct 22 ms 8372 KB Output is correct
41 Correct 52 ms 8728 KB Output is correct
42 Correct 20 ms 8492 KB Output is correct
43 Correct 18 ms 8400 KB Output is correct
44 Correct 18 ms 8368 KB Output is correct
45 Correct 17 ms 8360 KB Output is correct
46 Correct 17 ms 8364 KB Output is correct
47 Correct 18 ms 8392 KB Output is correct
48 Correct 23 ms 8356 KB Output is correct
49 Correct 17 ms 8392 KB Output is correct
50 Correct 17 ms 8392 KB Output is correct
51 Correct 17 ms 8360 KB Output is correct
52 Correct 17 ms 8372 KB Output is correct
53 Correct 24 ms 8392 KB Output is correct
54 Correct 17 ms 8372 KB Output is correct
55 Correct 17 ms 8376 KB Output is correct
56 Correct 19 ms 8340 KB Output is correct
57 Correct 62 ms 8556 KB Output is correct
58 Correct 54 ms 8812 KB Output is correct
59 Correct 56 ms 8732 KB Output is correct
60 Correct 53 ms 8808 KB Output is correct
61 Correct 54 ms 8732 KB Output is correct
62 Correct 54 ms 8812 KB Output is correct
63 Correct 52 ms 8768 KB Output is correct
64 Correct 17 ms 8356 KB Output is correct
65 Correct 18 ms 8344 KB Output is correct
66 Correct 27 ms 8400 KB Output is correct
67 Correct 29 ms 8392 KB Output is correct
68 Correct 548 ms 8492 KB Output is correct
69 Correct 942 ms 8360 KB Output is correct
70 Correct 775 ms 8368 KB Output is correct
71 Correct 843 ms 8392 KB Output is correct
72 Correct 873 ms 8400 KB Output is correct
73 Correct 790 ms 8392 KB Output is correct
74 Correct 874 ms 8476 KB Output is correct
75 Correct 703 ms 8352 KB Output is correct
76 Correct 953 ms 8396 KB Output is correct
77 Correct 708 ms 8344 KB Output is correct
78 Correct 884 ms 8356 KB Output is correct
79 Correct 701 ms 8356 KB Output is correct
80 Correct 948 ms 8372 KB Output is correct
81 Correct 0 ms 336 KB Output is correct
82 Correct 1 ms 464 KB Output is correct
83 Correct 1 ms 464 KB Output is correct
84 Correct 18 ms 8356 KB Output is correct
85 Correct 17 ms 8392 KB Output is correct
86 Correct 24 ms 8360 KB Output is correct
87 Correct 17 ms 8356 KB Output is correct
88 Correct 23 ms 8308 KB Output is correct
89 Correct 18 ms 8356 KB Output is correct
90 Correct 18 ms 8392 KB Output is correct
91 Correct 17 ms 8364 KB Output is correct
92 Correct 17 ms 8364 KB Output is correct
93 Correct 16 ms 8372 KB Output is correct
94 Correct 1 ms 464 KB Output is correct
95 Correct 1 ms 416 KB Output is correct
96 Correct 1 ms 464 KB Output is correct
97 Correct 1 ms 464 KB Output is correct
98 Correct 1 ms 464 KB Output is correct
99 Correct 1 ms 464 KB Output is correct
100 Correct 1 ms 464 KB Output is correct
101 Correct 1 ms 464 KB Output is correct
102 Correct 1 ms 464 KB Output is correct
103 Correct 1 ms 464 KB Output is correct
104 Execution timed out 4094 ms 7852 KB Time limit exceeded
105 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 377 ms 4944 KB Output is correct
2 Correct 895 ms 8356 KB Output is correct
3 Correct 877 ms 8356 KB Output is correct
4 Correct 909 ms 8392 KB Output is correct
5 Correct 784 ms 8392 KB Output is correct
6 Correct 565 ms 8344 KB Output is correct
7 Correct 815 ms 8352 KB Output is correct
8 Correct 0 ms 336 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 464 KB Output is correct
13 Correct 1 ms 464 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 464 KB Output is correct
22 Correct 0 ms 208 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 1 ms 464 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 464 KB Output is correct
38 Correct 1 ms 464 KB Output is correct
39 Correct 1 ms 464 KB Output is correct
40 Correct 1 ms 464 KB Output is correct
41 Correct 1 ms 464 KB Output is correct
42 Correct 1 ms 464 KB Output is correct
43 Correct 1 ms 464 KB Output is correct
44 Correct 1 ms 464 KB Output is correct
45 Correct 1 ms 464 KB Output is correct
46 Correct 17 ms 5432 KB Output is correct
47 Correct 30 ms 8400 KB Output is correct
48 Correct 23 ms 8392 KB Output is correct
49 Correct 29 ms 8392 KB Output is correct
50 Correct 22 ms 8372 KB Output is correct
51 Correct 52 ms 8728 KB Output is correct
52 Correct 20 ms 8492 KB Output is correct
53 Correct 18 ms 8400 KB Output is correct
54 Correct 18 ms 8368 KB Output is correct
55 Correct 17 ms 8360 KB Output is correct
56 Correct 17 ms 8364 KB Output is correct
57 Correct 18 ms 8392 KB Output is correct
58 Correct 23 ms 8356 KB Output is correct
59 Correct 17 ms 8392 KB Output is correct
60 Correct 17 ms 8392 KB Output is correct
61 Correct 17 ms 8360 KB Output is correct
62 Correct 17 ms 8372 KB Output is correct
63 Correct 24 ms 8392 KB Output is correct
64 Correct 17 ms 8372 KB Output is correct
65 Correct 17 ms 8376 KB Output is correct
66 Correct 19 ms 8340 KB Output is correct
67 Correct 62 ms 8556 KB Output is correct
68 Correct 54 ms 8812 KB Output is correct
69 Correct 56 ms 8732 KB Output is correct
70 Correct 53 ms 8808 KB Output is correct
71 Correct 54 ms 8732 KB Output is correct
72 Correct 54 ms 8812 KB Output is correct
73 Correct 52 ms 8768 KB Output is correct
74 Correct 17 ms 8356 KB Output is correct
75 Correct 18 ms 8344 KB Output is correct
76 Correct 27 ms 8400 KB Output is correct
77 Correct 29 ms 8392 KB Output is correct
78 Correct 548 ms 8492 KB Output is correct
79 Correct 942 ms 8360 KB Output is correct
80 Correct 775 ms 8368 KB Output is correct
81 Correct 843 ms 8392 KB Output is correct
82 Correct 873 ms 8400 KB Output is correct
83 Correct 790 ms 8392 KB Output is correct
84 Correct 874 ms 8476 KB Output is correct
85 Correct 703 ms 8352 KB Output is correct
86 Correct 953 ms 8396 KB Output is correct
87 Correct 708 ms 8344 KB Output is correct
88 Correct 884 ms 8356 KB Output is correct
89 Correct 701 ms 8356 KB Output is correct
90 Correct 948 ms 8372 KB Output is correct
91 Correct 0 ms 336 KB Output is correct
92 Correct 1 ms 464 KB Output is correct
93 Correct 1 ms 464 KB Output is correct
94 Correct 18 ms 8356 KB Output is correct
95 Correct 17 ms 8392 KB Output is correct
96 Correct 24 ms 8360 KB Output is correct
97 Correct 17 ms 8356 KB Output is correct
98 Correct 23 ms 8308 KB Output is correct
99 Correct 18 ms 8356 KB Output is correct
100 Correct 18 ms 8392 KB Output is correct
101 Correct 17 ms 8364 KB Output is correct
102 Correct 17 ms 8364 KB Output is correct
103 Correct 16 ms 8372 KB Output is correct
104 Correct 1 ms 464 KB Output is correct
105 Correct 1 ms 416 KB Output is correct
106 Correct 1 ms 464 KB Output is correct
107 Correct 1 ms 464 KB Output is correct
108 Correct 1 ms 464 KB Output is correct
109 Correct 1 ms 464 KB Output is correct
110 Correct 1 ms 464 KB Output is correct
111 Correct 1 ms 464 KB Output is correct
112 Correct 1 ms 464 KB Output is correct
113 Correct 1 ms 464 KB Output is correct
114 Execution timed out 4059 ms 2256 KB Time limit exceeded
115 Halted 0 ms 0 KB -