Luzhiled's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub ei1333/library

:heavy_check_mark: test/verify/yosupo-point-add-rectangle-sum-4.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/point_add_rectangle_sum

#include "../../template/template.hpp"

#include "../../structure/segment-tree/range-tree.hpp"
#include "../../structure/class/point-add-rectangle-sum.hpp"

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> X(N), Y(N), W(N), c(Q), s(Q), t(Q), u(Q), v(Q);
  using monoid = PointAddRectangleSum< int64 >;
  RangeTree< int, monoid > seg{monoid()};
  for(int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> W[i];
    seg.add_point(X[i], Y[i]);
  }
  for(int i = 0; i < Q; i++) {
    cin >> c[i] >> s[i] >> t[i] >> u[i];
    if (c[i]) cin >> v[i];
    else seg.add_point(s[i], t[i]);
  }
  seg.build();
  for(int i = 0; i < N; i++) {
    seg.apply(X[i], Y[i], W[i]);
  }
  for(int i = 0; i < Q; i++) {
    if (c[i]) cout << seg.prod(s[i], t[i], u[i], v[i]) << "\n";
    else seg.apply(s[i], t[i], u[i]);
  }
}
#line 1 "test/verify/yosupo-point-add-rectangle-sum-4.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/point_add_rectangle_sum

#line 1 "template/template.hpp"
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif

using namespace std;

using int64 = long long;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

template <typename T = int64>
vector<T> make_v(size_t a) {
  return vector<T>(a);
}

template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t) fill_v(e, v);
}

template <typename F>
struct FixPoint : F {
  explicit FixPoint(F &&f) : F(std::forward<F>(f)) {}

  template <typename... Args>
  decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};

template <typename F>
inline decltype(auto) MFP(F &&f) {
  return FixPoint<F>{std::forward<F>(f)};
}
#line 4 "test/verify/yosupo-point-add-rectangle-sum-4.test.cpp"

#line 1 "structure/segment-tree/range-tree.hpp"
template <typename K, typename Monoid2D>
struct RangeTree {
  using S = typename Monoid2D::S;
  using D = typename Monoid2D::D;

 private:
  vector<vector<K> > ys;
  vector<pair<K, K> > ps;
  vector<K> xs;
  vector<D> ds;
  int n;

  Monoid2D m;

  int id(K x) const {
    return lower_bound(xs.begin(), xs.end(), x) - xs.begin();
  }

  int id(int k, K y) const {
    return lower_bound(ys[k].begin(), ys[k].end(), y) - ys[k].begin();
  }

 public:
  RangeTree() = default;

  explicit RangeTree(Monoid2D m) : m(m) {}

  void add_point(K x, K y) { ps.emplace_back(x, y); }

  void build() {
    sort(ps.begin(), ps.end());
    ps.erase(unique(ps.begin(), ps.end()), ps.end());
    n = (int)ps.size();
    xs.reserve(n);
    for (auto& [x, _] : ps) {
      xs.emplace_back(x);
    }
    ys.resize(2 * n);
    ds.resize(2 * n);
    for (int i = 0; i < n; i++) {
      ys[i + n] = {ps[i].second};
      ds[i + n] = m.init(1);
    }
    for (int i = n - 1; i > 0; i--) {
      ys[i].resize(ys[i << 1].size() + ys[(i << 1) | 1].size());
      merge(ys[i << 1].begin(), ys[i << 1].end(), ys[(i << 1) | 1].begin(),
            ys[(i << 1) | 1].end(), ys[i].begin());
      ys[i].erase(unique(ys[i].begin(), ys[i].end()), ys[i].end());
      ds[i] = m.init((int)ys[i].size());
    }
  }

  void apply(K x, K y, S a) {
    int k = lower_bound(ps.begin(), ps.end(), make_pair(x, y)) - ps.begin();
    assert(ps[k] == make_pair(x, y));
    k += n;
    while (k > 0) {
      m.apply(ds[k], id(k, y), a);
      k >>= 1;
    }
  }

  S prod(K l, K d, K r, K u) {
    int a = id(l), b = id(r);
    a += n;
    b += n;
    S L = m.e(), R = m.e();
    while (a < b) {
      if (a & 1) {
        L = m.op(L, m.prod(ds[a], id(a, d), id(a, u)));
        ++a;
      }
      if (b & 1) {
        --b;
        R = m.op(m.prod(ds[b], id(b, d), id(b, u)), R);
      }
      a >>= 1;
      b >>= 1;
    }
    return m.op(L, R);
  }
};
#line 1 "structure/others/binary-indexed-tree.hpp"
template <typename T>
struct BinaryIndexedTree {
 private:
  int n;
  vector<T> data;

 public:
  BinaryIndexedTree() = default;

  explicit BinaryIndexedTree(int n) : n(n) { data.assign(n + 1, T()); }

  explicit BinaryIndexedTree(const vector<T> &v)
      : BinaryIndexedTree((int)v.size()) {
    build(v);
  }

  void build(const vector<T> &v) {
    assert(n == (int)v.size());
    for (int i = 1; i <= n; i++) data[i] = v[i - 1];
    for (int i = 1; i <= n; i++) {
      int j = i + (i & -i);
      if (j <= n) data[j] += data[i];
    }
  }

