답안 #1097480

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1097480 2024-10-07T13:01:34 Z avighna Meteors (POI11_met) C++17
100 / 100
1431 ms 38400 KB
#include <bits/stdc++.h>

class FenwickTree {
public:
  std::vector<long long> f;
  int n;

  FenwickTree(int n) : n(n) { f.resize(n + 1); }

  void add(int idx, int del) {
    for (int i = idx; i <= n; i += i & (-i)) {
      f[i] += del;
    }
  }

  long long query(int idx) {
    long long ans = 0;
    for (int i = idx; i >= 1; i -= i & (-i)) {
      ans += f[i];
    }
    return ans;
  }
};

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n, m;
  std::cin >> n >> m;
  std::vector owned(n + 1, std::vector<int>());
  for (int i = 1, x; i <= m; ++i) {
    std::cin >> x;
    owned[x].push_back(i);
  }
  std::vector<long long> p(n + 1);
  for (int i = 1; i <= n; ++i) {
    std::cin >> p[i];
  }
  int k;
  std::cin >> k;
  struct Update {
    int l, r, a;
  };
  std::vector<Update> upd(k);
  for (auto &[l, r, a] : upd) {
    std::cin >> l >> r >> a;
  }

  struct State {
    int i, l, r;
  };
  std::vector<State> states;
  for (int i = 0; i < n; ++i) {
    states.push_back({i, 0, k - 1});
  }
  auto comp = [](const State &a, const State &b) {
    return (a.l + a.r) / 2 > (b.l + b.r) / 2;
  };
  std::sort(states.begin(), states.end(), comp);
  std::vector<int> ans(n, k);
  while (true) {
    std::vector<State> new_states;
    bool changed = false;
    FenwickTree tree(m + 1);
    auto apply = [&](int l, int r, int d) {
      tree.add(l, d);
      tree.add(r + 1, -d);
    };
    for (int i = 0; i < k; ++i) {
      if (upd[i].l <= upd[i].r) {
        apply(upd[i].l, upd[i].r, upd[i].a);
      } else {
        apply(upd[i].l, m, upd[i].a);
        apply(1, upd[i].r, upd[i].a);
      }
      while (!states.empty() and (states.back().l + states.back().r) / 2 == i) {
        changed = true;
        auto [_i, l, r] = states.back();
        states.pop_back();
        if (l > r) {
          continue;
        }
        long long obtained = 0;
        for (auto &x : owned[_i + 1]) {
          obtained += tree.query(x);
          if (obtained >= p[_i + 1]) {
            break;
          }
        }
        if (obtained >= p[_i + 1]) {
          ans[_i] = std::min(ans[_i], i);
        }
        if (l < r) {
          if (obtained >= p[_i + 1] and l + i - 1 >= 0) {
            new_states.push_back({_i, l, i - 1});
          }
          if (obtained < p[_i + 1] and i + r + 1 < 2 * k) {
            new_states.push_back({_i, i + 1, r});
          }
        }
      }
    }
    if (!changed) {
      break;
    }
    std::sort(new_states.begin(), new_states.end(), comp);
    states = std::move(new_states);
  }
  for (auto &i : ans) {
    if (i == k) {
      std::cout << "NIE\n";
    } else {
      std::cout << i + 1 << '\n';
    }
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 3096 KB Output is correct
2 Correct 100 ms 5032 KB Output is correct
3 Correct 68 ms 3964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 3608 KB Output is correct
2 Correct 71 ms 3612 KB Output is correct
3 Correct 102 ms 5220 KB Output is correct
4 Correct 24 ms 2976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 3024 KB Output is correct
2 Correct 82 ms 5260 KB Output is correct
3 Correct 30 ms 1624 KB Output is correct
4 Correct 71 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 2648 KB Output is correct
2 Correct 89 ms 3812 KB Output is correct
3 Correct 53 ms 2908 KB Output is correct
4 Correct 91 ms 5292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 981 ms 25132 KB Output is correct
2 Correct 598 ms 15440 KB Output is correct
3 Correct 164 ms 5724 KB Output is correct
4 Correct 1154 ms 35304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 978 ms 23864 KB Output is correct
2 Correct 631 ms 13784 KB Output is correct
3 Correct 136 ms 6228 KB Output is correct
4 Correct 1431 ms 38400 KB Output is correct