Submission #702346

# Submission time Handle Problem Language Result Execution time Memory
702346 2023-02-23T15:43:04 Z dykw Radio Towers (IOI22_towers) C++17
17 / 100
986 ms 35604 KB
#include "towers.h"

#include <bits/stdc++.h>

using namespace std;

constexpr int MaxN = 100000;
constexpr int LogN = 16;
constexpr int Inf = 1E9;

namespace RMQ {
  int st[LogN + 1][MaxN];
  
  void build(vector<int> &H) {
    int N = H.size();
    for (int i = 0; i < N; ++i) {
      st[0][i] = H[i];
    }
    for (int i = 1; (1 << i) <= N; ++i) {
      for (int j = 0; j + (1 << i) - 1 < N; ++j) {
        st[i][j] = max(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
      }
    }
  }
  
  int ask(int l, int r) {
    if (l > r) {
      return -1;
    }
    int k = __lg(r - l + 1);
    return max(st[k][l], st[k][r - (1 << k) + 1]);
  }
}

namespace T {
  int val_l[MaxN * 4], val_r[MaxN * 4];
  
  void build(int l, int r, int x, const vector<int> &vl, const vector<int> &vr) {
    if (l == r) {
      val_l[x] = vl[l];
      val_r[x] = vr[l];
      return;
    }
    int mid = (l + r) / 2;
    build(l, mid, x * 2, vl, vr);
    build(mid + 1, r, x * 2 + 1, vl, vr);
    val_l[x] = max(val_l[x * 2], val_l[x * 2 + 1]);
    val_r[x] = max(val_r[x * 2], val_r[x * 2 + 1]);
  }
  
  int find_l(int l, int r, int ql, int qr, int delta, int x) {
    if (val_r[x] < delta) {
      return MaxN;
    }
    if (l == r) {
      return l;
    }
    int mid = (l + r) / 2;
    if (ql <= l && r <= qr) {
      if (val_r[x * 2] >= delta) {
        return find_l(l, mid, ql, qr, delta, x * 2);
      } else {
        return find_l(mid + 1, r, ql, qr, delta, x * 2 + 1);
      }
    }
    if (ql <= mid) {
      int res = find_l(l, mid, ql, qr, delta, x * 2);
      if (res != MaxN) {
        return res;
      }
    }
    return find_l(mid + 1, r, ql, qr, delta, x * 2 + 1);
  }
  
  int find_r(int l, int r, int ql, int qr, int delta, int x) {
    if (val_l[x] < delta) {
      return -1;
    }
    if (l == r) {
      return l;
    }
    int mid = (l + r) / 2;
    if (ql <= l && r <= qr) {
      if (val_l[x * 2 + 1] >= delta) {
        return find_r(mid + 1, r, ql, qr, delta, x * 2 + 1);
      } else {
        return find_r(l, mid, ql, qr, delta, x * 2);
      }
    }
    if (qr > mid) {
      int res = find_r(mid + 1, r, ql, qr, delta, x * 2 + 1);
      if (res != -1) {
        return res;
      }
    }
    return find_r(l, mid, ql, qr, delta, x * 2);
  }
}

namespace PerT {
  struct Node {
    int l, r;
    int v;
    Node() : l(), r(), v() {}
  };
  vector<Node> t(1);
  
  int insert(int l, int r, int p, int x) {
    int y = t.size();
    t.push_back(t[x]);
    ++t[y].v;
    if (l == r) {
      return y;
    }
    int mid = (l + r) / 2;
    if (p <= mid) {
      t[y].l = insert(l, mid, p, t[x].l);
    } else {
      t[y].r = insert(mid + 1, r, p, t[x].r);
    }
    return y;
  }
  
