Submission #670371

# Submission time Handle Problem Language Result Execution time Memory
670371 2022-12-08T19:25:58 Z MilosMilutinovic Radio Towers (IOI22_towers) C++17
0 / 100
602 ms 23696 KB
#include "towers.h"

#include <bits/stdc++.h>

using namespace std;

const int MAX = 100005;
const int MAXN = 20 * MAX;

int n, root[MAX], tsz, st[MAXN], ls[MAXN], rs[MAXN];

void Modify(int& v, int p, int l, int r, int i, int x) {
  v = ++tsz;
  ls[v] = ls[p];
  rs[v] = rs[p];
  st[v] = st[p] + x;
  if (l == r) {
    return;
  }
  int mid = l + r >> 1;
  if (i <= mid) {
    Modify(ls[v], ls[p], l, mid, i, x);
  } else {
    Modify(rs[v], rs[p], mid + 1, r, i, x);
  }
}

int Query(int v, int l, int r, int ll, int rr) {
  if (l > r || l > rr || r < ll) {
    return 0;
  }
  if (ll <= l && r <= rr) {
    return st[v];
  }
  int mid = l + r >> 1;
  return Query(ls[v], l, mid, ll, rr) + Query(rs[v], mid + 1, r, ll, rr);
}

vector<pair<int, int>> qp;

void init(int n, std::vector<int> h) {
  ::n = n;
  vector<int> L(n);
  vector<int> R(n);
  vector<int> stk;
  for (int i = 0; i < n; i++) {
    while (!stk.empty() && h[i] < h[stk.back()]) {
      stk.pop_back();
    }
    L[i] = (stk.empty() ? -1 : stk.back());
    stk.push_back(i);
  }
  stk.clear();
  for (int i = n - 1; i >= 0; i--) {
    while (!stk.empty() && h[i] < h[stk.back()]) {
      stk.pop_back();
    }
    R[i] = (stk.empty() ? -1 : stk.back());
    stk.push_back(i);
  }
  for (int i = 0; i < n; i++) {
    if (L[i] == -1 || R[i] == -1) {
      continue;
    }
    int mx = max(h[L[i]], h[R[i]]);
    qp.push_back({h[i] - mx, i});
  }
  sort(qp.begin(), qp.end());
  int prv = 0;
  for (int i = 0; i < (int) qp.size(); i++) {
    Modify(root[i], prv, 0, n - 1, qp[i].second, +1);
    prv = root[i];
  }
}

int max_towers(int L, int R, int D) {
  if (qp.empty())
    return 1;
  int pos = lower_bound(qp.begin(), qp.end(), make_pair(D, -1)) - qp.begin();
  while (pos >= 0 && (pos >= (int) qp.size() || qp[pos].second > D)) pos--;
  if (pos == -1)
    return 1;
  return Query(root[pos], 0, n - 1, L, R) + 1;
}

Compilation message

towers.cpp: In function 'void Modify(int&, int, int, int, int, int)':
towers.cpp:20:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   20 |   int mid = l + r >> 1;
      |             ~~^~~
towers.cpp: In function 'int Query(int, int, int, int, int)':
towers.cpp:35:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   35 |   int mid = l + r >> 1;
      |             ~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 456 ms 14104 KB 1st lines differ - on the 1st token, expected: '1', found: '21741'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '13', found: '23'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '13', found: '23'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 602 ms 23696 KB 1st lines differ - on the 1st token, expected: '11903', found: '1'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 232 ms 5452 KB 1st lines differ - on the 1st token, expected: '7197', found: '11745'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '13', found: '23'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 456 ms 14104 KB 1st lines differ - on the 1st token, expected: '1', found: '21741'
2 Halted 0 ms 0 KB -