답안 #749889

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
749889 2023-05-28T20:31:02 Z taher Growing Trees (BOI11_grow) C++17
100 / 100
107 ms 3276 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "C:\GCC\debug.h"
#else
#define debug(...) void(42)
#endif

template<typename T>
class fenwick {
  public:
  int n;
  vector<T> fenw;

  fenwick(int _n) : n(_n) {
    fenw.resize(n);
  }

  void modify(int x, T v) {
    while (x < n) {
      fenw[x] += v;
      x |= (x + 1);
    }
  }

  T get(int x) {
    T v{};
    while (x >= 0) {
      v += fenw[x];
      x = (x & (x + 1)) - 1;
    }
    return v;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());

  fenwick<int> fenw(n);

  auto find_first = [&](int h) {
    // find smallest index such that Ai >= h
    int low = 0, high = n - 1;

    while (low <= high) {
      int mid = low + (high - low) / 2;

      if (a[mid] + fenw.get(mid) >= h) {
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    ++high;
    return high;
  };

  auto find_last = [&](int h) {
    // find biggest index such that Ai <= h
    int low = 0, high = n - 1;
    while (low <= high) {
      int mid = low + (high - low) / 2;

      if (a[mid] + fenw.get(mid) <= h) {
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    --low;
    return low;
  };

  auto Add = [&](int h, int c) {
    int i = find_first(h);
    int nxt = i + c;

    if (nxt >= n) {
      fenw.modify(i, +1);
      return ;
    }

    int x = a[nxt] + fenw.get(nxt);
    int y = a[nxt - 1] + fenw.get(nxt - 1);
    assert(y <= x);

    if (y < x) {
      fenw.modify(i, +1);
      fenw.modify(nxt, -1);
      return ;  
    }

    int segDeb = find_first(x);
    fenw.modify(i, +1);
    fenw.modify(segDeb, -1);
    int segEnd = find_last(x);
    int marge = nxt - segDeb;

    fenw.modify(segEnd - marge + 1, +1);
    if (segEnd + 1 < n) {
      fenw.modify(segEnd + 1, -1);
    }
    return ;
  };

  for (int i = 0; i < m; i++) {
    char type;
    cin >> type;

    if (type == 'F') {
      int c, h;
      cin >> c >> h;

      Add(h, c);
    } else {
      int mnH, mxH;
      cin >> mnH >> mxH;
      int x = find_first(mnH);
      int y = find_last(mxH);
      cout << y - x + 1 << '\n';
    }
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 2300 KB Output is correct
2 Correct 99 ms 2744 KB Output is correct
3 Correct 39 ms 2552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 328 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 48 ms 1384 KB Output is correct
6 Correct 55 ms 1584 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 29 ms 1132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 1544 KB Output is correct
2 Correct 53 ms 1756 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 35 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 1844 KB Output is correct
2 Correct 61 ms 1648 KB Output is correct
3 Correct 4 ms 468 KB Output is correct
4 Correct 58 ms 1704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 1936 KB Output is correct
2 Correct 95 ms 2372 KB Output is correct
3 Correct 18 ms 852 KB Output is correct
4 Correct 32 ms 2284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 1952 KB Output is correct
2 Correct 107 ms 2348 KB Output is correct
3 Correct 42 ms 2396 KB Output is correct
4 Correct 17 ms 832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 2076 KB Output is correct
2 Correct 78 ms 2380 KB Output is correct
3 Correct 36 ms 2548 KB Output is correct
4 Correct 17 ms 832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 2752 KB Output is correct
2 Correct 100 ms 2288 KB Output is correct
3 Correct 26 ms 1812 KB Output is correct
4 Correct 63 ms 2172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 2508 KB Output is correct
2 Correct 93 ms 2692 KB Output is correct
3 Correct 82 ms 2860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 3276 KB Output is correct