  int ask(int l, int r, int ql, int qr, int x, int y) {
    if (ql <= l && r <= qr) {
      return t[y].v - t[x].v;
    }
    int mid = (l + r) / 2;
    if (ql > mid) {
      return ask(mid + 1, r, ql, qr, t[x].r, t[y].r);
    } else if (qr <= mid) {
      return ask(l, mid, ql, qr, t[x].l, t[y].l);
    } else {
      return ask(l, mid, ql, qr, t[x].l, t[y].l) + ask(mid + 1, r, ql, qr, t[x].r, t[y].r);
    }
  }
}

int N, root[MaxN + 1];

void init(int N_, vector<int> H) {
  N = N_;
  RMQ::build(H);
  
  vector<int> stk(N + 1);
  int top = 0;
  
  vector<int> l(N), r(N);
  for (int i = 0; i < N; ++i) {
    while (top && H[stk[top]] > H[i]) {
      --top;
    }
    l[i] = top ? RMQ::ask(stk[top] + 1, i - 1) : Inf;
    if (top && l[i] > 0) {
      l[i] -= H[i];
    }
    stk[++top] = i;
  }
  top = 0;
  for (int i = N - 1; i >= 0; --i) {
    while (top && H[stk[top]] > H[i]) {
      --top;
    }
    r[i] = top ? RMQ::ask(i + 1, stk[top] - 1) : Inf;
    if (top && r[i] > 0) {
      r[i] -= H[i];
    }
    stk[++top] = i;
  }
  
  T::build(0, N - 1, 1, l, r);
  for (int i = 0; i < N; ++i) {
    if (l[i] == -1 || r[i] == -1) {
      root[i + 1] = root[i];
      continue;
    }
    root[i + 1] = PerT::insert(0, Inf, min(l[i], r[i]), root[i]);
  }
}

int max_towers(int L, int R, int D) {
  int avail_l = T::find_l(L, R, 0, N - 1, D, 1);
  int avail_r = T::find_r(L, R, 0, N - 1, D, 1);
  if (avail_l > avail_r || avail_r - avail_l <= 1) {
    return 1;
  }
  return PerT::ask(0, Inf, D, Inf, root[avail_l + 1], root[avail_r]) + 2;
}

#ifdef LOCAL_GRADER

int main() {
  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
# Verdict Execution time Memory Grader output
1 Incorrect 345 ms 6224 KB 1st lines differ - on the 1st token, expected: '1', found: '2'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 464 KB 1st lines differ - on the 1st token, expected: '13', found: '15'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 464 KB 1st lines differ - on the 1st token, expected: '13', found: '15'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 807 ms 23064 KB 1st lines differ - on the 1st token, expected: '11903', found: '11904'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 271 ms 5828 KB Output is correct
2 Correct 936 ms 23044 KB Output is correct
3 Correct 702 ms 23064 KB Output is correct
4 Correct 986 ms 35500 KB Output is correct
5 Correct 967 ms 35504 KB Output is correct
6 Correct 792 ms 35524 KB Output is correct
7 Correct 792 ms 35492 KB Output is correct
8 Correct 889 ms 10800 KB Output is correct
9 Correct 751 ms 10820 KB Output is correct
10 Correct 904 ms 10824 KB Output is correct
11 Correct 714 ms 10804 KB Output is correct
12 Correct 45 ms 23144 KB Output is correct
13 Correct 60 ms 35448 KB Output is correct
14 Correct 69 ms 35492 KB Output is correct
15 Correct 21 ms 10820 KB Output is correct
16 Correct 24 ms 10808 KB Output is correct
17 Correct 41 ms 22872 KB Output is correct
18 Correct 42 ms 23136 KB Output is correct
19 Correct 53 ms 23140 KB Output is correct
20 Correct 64 ms 35436 KB Output is correct
21 Correct 62 ms 35604 KB Output is correct
22 Correct 70 ms 35496 KB Output is correct
23 Correct 59 ms 35464 KB Output is correct
24 Correct 19 ms 10792 KB Output is correct
25 Correct 18 ms 10824 KB Output is correct
26 Correct 18 ms 10796 KB Output is correct
27 Correct 17 ms 10804 KB Output is correct
28 Correct 1 ms 1040 KB Output is correct
29 Correct 1 ms 1040 KB Output is correct
30 Correct 2 ms 1040 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 720 KB Output is correct
34 Correct 1 ms 1040 KB Output is correct
35 Correct 1 ms 1040 KB Output is correct
36 Correct 1 ms 1040 KB Output is correct
37 Correct 1 ms 1040 KB Output is correct
38 Correct 1 ms 1040 KB Output is correct
39 Correct 1 ms 1040 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
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 464 KB 1st lines differ - on the 1st token, expected: '13', found: '15'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 345 ms 6224 KB 1st lines differ - on the 1st token, expected: '1', found: '2'
2 Halted 0 ms 0 KB -