답안 #748225

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
748225 2023-05-25T17:53:10 Z vjudge1 Growing Trees (BOI11_grow) C++17
100 / 100
104 ms 3348 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 63 ms 2284 KB Output is correct
2 Correct 104 ms 2820 KB Output is correct
3 Correct 35 ms 2560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 3 ms 336 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 45 ms 1372 KB Output is correct
6 Correct 55 ms 1628 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 26 ms 1076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 1624 KB Output is correct
2 Correct 55 ms 1788 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 33 ms 1268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 1808 KB Output is correct
2 Correct 62 ms 1696 KB Output is correct
3 Correct 5 ms 468 KB Output is correct
4 Correct 56 ms 1688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 1976 KB Output is correct
2 Correct 98 ms 2268 KB Output is correct
3 Correct 18 ms 836 KB Output is correct
4 Correct 32 ms 2276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 2100 KB Output is correct
2 Correct 103 ms 2276 KB Output is correct
3 Correct 34 ms 2536 KB Output is correct
4 Correct 16 ms 760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 2076 KB Output is correct
2 Correct 63 ms 2308 KB Output is correct
3 Correct 35 ms 2636 KB Output is correct
4 Correct 18 ms 800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 2692 KB Output is correct
2 Correct 102 ms 2260 KB Output is correct
3 Correct 26 ms 1868 KB Output is correct
4 Correct 63 ms 2124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 2564 KB Output is correct
2 Correct 94 ms 2592 KB Output is correct
3 Correct 82 ms 2876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 3348 KB Output is correct