  void apply(int k, const T &x) {
    for (++k; k <= n; k += k & -k) data[k] += x;
  }

  T prod(int r) const {
    T ret = T();
    for (; r > 0; r -= r & -r) ret += data[r];
    return ret;
  }

  T prod(int l, int r) const { return prod(r) - prod(l); }

  int lower_bound(T x) const {
    int i = 0;
    for (int k = 1 << (__lg(n) + 1); k > 0; k >>= 1) {
      if (i + k <= n && data[i + k] < x) {
        x -= data[i + k];
        i += k;
      }
    }
    return i;
  }

  int upper_bound(T x) const {
    int i = 0;
    for (int k = 1 << (__lg(n) + 1); k > 0; k >>= 1) {
      if (i + k <= n && data[i + k] <= x) {
        x -= data[i + k];
        i += k;
      }
    }
    return i;
  }
};
#line 2 "structure/class/point-add-rectangle-sum.hpp"

template <typename T>
struct PointAddRectangleSum {
  using S = T;
  using D = BinaryIndexedTree<T>;
  static constexpr S op(const S& a, const S& b) { return a + b; }
  static constexpr S e() { return 0; }
  static constexpr D init(int n) { return D{n}; }
  static constexpr void apply(D& d, int k, const S& v) { d.apply(k, v); }
  static constexpr S prod(D& d, int l, int r) { return d.prod(l, r); }
};
#line 7 "test/verify/yosupo-point-add-rectangle-sum-4.test.cpp"

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> X(N), Y(N), W(N), c(Q), s(Q), t(Q), u(Q), v(Q);
  using monoid = PointAddRectangleSum< int64 >;
  RangeTree< int, monoid > seg{monoid()};
  for(int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> W[i];
    seg.add_point(X[i], Y[i]);
  }
  for(int i = 0; i < Q; i++) {
    cin >> c[i] >> s[i] >> t[i] >> u[i];
    if (c[i]) cin >> v[i];
    else seg.add_point(s[i], t[i]);
  }
  seg.build();
  for(int i = 0; i < N; i++) {
    seg.apply(X[i], Y[i], W[i]);
  }
  for(int i = 0; i < Q; i++) {
    if (c[i]) cout << seg.prod(s[i], t[i], u[i], v[i]) << "\n";
    else seg.apply(s[i], t[i], u[i]);
  }
}

Test cases

Env Name Status Elapsed Memory
g++ example_00 :heavy_check_mark: AC 5 ms 3 MB
g++ many_points_00 :heavy_check_mark: AC 588 ms 85 MB
g++ many_queries_00 :heavy_check_mark: AC 538 ms 55 MB
g++ max_random_00 :heavy_check_mark: AC 579 ms 70 MB
g++ max_random_01 :heavy_check_mark: AC 578 ms 70 MB
g++ power_of_2_00 :heavy_check_mark: AC 305 ms 32 MB
g++ power_of_2_01 :heavy_check_mark: AC 305 ms 32 MB
g++ random_00 :heavy_check_mark: AC 212 ms 28 MB
g++ random_01 :heavy_check_mark: AC 236 ms 37 MB
g++ random_02 :heavy_check_mark: AC 284 ms 40 MB
g++ small_00 :heavy_check_mark: AC 6 ms 4 MB
g++ small_01 :heavy_check_mark: AC 5 ms 4 MB
g++ small_02 :heavy_check_mark: AC 5 ms 4 MB
g++ small_03 :heavy_check_mark: AC 5 ms 4 MB
g++ small_04 :heavy_check_mark: AC 6 ms 4 MB
g++ xy_concentrate_00 :heavy_check_mark: AC 387 ms 55 MB
g++ xy_concentrate_01 :heavy_check_mark: AC 386 ms 55 MB
g++ xy_concentrate_02 :heavy_check_mark: AC 386 ms 55 MB
clang++ example_00 :heavy_check_mark: AC 5 ms 3 MB
clang++ many_points_00 :heavy_check_mark: AC 749 ms 85 MB
clang++ many_queries_00 :heavy_check_mark: AC 583 ms 55 MB
clang++ max_random_00 :heavy_check_mark: AC 677 ms 70 MB
clang++ max_random_01 :heavy_check_mark: AC 667 ms 70 MB
clang++ power_of_2_00 :heavy_check_mark: AC 297 ms 32 MB
clang++ power_of_2_01 :heavy_check_mark: AC 291 ms 32 MB
clang++ random_00 :heavy_check_mark: AC 212 ms 28 MB
clang++ random_01 :heavy_check_mark: AC 226 ms 37 MB
clang++ random_02 :heavy_check_mark: AC 312 ms 40 MB
clang++ small_00 :heavy_check_mark: AC 6 ms 4 MB
clang++ small_01 :heavy_check_mark: AC 5 ms 4 MB
clang++ small_02 :heavy_check_mark: AC 5 ms 4 MB
clang++ small_03 :heavy_check_mark: AC 5 ms 4 MB
clang++ small_04 :heavy_check_mark: AC 6 ms 4 MB
clang++ xy_concentrate_00 :heavy_check_mark: AC 398 ms 55 MB
clang++ xy_concentrate_01 :heavy_check_mark: AC 400 ms 55 MB
clang++ xy_concentrate_02 :heavy_check_mark: AC 396 ms 55 MB
Back to top page