답안 #414013

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
414013 2021-05-29T18:43:02 Z Alex_tz307 Growing Trees (BOI11_grow) C++17
100 / 100
143 ms 4544 KB
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

const int MAXN = 1e5 + 5;
int a[MAXN];

struct SegTree {
  int Size;
  vector<int> tree, lazy;

  SegTree(int N) {
    Size = 1;
    while (Size < N)
      Size <<= 1;
    tree.resize(Size << 1);
    lazy.resize(Size << 1);
  }

  void build(int x, int lx, int rx) {
    if (lx == rx) {
      tree[x] = a[lx];
      return;
    }
    int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
    build(lSon, lx, mid);
    build(rSon, mid + 1, rx);
    tree[x] = max(tree[lSon], tree[rSon]);
  }

  void push(int x) {
    if (lazy[x] == 0)
      return;
    for (int i = 0; i < 2; ++i) {
      int son = x << 1 | i;
      tree[son] += lazy[x];
      lazy[son] += lazy[x];
    }
    lazy[x] = 0;
  }

  void update(int x, int lx, int rx, int st, int dr) {
    if (st <= lx && rx <= dr) {
      ++tree[x], ++lazy[x];
      return;
    }
    push(x);
    int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
    if (st <= mid)
      update(lSon, lx, mid, st, dr);
    if (mid < dr)
      update(rSon, mid + 1, rx, st, dr);
    tree[x] = max(tree[lSon], tree[rSon]);
  }

  int get_first_greater_equal(int x, int lx, int rx, int val) {
    if (lx == rx)
      return lx;
    push(x);
    int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
    if (tree[lSon] >= val)
      return get_first_greater_equal(lSon, lx, mid, val);
    return get_first_greater_equal(rSon, mid + 1, rx, val);
  }

  int get_first_greater(int x, int lx, int rx, int val) {
    if (lx == rx)
      return lx;
    push(x);
    int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
    if (tree[lSon] > val)
      return get_first_greater(lSon, lx, mid, val);
    return get_first_greater(rSon, mid + 1, rx, val);
  }

  int get_val(int x, int lx, int rx, int poz) {
    if (lx == rx)
      return tree[x];
    push(x);
    int mid = (lx + rx) >> 1;
    if (poz <= mid)
      return get_val(x << 1, lx, mid, poz);
    return get_val(x << 1 | 1, mid + 1, rx, poz);
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int N, Q;
  cin >> N >> Q;
  for (int i = 1; i <= N; ++i)
    cin >> a[i];
  sort(a + 1, a + N + 1);
  a[++N] = INF;
  SegTree tree(N);
  tree.build(1, 1, N);
  for (int q = 0; q < Q; ++q) {
    char t;
    int x, y;
    cin >> t >> x >> y;
    if (t == 'F') {
      int first_poz = tree.get_first_greater_equal(1, 1, N, y);
      if (first_poz == N)
        continue;
      if (first_poz + x - 1 > N - 1) {
        tree.update(1, 1, N, first_poz, min(N, first_poz + x - 1));
        continue;
      }
      int last_poz = first_poz + x - 1;
      int val = tree.get_val(1, 1, N, last_poz);
      int last_poz_update = tree.get_first_greater_equal(1, 1, N, val) - 1;
      if (first_poz <= last_poz_update)
        tree.update(1, 1, N, first_poz, last_poz_update);
      int rem = last_poz - last_poz_update;
      if (rem == 0)
        continue;
      last_poz_update = tree.get_first_greater(1, 1, N, val) - 1;
      tree.update(1, 1, N, last_poz_update - rem + 1, last_poz_update);
    } else cout << tree.get_first_greater(1, 1, N, y) - tree.get_first_greater_equal(1, 1, N, x) << '\n';
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 2912 KB Output is correct
2 Correct 114 ms 4480 KB Output is correct
3 Correct 107 ms 4344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 3 ms 332 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 41 ms 660 KB Output is correct
6 Correct 52 ms 720 KB Output is correct
7 Correct 5 ms 460 KB Output is correct
8 Correct 26 ms 1216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 832 KB Output is correct
2 Correct 54 ms 1808 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 38 ms 1348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 844 KB Output is correct
2 Correct 68 ms 1696 KB Output is correct
3 Correct 9 ms 592 KB Output is correct
4 Correct 56 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 1724 KB Output is correct
2 Correct 104 ms 4000 KB Output is correct
3 Correct 16 ms 1228 KB Output is correct
4 Correct 102 ms 4040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 2684 KB Output is correct
2 Correct 106 ms 4088 KB Output is correct
3 Correct 119 ms 4292 KB Output is correct
4 Correct 16 ms 1212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 2788 KB Output is correct
2 Correct 77 ms 4032 KB Output is correct
3 Correct 116 ms 4320 KB Output is correct
4 Correct 15 ms 1244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 133 ms 2816 KB Output is correct
2 Correct 123 ms 2808 KB Output is correct
3 Correct 31 ms 3484 KB Output is correct
4 Correct 59 ms 3888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 2948 KB Output is correct
2 Correct 102 ms 4380 KB Output is correct
3 Correct 143 ms 4544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 3268 KB Output is correct