Submission #626505

# Submission time Handle Problem Language Result Execution time Memory
626505 2022-08-11T13:38:55 Z ITO Radio Towers (IOI22_towers) C++17
37 / 100
1195 ms 29936 KB
#include "towers.h"

#include <bits/stdc++.h>
using namespace std;
int n, h[100005], hi[100005], mama[100005];
vector<pair<int, int>> pv;

int av[400000], bv[400000];
vector<int> aa[400000];

int build(int id, int ll, int rr) {
  if (ll == rr) {
    av[id] = h[ll];
    bv[id] = mama[ll];
    if (bv[id] == 2000000001) bv[id] = -1000000001;
    aa[id].push_back(mama[ll]);
  } else {
    int m = (ll + rr) / 2, i = 0, j = 0;
    av[id] = min(build(id * 2, ll, m), build(id * 2 + 1, m + 1, rr));
    bv[id] = max(bv[id * 2], bv[id * 2 + 1]);
    while (i < aa[id * 2].size() && j < aa[id * 2 + 1].size()) {
      if (aa[id * 2][i] <= aa[id * 2 + 1][j]) {
        aa[id].push_back(aa[id * 2][i]);
        i++;
      } else {
        aa[id].push_back(aa[id * 2 + 1][j]);
        j++;
      }
    }
    while (i < aa[id * 2].size()) {
      aa[id].push_back(aa[id * 2][i]);
      i++;
    }
    while (j < aa[id * 2 + 1].size()) {
      aa[id].push_back(aa[id * 2 + 1][j]);
      j++;
    }
  }
  return av[id];
}
int mi(int id, int l, int r, int ll, int rr) {
  if (r < ll || l > rr) return 2000000001;
  if (ll >= l && rr <= r) return av[id];
  int m = (ll + rr) / 2;
  return min(mi(id * 2, l, r, ll, m), mi(id * 2 + 1, l, r, m + 1, rr));
}
int lar(int id, int va, int l, int r, int ll, int rr) {
  if (r < ll || l > rr || bv[id] < va) return -1;
  if (ll == rr) return ll;
  int m = (ll + rr) / 2;
  int te = lar(id * 2 + 1, va, l, r, m + 1, rr);
  if (te >= 0) return te;
  return lar(id * 2, va, l, r, ll, m);
}
int lal(int id, int va, int l, int r, int ll, int rr) {
  if (r < ll || l > rr || bv[id] < va) return -1;
  if (ll == rr) return ll;
  int m = (ll + rr) / 2;
  int te = lal(id * 2, va, l, r, ll, m);
  if (te >= 0) return te;
  return lal(id * 2 + 1, va, l, r, m + 1, rr);
}
int bel(int id, int va, int l, int r, int ll, int rr) {
  if (r < ll || l > rr) return 0;
  if (ll >= l && r >= rr) {
    return upper_bound(aa[id].begin(), aa[id].end(), va) - aa[id].begin();
  }
  int m = (ll + rr) / 2;
  return bel(id * 2, va, l, r, ll, m) + bel(id * 2 + 1, va, l, r, m + 1, rr);
}

