Submission #968877

# Submission time Handle Problem Language Result Execution time Memory
968877 2024-04-24T08:21:10 Z nguyentunglam Radio Towers (IOI22_towers) C++17
0 / 100
4000 ms 10304 KB
#include "towers.h"
#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

int h[N], lg[N], sp[20][N], L[N], R[N];

int n;

int get(int l, int r) {
  if (l > r) return -1e9;
  int k = lg[r - l + 1];
  return max(sp[k][l], sp[k][r - (1 << k) + 1]);
}

void init(int N, std::vector<int> H) {
  n = N;
  for(int i = 0; i < n; i++) h[i] = H[i];
  stack<int> st;
  for(int i = 0; i < n; i++) {
    while (!st.empty() && h[st.top()] >= h[i]) st.pop();
    L[i] = st.empty() ? -1 : st.top();
    st.push(i);
  }
  while (!st.empty()) st.pop();
  for(int i = n - 1; i >= 0; i--) {
    while (!st.empty() && h[st.top()] >= h[i]) st.pop();
    R[i] = st.empty() ? n : st.top();
    st.push(i);
  }

  for(int i = 0; i < n; i++) sp[0][i] = h[i];
  for(int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 1;
  for(int j = 1; j <= lg[n]; j++) for(int i = 0; i + (1 << j) - 1 < n; i++) {
    sp[j][i] = max(sp[j - 1][i], sp[j - 1][i + (1 << j - 1)]);
  }

}

int max_towers(int l, int r, int d) {
  int ans = 0;

  for(int i = l; i <= r; i++) {
    if (get(L[i] + 1, i - 1) - d < h[i]) continue;
    if (get(i + 1, R[i] - 1) - d < h[i]) continue;
    ans++;
  }

  int left = -1, right = -1;

  for(int i = l; i <= r; i++) {
    if (L[i] < l) {
      if (R[i] > r || get(i + 1, R[i] - 1) - d >= h[i]) {
        left = i;
        break;
      }
    }
  }

  for(int i = r; i >= l; i--) {
    if (R[i] > r) {
      if (L[i] < l || get(L[i] + 1, i - 1) - d >= h[i]) {
        right = i;
        break;
      }
    }
  }

  if (get(L[left] - 1, left - 1) - d < h[left]) ans += left >= 0;
  if (right != left && get(right + 1, R[right] - 1) - d < h[right]) ans += right >= 0;

  return ans;
}

#ifdef ngu
int main() {
  freopen ("task.inp", "r", stdin);
  freopen ("task.ans", "w", stdout);
  int N, Q;
  assert(2 == scanf("%d %d", &N, &Q));
  std::vector<int> H(N);
  for (int i = 0; i < N; ++i) {
    assert(1 == scanf("%d", &H[i]));
  }
  init(N, H);

  for (int i = 0; i < Q; ++i) {
    int L, R, D;
    assert(3 == scanf("%d %d %d", &L, &R, &D));
    printf("%d\n", max_towers(L, R, D));
  }
  return 0;
}
#endif // ngu

Compilation message

towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:36:56: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   36 |     sp[j][i] = max(sp[j - 1][i], sp[j - 1][i + (1 << j - 1)]);
      |                                                      ~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 3183 ms 9900 KB 2nd lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6488 KB Output is correct
5 Correct 1 ms 6488 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Incorrect 2 ms 6488 KB 1st lines differ - on the 1st token, expected: '34', found: '33'
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6488 KB Output is correct
5 Correct 1 ms 6488 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Incorrect 2 ms 6488 KB 1st lines differ - on the 1st token, expected: '34', found: '33'
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4026 ms 10304 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4027 ms 6924 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6488 KB Output is correct
5 Correct 1 ms 6488 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Incorrect 2 ms 6488 KB 1st lines differ - on the 1st token, expected: '34', found: '33'
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3183 ms 9900 KB 2nd lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -