kyopro-lib

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:heavy_check_mark: test/aoj/3165/main.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=3165"

#include <iostream>
#include "Mylib/DataStructure/SegmentTree/segment_tree_linear_add.cpp"
#include "Mylib/IO/input_tuples.cpp"
#include "Mylib/IO/join.cpp"

namespace hl = haar_lib;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int N, Q;
  std::cin >> N >> Q;

  auto seg = hl::segment_tree_linear_add<int64_t>(N);

  for (auto [l, k] : hl::input_tuples<int, int>(Q)) {
    --l;
    seg.update(l, l + k, 1, 1);
  }

  auto ans = seg.get_all(N);
  std::cout << hl::join(ans.begin(), ans.end()) << "\n";

  return 0;
}
#line 1 "test/aoj/3165/main.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=3165"

#include <iostream>
#line 2 "Mylib/DataStructure/SegmentTree/segment_tree_linear_add.cpp"
#include <cassert>
#include <utility>
#include <vector>

namespace haar_lib {
  template <typename T>
  class segment_tree_linear_add {
  public:
    using value_type = T;

  private:
    using P = std::pair<T, T>;

    int depth_, size_, hsize_;
    std::vector<P> data_;
    std::vector<int> from_;

    P add(P a, P b) {
      return {a.first + b.first, a.second + b.second};
    }

    void propagate(int i) {
      if (i < hsize_) {
        data_[i << 1 | 0] = add(data_[i << 1 | 0], data_[i]);

        const int len = hsize_ >> (31 - __builtin_clz(i) + 1);
        data_[i].first += data_[i].second * len;
        data_[i << 1 | 1] = add(data_[i << 1 | 1], data_[i]);

        data_[i] = P();
      }
    }

    void propagate_top_down(int i) {
      std::vector<int> temp;
      while (i > 1) {
        i >>= 1;
        temp.push_back(i);
      }

      for (auto it = temp.rbegin(); it != temp.rend(); ++it) propagate(*it);
    }

  public:
    segment_tree_linear_add() {}
    segment_tree_linear_add(int n) : depth_(n > 1 ? 32 - __builtin_clz(n - 1) + 1 : 1),
                                     size_(1 << depth_),
                                     hsize_(size_ / 2),
                                     data_(size_, P()),
                                     from_(size_) {
      int s = 0;
      for (int i = 1; i < size_; ++i) {
        from_[i] = s;
        int l    = hsize_ >> (31 - __builtin_clz(i));
        s += l;
        if (s == hsize_) s = 0;
      }
    }

    void update(int l, int r, T a, T b) {
      assert(0 <= l and l <= r and r <= hsize_);
      int L = l + hsize_;
      int R = r + hsize_;

      while (L < R) {
        if (R & 1) {
          --R;
          data_[R] = add(std::make_pair(b + a * (from_[R] - l), a), data_[R]);
        }
        if (L & 1) {
          data_[L] = add(std::make_pair(b + a * (from_[L] - l), a), data_[L]);
          ++L;
        }
        L >>= 1;
        R >>= 1;
      }
    }

    T operator[](int i) {
      assert(0 <= i and i < hsize_);
      propagate_top_down(i + hsize_);
      return data_[i + hsize_].first;
    }

    std::vector<T> get_all(int n) {
      std::vector<T> ret(n);
      for (int i = 1; i < hsize_; ++i) propagate(i);
      for (int i = hsize_; i < hsize_ + n; ++i) ret[i - hsize_] = data_[i].first;
      return ret;
    }
  };
}  // namespace haar_lib
#line 2 "Mylib/IO/input_tuples.cpp"
#include <initializer_list>
#line 4 "Mylib/IO/input_tuples.cpp"
#include <tuple>
#line 6 "Mylib/IO/input_tuple.cpp"

namespace haar_lib {
  template <typename T, size_t... I>
  static void input_tuple_helper(std::istream &s, T &val, std::index_sequence<I...>) {
    (void) std::initializer_list<int>{(void(s >> std::get<I>(val)), 0)...};
  }

  template <typename T, typename U>
  std::istream &operator>>(std::istream &s, std::pair<T, U> &value) {
    s >> value.first >> value.second;
    return s;
  }

  template <typename... Args>
  std::istream &operator>>(std::istream &s, std::tuple<Args...> &value) {
    input_tuple_helper(s, value, std::make_index_sequence<sizeof...(Args)>());
    return s;
  }
}  // namespace haar_lib
#line 8 "Mylib/IO/input_tuples.cpp"

namespace haar_lib {
  template <typename... Args>
  class InputTuples {
    struct iter {
      using value_type = std::tuple<Args...>;
      value_type value;
      bool fetched = false;
      int N, c = 0;

      value_type operator*() {
        if (not fetched) {
          std::cin >> value;
        }
        return value;
      }

      void operator++() {
        ++c;
        fetched = false;
      }

      bool operator!=(iter &) const {
        return c < N;
      }

      iter(int N) : N(N) {}
    };

    int N;

  public:
    InputTuples(int N) : N(N) {}

    iter begin() const { return iter(N); }
    iter end() const { return iter(N); }
  };

  template <typename... Args>
  auto input_tuples(int N) {
    return InputTuples<Args...>(N);
  }
}  // namespace haar_lib
#line 3 "Mylib/IO/join.cpp"
#include <sstream>
#include <string>

namespace haar_lib {
  template <typename Iter>
  std::string join(Iter first, Iter last, std::string delim = " ") {
    std::stringstream s;

    for (auto it = first; it != last; ++it) {
      if (it != first) s << delim;
      s << *it;
    }

    return s.str();
  }
}  // namespace haar_lib
#line 7 "test/aoj/3165/main.test.cpp"

namespace hl = haar_lib;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int N, Q;
  std::cin >> N >> Q;

  auto seg = hl::segment_tree_linear_add<int64_t>(N);

  for (auto [l, k] : hl::input_tuples<int, int>(Q)) {
    --l;
    seg.update(l, l + k, 1, 1);
  }

  auto ans = seg.get_all(N);
  std::cout << hl::join(ans.begin(), ans.end()) << "\n";

  return 0;
}
Back to top page