lmori's Library

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

View the Project on GitHub lmorinn/library

:heavy_check_mark: verify/AizuOnlineJudge/data-structure/others/HyperCubeDatabaseSystem.test.cpp

Depends on

Code

#include "../../../../template/template.hpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/4077"
#include "../../../../data-structure/others/MultiDimensionalSum.hpp"

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  in(n);
  vector<int> v(n);
  in(v);
  int siz = 1;
  multi_dimensional_sum<lint> sum(v);
  rep(i, n) siz *= v[i];

  vector<int> x(n, 0);
  rep(i, siz) {
    lint c;
    in(c);
    sum.add(x, c);
    x.back()++;
    if (x.back() >= v.back()) {
      for (int j = n - 1; j > 0; j--) {
        if (x[j] >= v[j]) {
          x[j] = 0;
          x[j - 1]++;
        }
      }
    }
  }
  sum.build();

  int q;
  in(q);
  vector<int> l(n), r(n);
  rep(i, q) {
    rep(j, n) in(l[j], r[j]);
    out(sum.calc(l, r));
  }
}
#line 2 "template/template.hpp"
#pragma region Macros
#include <bits/stdc++.h>
#include <tr2/dynamic_bitset>

using namespace std;
using namespace tr2;
using lint = long long;
using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define uniqv(v) v.erase(unique(all(v)), v.end())
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;

ostream &operator<<(ostream &dest, __int128_t value) {
  ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

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 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const unordered_map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
  os << pair_var.first << " " << pair_var.second;
  return os;
}

void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

void outf() { cout << '\n'; }
template <class T, class... Ts>
void outf(const T &a, const Ts &...b) {
  cout << fixed << setprecision(14) << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

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

inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}

template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};

#pragma endregion
#line 2 "verify/AizuOnlineJudge/data-structure/others/HyperCubeDatabaseSystem.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/4077"
#line 1 "data-structure/others/MultiDimensionalSum.hpp"
template <class T>
class multi_dimensional_sum {
 private:
  vector<T> sum;
  vector<int> mx;
  vector<long long> prod;
  int n, siz;

  T idx(const vector<int>& id) {
    int ret = 0;
    for (int i = 0; i < n; i++) ret += prod[i] * id[i];
    return ret;
  }

 public:
  // [1, mx_0] x [1, mx_1] x ... x [1, mx_{n-1}]
  multi_dimensional_sum(const vector<int>& mx) : mx(mx) {
    n = int(mx.size());
    siz = 1;
    for (int i = 0; i < n; i++) siz *= mx[i];
    sum.assign(siz, 0);
    prod.resize(n);
    prod[n - 1] = 1;
    for (int i = n - 1; i > 0; i--) prod[i - 1] = prod[i] * mx[i];
  };

  void add(vector<int> id, T x) {
    assert(int(id.size()) == n);
    int i = idx(id);
    assert(i < siz);
    sum[i] += x;
  }

  void build() {
    for (int i = 0; i < n; i++) {
      int j = prod[i];
      for (int id = 0; id < siz; id++) {
        if ((id / j) % mx[i] > 0) sum[id] += sum[id - j];
      }
    }
  }

  // sum [l_0, r_0] x [l_1, r_1] x ... x [l_{n-1}, r_{n-1}]
  T calc(const vector<int>& l, const vector<int>& r) {
    assert(int(l.size()) == n and int(r.size()) == n);
    for (int i = 0; i < n; i++) {
      if (l[i] > r[i]) return 0;
    }

    T ret = 0;
    vector<int> id(n);
    for (unsigned int bit = 0; bit < (1u << n); bit++) {
      bool ok = true;

      for (int i = 0; i < n; i++) {
        if ((bit & (1 << i))) {
          id[i] = r[i] - 1;
        } else {
          id[i] = l[i] - 2;
        }

        if (id[i] < 0) {
          ok = false;
          break;
        }
      }
      if (!ok) continue;
      int lcnt = n - popcount(bit);
      ret += (lcnt % 2 ? -sum[idx(id)] : sum[idx(id)]);
    }
    return ret;
  }
};
#line 4 "verify/AizuOnlineJudge/data-structure/others/HyperCubeDatabaseSystem.test.cpp"

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  in(n);
  vector<int> v(n);
  in(v);
  int siz = 1;
  multi_dimensional_sum<lint> sum(v);
  rep(i, n) siz *= v[i];

  vector<int> x(n, 0);
  rep(i, siz) {
    lint c;
    in(c);
    sum.add(x, c);
    x.back()++;
    if (x.back() >= v.back()) {
      for (int j = n - 1; j > 0; j--) {
        if (x[j] >= v[j]) {
          x[j] = 0;
          x[j - 1]++;
        }
      }
    }
  }
  sum.build();

  int q;
  in(q);
  vector<int> l(n), r(n);
  rep(i, q) {
    rep(j, n) in(l[j], r[j]);
    out(sum.calc(l, r));
  }
}
Back to top page