CP-templates

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

View the Project on GitHub Misuki743/CP-templates

:heavy_check_mark: test/rectangle_add_point_get.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/rectangle_add_point_get"

#include "../default/t.cpp"
#include "../ds/fenwickTree2D.cpp"
#include "../misc/compression.cpp"
#include "../ds_problem/rectangleAddPointGet.cpp"

signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  int n, q; cin >> n >> q;
  vector<tuple<int, int, int, int, ll>> rect(n);
  for(auto &[l, r, d, u, w] : rect)
    cin >> l >> d >> r >> u >> w;
  vector<array<int, 2>> query;
  vector<int> updT(n, 0);
  while(q--) {
    int op; cin >> op;
    if (op == 0) {
      int l, d, r, u; cin >> l >> d >> r >> u;
      ll w; cin >> w;
      rect.emplace_back(l, r, d, u, w);
      updT.emplace_back(ssize(query));
    } else if (op == 1) {
      int x, y; cin >> x >> y;
      query.push_back({x, y});
    }
  }

  for(ll ans : rectAddPointGet<int, ll>(rect, query, updT))
    cout << ans << '\n';

  return 0;
}
#line 1 "test/rectangle_add_point_get.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/rectangle_add_point_get"

#line 1 "default/t.cpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>

#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)

#define clock chrono::steady_clock::now().time_since_epoch().count()

using namespace std;

template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2> pr) {
  return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
  for(size_t i = 0; T x : arr) {
    os << x;
    if (++i != N) os << ' ';
  }
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
  for(size_t i = 0; T x : vec) {
    os << x;
    if (++i != size(vec)) os << ' ';
  }
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const set<T> &s) {
  for(size_t i = 0; T x : s) {
    os << x;
    if (++i != size(s)) os << ' ';
  }
  return os;
}
template<class T1, class T2>
ostream& operator<<(ostream& os, const map<T1, T2> &m) {
  for(size_t i = 0; pair<T1, T2> x : m) {
    os << x;
    if (++i != size(m)) os << ' ';
  }
  return os;
}

#ifdef DEBUG
#define dbg(...) cerr << '(', _do(#__VA_ARGS__), cerr << ") = ", _do2(__VA_ARGS__)
template<typename T> void _do(T &&x) { cerr << x; }
template<typename T, typename ...S> void _do(T &&x, S&&...y) { cerr << x << ", "; _do(y...); }
template<typename T> void _do2(T &&x) { cerr << x << endl; }
template<typename T, typename ...S> void _do2(T &&x, S&&...y) { cerr << x << ", "; _do2(y...); }
#else
#define dbg(...)
#endif

using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using max_heap = priority_queue<T>;

template<ranges::forward_range rng, class T = ranges::range_value_t<rng>, class OP = plus<T>>
void pSum(rng &v) {
  if (!v.empty())
    for(T p = v[0]; T &x : v | views::drop(1))
      x = p = OP()(p, x);
}
template<ranges::forward_range rng, class T = ranges::range_value_t<rng>, class OP>
void pSum(rng &v, OP op) {
  if (!v.empty())
    for(T p = v[0]; T &x : v | views::drop(1))
      x = p = op(p, x);
}

template<ranges::forward_range rng>
void Unique(rng &v) {
  ranges::sort(v);
  v.resize(unique(v.begin(), v.end()) - v.begin());
}

template<ranges::random_access_range rng>
rng invPerm(rng p) {
  rng ret = p;
  for(int i = 0; i < ssize(p); i++)
    ret[p[i]] = i;
  return ret;
}

template<ranges::random_access_range rng, ranges::random_access_range rng2>
rng Permute(rng v, rng2 p) {
  rng ret = v;
  for(int i = 0; i < ssize(p); i++)
    ret[p[i]] = v[i];
  return ret;
}

template<bool directed>
vector<vector<int>> readGraph(int n, int m, int base) {
  vector<vector<int>> g(n);
  for(int i = 0; i < m; i++) {
    int u, v; cin >> u >> v;
    u -= base, v -= base;
    g[u].emplace_back(v);
    if constexpr (!directed)
      g[v].emplace_back(u);
  }
  return g;
}

template<class T>
void setBit(T &msk, int bit, bool x) {
  msk = (msk & ~(T(1) << bit)) | (T(x) << bit);
}
template<class T> void flipBit(T &msk, int bit) { msk ^= T(1) << bit; }
template<class T> bool getBit(T msk, int bit) { return msk >> bit & T(1); }

template<class T>
T floorDiv(T a, T b) {
  if (b < 0) a *= -1, b *= -1;
  return a >= 0 ? a / b : (a - b + 1) / b;
}
template<class T>
T ceilDiv(T a, T b) {
  if (b < 0) a *= -1, b *= -1;
  return a >= 0 ? (a + b - 1) / b : a / b;
}

template<class T> bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T> bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
#line 1 "ds/fenwickTree2D.cpp"
//source: KACTL

/**
 * Author: Lukas Polacek
 * Date: 2009-10-30
 * License: CC0
 * Source: folklore/TopCoder
 * Description: Computes partial sums a[0] + a[1] + ... + a[pos - 1], and updates single elements a[i],
 * taking the difference between the old and new value.
 * Time: Both operations are $O(\log N)$.
 * Status: Stress-tested
 */