int rii[100005], lee[100005];
void init(int N, std::vector<int> H) {
  n = N;
  pv.push_back({-2000000001, -1});
  for (int i = 0; i < N; i++) {
    mama[i] = 2000000001;
    h[i] = H[i];
    int te = pv.size();
    if (te >= 2 && ((pv[te - 1].first > pv[te - 2].first && h[i] > pv[te - 1].first) || (pv[te - 1].first < pv[te - 2].first && h[i] < pv[te - 1].first))) {
      pv[te - 1] = make_pair(h[i], i);
    } else {
      pv.push_back({h[i], i});
    }
    hi[i] = (pv.size() - 1);
  }
  int te = pv.size();
  if ((pv[te - 1].first > pv[te - 2].first && -1000000001 > pv[te - 1].first) || (pv[te - 1].first < pv[te - 2].first && -1000000001 < pv[te - 1].first)) {
    pv[te - 1] = make_pair(-1000000001, N);
  } else {
    pv.push_back({-1000000001, N});
  }
  te = pv.size();
  priority_queue<pair<int, pair<int, int>>> pq;
  for (int i = 1; i < te; i++) {
    pq.push({-abs(pv[i].first - pv[i - 1].first), {i - 1, i}});
    rii[i - 1] = i;
    lee[i] = i - 1;
  }
  int xx = te / 2;
  while (1) {
    pair<int, pair<int, int>> pin = pq.top();
    pq.pop();
    if (lee[pin.second.first] == -1 || lee[pin.second.second] == -1) continue;
    pq.push({-abs(pv[lee[pin.second.first]].first - pv[rii[pin.second.second]].first), {lee[pin.second.first], rii[pin.second.second]}});
    if (pv[pin.second.first].first > pv[pin.second.second].first) {
      mama[pv[pin.second.first].second] = -pin.first;
    } else {
      mama[pv[pin.second.second].second] = -pin.first;
    }
    xx--;
    if (!xx) break;
    rii[lee[pin.second.first]] = rii[pin.second.second];
    lee[rii[pin.second.second]] = lee[pin.second.first];
    lee[pin.second.first] = -1;
    lee[pin.second.second] = -1;
  }
  build(1, 0, N - 1);
}

int max_towers(int L, int R, int D) {
  int lai = lal(1, D, L, R, 0, n - 1), rai = lar(1, D, L, R, 0, n - 1);
  if (hi[L] == hi[R] || lai == -1) return 1;
  int co = (hi[rai] - hi[lai]) / 2 - bel(1, D - 1, lai, rai, 0, n - 1);
  if (h[lai] - mi(1, L, lai, 0, n - 1) >= D) co++;
  if (h[rai] - mi(1, rai, R, 0, n - 1) >= D) co++;
  return co;
}

Compilation message