template<class T>
struct FT {
	vector<T> s;
	FT(int n) : s(n) {}
	void update(int pos, T dif) { // a[pos] += dif
		for (; pos < ssize(s); pos |= pos + 1) s[pos] += dif;
	}
	T query(int pos) { // sum of values in [0, pos)
		T res = 0;
		for (; pos > 0; pos &= pos - 1) res += s[pos-1];
		return res;
	}
	int lower_bound(T sum) {// min pos st sum of [0, pos] >= sum
		// Returns n if no sum is >= sum, or -1 if empty sum is.
		if (sum <= 0) return -1;
		int pos = 0;
		for (int pw = 1 << 25; pw; pw >>= 1) {
			if (pos + pw <= ssize(s) && s[pos + pw-1] < sum)
				pos += pw, sum -= s[pos-1];
		}
		return pos;
	}
};

/**
 * Author: Simon Lindholm
 * Date: 2017-05-11
 * License: CC0
 * Source: folklore
 * Description: Computes sums a[i,j] for all i<I, j<J, and increases single elements a[i,j].
 *  Requires that the elements to be updated are known in advance (call fakeUpdate() before init()).
 * Time: $O(\log^2 N)$. (Use persistent segment trees for $O(\log N)$.)
 * Status: stress-tested
 */
template<class T1, class T2>
struct FT2 {
	vector<vector<T1>> ys; vector<FT<T2>> ft;
	FT2(int limx) : ys(limx) {}
	void fakeUpdate(int x, T1 y) {
		for (; x < ssize(ys); x |= x + 1) ys[x].push_back(y);
	}
	void init() {
		for (vector<T1>& v : ys) ranges::sort(v), ft.emplace_back(ssize(v));
	}
	int ind(int x, T1 y) {
		return (int)(ranges::lower_bound(ys[x], y) - ys[x].begin()); }
	void update(int x, T1 y, T2 dif) {
		for (; x < ssize(ys); x |= x + 1)
			ft[x].update(ind(x, y), dif);
	}
	T2 query(int x, T1 y) {
		T2 sum = 0;
		for (; x; x &= x - 1)
			sum += ft[x-1].query(ind(x-1, y));
		return sum;
	}
};
#line 1 "misc/compression.cpp"
template<class T, bool duplicate = false>
struct compression {
  vector<int> ord;
  vector<T> val;

  compression(vector<T> &init) : val(init) { precompute(); }
  compression(int size = 0) { val.reserve(size); }

  void precompute() {
    vector<T> init = val;
    ord.resize(ssize(val));
    ranges::sort(val);
    if constexpr (duplicate) {
      vector<int> cnt(ssize(init));
      iota(cnt.begin(), cnt.end(), 0);
      for(int i = 0; i < ssize(ord); i++)
        ord[i] = cnt[lower_bound(init[i])]++;
    } else {
      val.resize(unique(val.begin(), val.end()) - val.begin());
      for(int i = 0; i < ssize(ord); i++)
        ord[i] = lower_bound(init[i]);
    }
  }

  int lower_bound(T x) { return ranges::lower_bound(val, x) - val.begin(); }
  int size() { return ssize(val); }
  template<ranges::range rng, class proj = identity>
  void mapping(rng &v, proj p = {}) { for(auto &x : v) p(x) = lower_bound(p(x)); }
  template<ranges::range rng, class proj = identity>
  void insert(rng &v, proj p = {}) { for(auto &x : v) val.emplace_back(p(x)); }
};
#line 1 "ds_problem/rectangleAddPointGet.cpp"
//#include<ds/fenwickTree2D.cpp>
//#include<misc/compression.cpp>

template<class T1, class T2>
vector<T2> rectAddPointGet(vector<tuple<T1, T1, T1, T1, T2>> &rect, vector<array<T1, 2>> &query, vector<int> updT) {
  compression<T1> xs(ssize(query));
  xs.insert(query, [](auto &x) { return x[0]; });
  xs.precompute();
  xs.mapping(query, [](auto &x) -> T1& { return x[0]; });
  xs.mapping(rect, [](auto &x) -> T1& { return get<0>(x); });
  xs.mapping(rect, [](auto &x) -> T1& { return get<1>(x); });

  FT2<T1, T2> bit(xs.size());
  for(auto &[l, r, d, u, w] : rect) {
    bit.fakeUpdate(l, d);
    bit.fakeUpdate(r, u);
    bit.fakeUpdate(l, u);
    bit.fakeUpdate(r, d);
  }
  bit.init();

  vector<T2> ans(ssize(query));
  for(int i = 0, ptr = 0; auto &[x, y] : query) {
    while(ptr < ssize(rect) and updT[ptr] <= i) {
      auto [l, r, d, u, w] = rect[ptr++];
      bit.update(l, d, w);
      bit.update(r, u, w);
      bit.update(l, u, -w);
      bit.update(r, d, -w);
    }
    ans[i++] = bit.query(x + 1, y + 1);
  }

  return ans;
}
#line 7 "test/rectangle_add_point_get.test.cpp"

signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  int n, q; cin >> n >> q;
  vector<tuple<int, int, int, int, ll>> rect(n);
  for(auto &[l, r, d, u, w] : rect)
    cin >> l >> d >> r >> u >> w;
  vector<array<int, 2>> query;
  vector<int> updT(n, 0);
  while(q--) {
    int op; cin >> op;
    if (op == 0) {
      int l, d, r, u; cin >> l >> d >> r >> u;
      ll w; cin >> w;
      rect.emplace_back(l, r, d, u, w);
      updT.emplace_back(ssize(query));
    } else if (op == 1) {
      int x, y; cin >> x >> y;
      query.push_back({x, y});
    }
  }

  for(ll ans : rectAddPointGet<int, ll>(rect, query, updT))
    cout << ans << '\n';

  return 0;
}
Back to top page