towers.cpp: In function 'int build(int, int, int)':
towers.cpp:21:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     while (i < aa[id * 2].size() && j < aa[id * 2 + 1].size()) {
      |            ~~^~~~~~~~~~~~~~~~~~~
towers.cpp:21:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     while (i < aa[id * 2].size() && j < aa[id * 2 + 1].size()) {
      |                                     ~~^~~~~~~~~~~~~~~~~~~~~~~
towers.cpp:30:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |     while (i < aa[id * 2].size()) {
      |            ~~^~~~~~~~~~~~~~~~~~~
towers.cpp:34:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |     while (j < aa[id * 2 + 1].size()) {
      |            ~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 456 ms 19016 KB 18th lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9680 KB Output is correct
2 Correct 6 ms 9936 KB Output is correct
3 Correct 6 ms 9936 KB Output is correct
4 Correct 7 ms 9936 KB Output is correct
5 Correct 6 ms 9936 KB Output is correct
6 Correct 7 ms 10028 KB Output is correct
7 Correct 6 ms 9936 KB Output is correct
8 Correct 6 ms 9936 KB Output is correct
9 Correct 5 ms 9936 KB Output is correct
10 Correct 6 ms 9936 KB Output is correct
11 Correct 7 ms 9936 KB Output is correct
12 Correct 5 ms 9680 KB Output is correct
13 Correct 6 ms 9936 KB Output is correct
14 Correct 6 ms 9936 KB Output is correct
15 Correct 6 ms 9936 KB Output is correct
16 Correct 6 ms 9936 KB Output is correct
17 Correct 6 ms 9936 KB Output is correct
18 Correct 5 ms 9936 KB Output is correct
19 Correct 5 ms 9936 KB Output is correct
20 Correct 6 ms 9936 KB Output is correct
21 Correct 6 ms 9936 KB Output is correct
22 Correct 6 ms 9936 KB Output is correct
23 Correct 5 ms 9936 KB Output is correct
24 Correct 8 ms 9936 KB Output is correct
25 Correct 5 ms 9808 KB Output is correct
26 Correct 6 ms 9936 KB Output is correct
27 Correct 8 ms 9956 KB Output is correct
28 Correct 6 ms 9936 KB Output is correct
29 Correct 7 ms 9936 KB Output is correct
30 Correct 7 ms 9936 KB Output is correct
31 Correct 7 ms 9936 KB Output is correct
32 Correct 7 ms 9936 KB Output is correct
33 Correct 6 ms 9936 KB Output is correct
34 Correct 6 ms 9936 KB Output is correct
35 Correct 6 ms 9936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9680 KB Output is correct
2 Correct 6 ms 9936 KB Output is correct
3 Correct 6 ms 9936 KB Output is correct
4 Correct 7 ms 9936 KB Output is correct
5 Correct 6 ms 9936 KB Output is correct
6 Correct 7 ms 10028 KB Output is correct
7 Correct 6 ms 9936 KB Output is correct
8 Correct 6 ms 9936 KB Output is correct
9 Correct 5 ms 9936 KB Output is correct
10 Correct 6 ms 9936 KB Output is correct
11 Correct 7 ms 9936 KB Output is correct
12 Correct 5 ms 9680 KB Output is correct
13 Correct 6 ms 9936 KB Output is correct
14 Correct 6 ms 9936 KB Output is correct
15 Correct 6 ms 9936 KB Output is correct
16 Correct 6 ms 9936 KB Output is correct
17 Correct 6 ms 9936 KB Output is correct
18 Correct 5 ms 9936 KB Output is correct
19 Correct 5 ms 9936 KB Output is correct
20 Correct 6 ms 9936 KB Output is correct
21 Correct 6 ms 9936 KB Output is correct
22 Correct 6 ms 9936 KB Output is correct
23 Correct 5 ms 9936 KB Output is correct
24 Correct 8 ms 9936 KB Output is correct
25 Correct 5 ms 9808 KB Output is correct
26 Correct 6 ms 9936 KB Output is correct
27 Correct 8 ms 9956 KB Output is correct
28 Correct 6 ms 9936 KB Output is correct
29 Correct 7 ms 9936 KB Output is correct
30 Correct 7 ms 9936 KB Output is correct
31 Correct 7 ms 9936 KB Output is correct
32 Correct 7 ms 9936 KB Output is correct
33 Correct 6 ms 9936 KB Output is correct
34 Correct 6 ms 9936 KB Output is correct
35 Correct 6 ms 9936 KB Output is correct
36 Correct 51 ms 20756 KB Output is correct
37 Correct 76 ms 29008 KB Output is correct
38 Correct 77 ms 29016 KB Output is correct
39 Correct 87 ms 29880 KB Output is correct
40 Correct 81 ms 29880 KB Output is correct
41 Correct 81 ms 29936 KB Output is correct
42 Correct 84 ms 29932 KB Output is correct
43 Correct 55 ms 27076 KB Output is correct
44 Correct 53 ms 26948 KB Output is correct
45 Correct 53 ms 26988 KB Output is correct
46 Correct 50 ms 26976 KB Output is correct
47 Correct 73 ms 28980 KB Output is correct
48 Correct 96 ms 29836 KB Output is correct
49 Correct 100 ms 29880 KB Output is correct
50 Correct 68 ms 26996 KB Output is correct
51 Correct 52 ms 27020 KB Output is correct
52 Correct 91 ms 28948 KB Output is correct
53 Correct 90 ms 29884 KB Output is correct
54 Correct 103 ms 29916 KB Output is correct
55 Correct 51 ms 26940 KB Output is correct
56 Correct 56 ms 27016 KB Output is correct
57 Correct 70 ms 28536 KB Output is correct
58 Correct 80 ms 28988 KB Output is correct
59 Correct 79 ms 29020 KB Output is correct
60 Correct 84 ms 29916 KB Output is correct
61 Correct 85 ms 29852 KB Output is correct
62 Correct 87 ms 29876 KB Output is correct
63 Correct 87 ms 29888 KB Output is correct
64 Correct 57 ms 26992 KB Output is correct
65 Correct 53 ms 26976 KB Output is correct
66 Correct 55 ms 26976 KB Output is correct
67 Correct 53 ms 26936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1074 ms 28920 KB Output is correct
2 Correct 1195 ms 28996 KB Output is correct
3 Correct 1126 ms 28928 KB Output is correct
4 Correct 1160 ms 29880 KB Output is correct
5 Correct 1145 ms 29824 KB Output is correct
6 Correct 1067 ms 29880 KB Output is correct
7 Correct 916 ms 29868 KB Output is correct
8 Correct 819 ms 26948 KB Output is correct
9 Correct 839 ms 27020 KB Output is correct
10 Correct 869 ms 27032 KB Output is correct
11 Correct 934 ms 27024 KB Output is correct
12 Correct 872 ms 26996 KB Output is correct
13 Correct 916 ms 26980 KB Output is correct
14 Correct 5 ms 9680 KB Output is correct
15 Correct 6 ms 9936 KB Output is correct
16 Correct 6 ms 9936 KB Output is correct
17 Correct 75 ms 29072 KB Output is correct
18 Correct 90 ms 29908 KB Output is correct
19 Correct 80 ms 29888 KB Output is correct
20 Correct 59 ms 26976 KB Output is correct
21 Correct 60 ms 26948 KB Output is correct
22 Correct 73 ms 28988 KB Output is correct
23 Correct 89 ms 29892 KB Output is correct
24 Correct 81 ms 29888 KB Output is correct
25 Correct 50 ms 26948 KB Output is correct
26 Correct 49 ms 26980 KB Output is correct
27 Correct 6 ms 9936 KB Output is correct
28 Correct 6 ms 10052 KB Output is correct
29 Correct 6 ms 9936 KB Output is correct
30 Correct 6 ms 9972 KB Output is correct
31 Correct 5 ms 9936 KB Output is correct
32 Correct 6 ms 9936 KB Output is correct
33 Correct 6 ms 9968 KB Output is correct
34 Correct 6 ms 9956 KB Output is correct
35 Correct 6 ms 9936 KB Output is correct
36 Correct 6 ms 9936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 344 ms 14212 KB Output is correct
2 Incorrect 1154 ms 29012 KB 13344th lines differ - on the 1st token, expected: '1', found: '0'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9680 KB Output is correct
2 Correct 6 ms 9936 KB Output is correct
3 Correct 6 ms 9936 KB Output is correct
4 Correct 7 ms 9936 KB Output is correct
5 Correct 6 ms 9936 KB Output is correct
6 Correct 7 ms 10028 KB Output is correct
7 Correct 6 ms 9936 KB Output is correct
8 Correct 6 ms 9936 KB Output is correct
9 Correct 5 ms 9936 KB Output is correct
10 Correct 6 ms 9936 KB Output is correct
11 Correct 7 ms 9936 KB Output is correct
12 Correct 5 ms 9680 KB Output is correct
13 Correct 6 ms 9936 KB Output is correct
14 Correct 6 ms 9936 KB Output is correct
15 Correct 6 ms 9936 KB Output is correct
16 Correct 6 ms 9936 KB Output is correct
17 Correct 6 ms 9936 KB Output is correct
18 Correct 5 ms 9936 KB Output is correct
19 Correct 5 ms 9936 KB Output is correct
20 Correct 6 ms 9936 KB Output is correct
21 Correct 6 ms 9936 KB Output is correct
22 Correct 6 ms 9936 KB Output is correct
23 Correct 5 ms 9936 KB Output is correct
24 Correct 8 ms 9936 KB Output is correct
25 Correct 5 ms 9808 KB Output is correct
26 Correct 6 ms 9936 KB Output is correct
27 Correct 8 ms 9956 KB Output is correct
28 Correct 6 ms 9936 KB Output is correct
29 Correct 7 ms 9936 KB Output is correct
30 Correct 7 ms 9936 KB Output is correct
31 Correct 7 ms 9936 KB Output is correct
32 Correct 7 ms 9936 KB Output is correct
33 Correct 6 ms 9936 KB Output is correct
34 Correct 6 ms 9936 KB Output is correct
35 Correct 6 ms 9936 KB Output is correct
36 Correct 51 ms 20756 KB Output is correct
37 Correct 76 ms 29008 KB Output is correct
38 Correct 77 ms 29016 KB Output is correct
39 Correct 87 ms 29880 KB Output is correct
40 Correct 81 ms 29880 KB Output is correct
41 Correct 81 ms 29936 KB Output is correct
42 Correct 84 ms 29932 KB Output is correct
43 Correct 55 ms 27076 KB Output is correct
44 Correct 53 ms 26948 KB Output is correct
45 Correct 53 ms 26988 KB Output is correct
46 Correct 50 ms 26976 KB Output is correct
47 Correct 73 ms 28980 KB Output is correct
48 Correct 96 ms 29836 KB Output is correct
49 Correct 100 ms 29880 KB Output is correct
50 Correct 68 ms 26996 KB Output is correct
51 Correct 52 ms 27020 KB Output is correct
52 Correct 91 ms 28948 KB Output is correct
53 Correct 90 ms 29884 KB Output is correct
54 Correct 103 ms 29916 KB Output is correct
55 Correct 51 ms 26940 KB Output is correct
56 Correct 56 ms 27016 KB Output is correct
57 Correct 70 ms 28536 KB Output is correct
58 Correct 80 ms 28988 KB Output is correct
59 Correct 79 ms 29020 KB Output is correct
60 Correct 84 ms 29916 KB Output is correct
61 Correct 85 ms 29852 KB Output is correct
62 Correct 87 ms 29876 KB Output is correct
63 Correct 87 ms 29888 KB Output is correct
64 Correct 57 ms 26992 KB Output is correct
65 Correct 53 ms 26976 KB Output is correct
66 Correct 55 ms 26976 KB Output is correct
67 Correct 53 ms 26936 KB Output is correct
68 Correct 1074 ms 28920 KB Output is correct
69 Correct 1195 ms 28996 KB Output is correct
70 Correct 1126 ms 28928 KB Output is correct
71 Correct 1160 ms 29880 KB Output is correct
72 Correct 1145 ms 29824 KB Output is correct
73 Correct 1067 ms 29880 KB Output is correct
74 Correct 916 ms 29868 KB Output is correct
75 Correct 819 ms 26948 KB Output is correct
76 Correct 839 ms 27020 KB Output is correct
77 Correct 869 ms 27032 KB Output is correct
78 Correct 934 ms 27024 KB Output is correct
79 Correct 872 ms 26996 KB Output is correct
80 Correct 916 ms 26980 KB Output is correct
81 Correct 5 ms 9680 KB Output is correct
82 Correct 6 ms 9936 KB Output is correct
83 Correct 6 ms 9936 KB Output is correct
84 Correct 75 ms 29072 KB Output is correct
85 Correct 90 ms 29908 KB Output is correct
86 Correct 80 ms 29888 KB Output is correct
87 Correct 59 ms 26976 KB Output is correct
88 Correct 60 ms 26948 KB Output is correct
89 Correct 73 ms 28988 KB Output is correct
90 Correct 89 ms 29892 KB Output is correct
91 Correct 81 ms 29888 KB Output is correct
92 Correct 50 ms 26948 KB Output is correct
93 Correct 49 ms 26980 KB Output is correct
94 Correct 6 ms 9936 KB Output is correct
95 Correct 6 ms 10052 KB Output is correct
96 Correct 6 ms 9936 KB Output is correct
97 Correct 6 ms 9972 KB Output is correct
98 Correct 5 ms 9936 KB Output is correct
99 Correct 6 ms 9936 KB Output is correct
100 Correct 6 ms 9968 KB Output is correct
101 Correct 6 ms 9956 KB Output is correct
102 Correct 6 ms 9936 KB Output is correct
103 Correct 6 ms 9936 KB Output is correct
104 Correct 1004 ms 27584 KB Output is correct
105 Incorrect 1072 ms 28992 KB 22302nd lines differ - on the 1st token, expected: '1', found: '0'
106 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 456 ms 19016 KB 18th lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -