// Compile the next markdown for tutorial :D
/*
$\newcommand{\ceil}[1]{{\left\lceil{#1}\right\rceil}}$
$\newcommand{\floor}[1]{{\left\lfloor{#1}\right\rfloor}}$
$\newcommand{\prob}[1]{\Pr\paren{#1}}$
$\newcommand{\card}[1]{|#1|}$
$\newcommand{\paren}[1]{\left ( #1 \right )}$
$\newcommand{\Paren}[1]{\Big(#1\Big)}$
$\newcommand{\bracket}[1]{\left [ #1 \right ]}$
$\newcommand{\curly}[1]{\left\{ #1 \right\}}$
$\newcommand{\poly}{\mbox{\rm poly}}$
$\newcommand{\eps}{\varepsilon}$
$\newcommand{\inner}[2]{\langle #1\,,\,#2 \rangle}$
$\newcommand{\norm}[1]{ \lVert #1 \rVert}$
$\newcommand{\supp}[1]{\ensuremath{\textnormal{supp}}(#1)}$
$\renewcommand{\leq}{\leqslant}$
$\renewcommand{\bar}{\overline}$
$\renewcommand{\geq}{\geqslant}$
$\newcommand{\bb}[1]{\mathbb{ #1 }}$
$\newcommand{\backforth}{\leftrightarrow}$
Let $Q$ be the set of all updates.
We consider the state of the array without any updates. If we want to consider updates, we can add a superscript $S$ for $S \subseteq Q$ to denote the value after applying these updates. For example, $a^S_i$ would be the $a_i$ after applying all updates in $S$.
Define $p_i = a_1 + a_2 \dots + a_i$ for all $1 \leq i \leq N$. We want
$$
\begin{align*}
&\sum_{0 \leq l<r\leq N}(p_r-p_l)^2 \\
=&\sum_{0 \leq l < r \leq N} p_r^2-2p_rp_l+p_l^2 \\
= &\sum_{0 \leq l < N} (N-l)p_l^2+\sum_{1 \leq r \leq N} rp_r^2-\paren{\sum_{1 \leq i\leq N} p_i}^2 +\sum_{1 \leq i\leq N} p_i^2 \\
= &(N+1)\sum_{1 \leq i\leq N} p_i^2-\paren{\sum_{1 \leq i\leq N} p_i}^2
\end{align*}$$
Suppose we want to look at the contribution of the $k$ th bit.
Let $g(s, i) = 1$ iff the $s$ th bit is lid in $a_i$ and $0$ otherwise.
Let $f(k, i)$ to be the number of elements in the prefix $a_1, a_2, \dots, a_i$ with the bit $2^k$ lid, multiplied by $2^k$. In other words,
$$f^S(k, i) = \sum_{1 \leq j\leq i} 2^k g^S(k,j)$$
Observe that
$$p_i = \sum_{0 \leq k < 7} f(k,i)$$
Then we can write the above sum as
$$
\begin{align*}
&(N+1)\sum_{1 \leq i\leq N} p_i^2-\paren{\sum_{1 \leq i\leq N} p_i}^2 \\
= & (N+1)\sum_{1 \leq i\leq N} \paren{\sum_{0 \leq k <7} f(k,i)}^2-\paren{\sum_{1 \leq i \leq N} \sum_{0 \leq k< 7} f(k,i)}^2\\
=& (N+1)\sum_{0 \leq s, t < 7} \paren{ \sum_{1 \leq i \leq N} f(s,i)f(t,i)}-\paren{\sum_{1 \leq i \leq N} \sum_{0 \leq k< 7} f(k,i)}^2
\end{align*}
$$
We can let
$$A=\sum_{0 \leq s, t < 7} \paren{ \sum_{1 \leq i \leq N} f(s,i)f(t,i)} $$
$$B=\paren{\sum_{1 \leq i \leq N} \sum_{0 \leq k< 7} f(k,i)}^2$$
The answer to the problem then would be
$$(N+1) \sum_{S \subseteq Q} A^S - \sum_{S \subseteq Q} B^S.$$
We can further denote the above sums as $X$ and $Y$ respectively, so that the answer is $(N + 1)X - Y$.
Let's start with calculating $X$:
$$
\begin{align*}
X &= \sum_{S \subseteq Q} \sum_{0 \leq s, t < 7} \paren{ \sum_{1 \leq i \leq N} f^S(s,i)f^S(t,i)} \\
&= \sum_{0 \leq s, t < 7} \sum_{1 \leq i \leq N} \paren{\sum_{S \subseteq Q} f^S(s,i)f^S(t,i)}
\end{align*}
$$
Now, let's try to calculate $Y$. We have
$$\begin{align*}
B &= \paren{\sum_{1 \leq i \leq N} \sum_{0 \leq k< 7} f(k,i)}^2 \\
&= \sum_{1 \leq i, j \leq N} \sum_{0 \leq s, t < 7} f(s, i)f(t, j) \\
Y &= \sum_{S \subseteq Q}\sum_{1 \leq i, j \leq N} \sum_{0 \leq s, t < 7} f^S(s, i)f^S(t, j) \\
&= \sum_{1 \leq i, j \leq N} \sum_{0 \leq s, t < 7} \paren{\sum_{S \subseteq Q} f^S(s, i)f^S(t, j)}
\end{align*}
$$
Thus, if we let $J[s][t][i][j] = \sum_{S \subseteq Q} f^S(s, i)f^S(t, j)$, then
$$X = \sum_{0 \leq s, t < 7} \sum_{1 \leq i \leq N} J[s][t][i][i] $$
$$Y = \sum_{0 \leq s, t < 7} \sum_{1 \leq i, j \leq N} J[s][t][i][j].$$
So, we attempt to calculate $J$.
## Calculating $J$
Recall that $g(s, i) = 1$ iff the $s$ th bit is lid in $a_i$ and $0$ otherwise.
By definition,
$$f^S(s, i) = \sum_{1 \leq j\leq i} 2^s g^S(s,j)$$
This implies
$$\begin{align*}
J[s][t][i][j] &= \sum_{S \subseteq Q} \paren{\sum_{1 \leq x\leq i} 2^s g^S(s,x)} \paren{\sum_{1 \leq y\leq j} 2^t g^S(t,j)} \\
&= 2^{s + t} \sum_{1 \leq x \leq i} \sum_{1 \leq y \leq j} \sum_{S \subseteq Q}g^S(s, x) g^S(t, y)
\end{align*}
$$
Now, let $H_{s, i} = \{ S \subseteq Q : g^S(s, i) = 1 \}$. Observe that
$$ \sum_{S \subseteq Q}g^S(s, x) g^S(t, y) = |H_{s, x} \cap H_{t, y} |.$$
So, define $H[s][t][x][y] := |H_{s, x} \cap H_{t, y}|$, making
$$J[s][t][i][j] = 2^{s + t} \sum_{1 \leq x \leq i} \sum_{1 \leq y \leq j} H[s][t][x][y]$$
Which can be done using a 2D prefix sum if we have $H$.
Thus, we attempt to calculate array $H$.
## Calculating $H$
We first consider the following subproblem.
---
> Given $n > 0$. For $S \subseteq [n]$, define
>$$f_a(S)=\bigoplus_{i \in S} a_i$$
> Given two arrays $p$ and $q$ of $n$ ones and zeros. Define $A_{x, y}$ as follows:
>$$A_{x, y} = \curly{S\subseteq [n] : f_p(S)=x,f_q(S)=y}$$
> Calculate $|A_{x, y}|$ over all $x, y \in \curly{0, 1}$.
This can be solved by analyzing cases and counting the frequency of $(1, 1)$, $(1, 0)$, $(0, 1)$ and $(0, 0)$, but we want an elegant solution.
We can let $v_i = (p_i, q_i)$ and calculate a basis $B$ of $\text{span} \curly{v_1, v_2, \dots, v_n}$ over $\bb{F}^2_2$.
Then, $A_v = 0$ if $v \notin \text{span} \curly{v_1, v_2, \dots, v_n}$, otherwise, it's $2^{n - |B|}$.
---
Recall that $H[s][t][x][y] = |H_{s, x} \cap H_{t, y}|$ is the number of subsets $S$ of updates that keep $g^S(s, x) = g^S(t, y) = 1$.
In other words, if $a_x = 0$, then we consider the subsets of updates that change $a_x$ (similarly for $y$).
Now, let $F[s][t][j][\ell]$ be the total number of updates that change bit $s$ of $a_j$ and change bit $t$ of $a_\ell$.
Observe that $F$ can be calculated by 2D prefix sum (consider the problem on each $s, t$ independently, and imagine what you're trying to calculate on a grid)
After calculating $F$, consider the values
- $c_{1, 1} = F[s][t][j][\ell]$
- $c_{1, 0} = F[s][s][j][j] - u$
- $c_{0, 1} = F[t][t][\ell][\ell] - u$
- $c_{0, 0} = q-(u + v + w)$
Note that $c_{x, y}$ is the number of updates that apply $a_j := a_j \oplus (x2^s)$ and $a_\ell := a_\ell \oplus (y2^t)$.
We can treat the values above as frequencies of elements of an array from $\{ 0, 1 \}^2$, and use the solution to the subproblem above.
Now, if we have the solution in the form of an array $A[x][y]$, Let $b_i = 1$ if the $s$ th bit is lid in $a_i$ and $0$ otherwise.
Then $H[s][t][j][\ell] = A[1-b_j][1-b_\ell]$.
## Steps to solve the problem
1. For all $0 \leq s, t < 7$ and $1 \leq x, y \leq N$, $F[s][t][x][y]$ (the number of updates that change bit $s$ of $a_x$ and bit $t$ of $a_y$).
2. Use $F$ to calculate $H[s][t][x][y] = |H_{s, x} \cap H_{t, y}|$.
3. Calculate $J[s][t][i][j] = 2^{s + t}\sum_{1 \leq x \leq i} \sum_{1 \leq y \leq j} H[s][t][x][y]$
4. Calculate $X = \sum_{0 \leq s, t < 7} \sum_{1 \leq i \leq N} J[s][t][i][i]$
5. Calculate $Y = \sum_{0 \leq s, t < 7} \sum_{1 \leq i, j \leq N} J[s][t][i][j]$
6. Output $(N + 1) X - Y$.
Tip: for better memory and time, loop over $s, t$ and calculate the arrays with 2-dimensions only. However, you have to precompute powers of 2 up to $2 \cdot 10^5$ and precompute the solution of the subproblem that works with xor basis.
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a; swap(a, m);
u -= t * v; swap(u, v);
}
assert(m == 1);
return u;
}
template <typename T>
class Modular {
public:
using Type = typename decay<decltype(T::value)>::type;
constexpr Modular() : value() {}
template <typename U>
Modular(const U& x) {
value = normalize(x);
}
template <typename U>
static Type normalize(const U& x) {
Type v;
if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
else v = static_cast<Type>(x % mod());
if (v < 0) v += mod();
return v;
}
const Type& operator()() const { return value; }
template <typename U>
explicit operator U() const { return static_cast<U>(value); }
constexpr static Type mod() { return T::value; }
Modular& operator+=(const Modular& other) { value += other.value; value -= (value >= mod()) * mod(); return *this; }
Modular& operator-=(const Modular& other) { value -= other.value; value += (value < 0) * mod(); return *this; }
template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
Modular& operator++() { return *this += 1; }
Modular& operator--() { return *this -= 1; }
Modular operator++(int) { Modular result(*this); *this += 1; return result; }
Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
Modular operator-() const { return Modular(-value); }
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
return *this;
}
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) {
int64_t q = int64_t(static_cast<long double>(value) * rhs.value / mod());
value = normalize(value * rhs.value - q * mod());
return *this;
}
template <typename U = T>
typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
value = normalize(value * rhs.value);
return *this;
}
Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }
friend const Type& abs(const Modular& x) { return x.value; }
template <typename U>
friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename V, typename U>
friend V& operator>>(V& stream, Modular<U>& number);
private:
Type value;
};
template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }
template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }
template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
assert(b >= 0);
Modular<T> x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1) res *= x;
x *= x;
p >>= 1;
}
return res;
}
template <typename T>
bool IsZero(const Modular<T>& number) {
return number() == 0;
}
template <typename T>
string to_string(const Modular<T>& number) {
return to_string(number());
}
// U == std::ostream? but done this way because of fastoutput
template <typename U, typename T>
U& operator<<(U& stream, const Modular<T>& number) {
return stream << number();
}
// U == std::istream? but done this way because of fastinput
template <typename U, typename T>
U& operator>>(U& stream, Modular<T>& number) {
typename common_type<typename Modular<T>::Type, int64_t>::type x;
stream >> x;
number.value = Modular<T>::normalize(x);
return stream;
}
constexpr int md = 1e9 + 7;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
const int N = 1012;
const int M = 100123;
int a[N], n, q;
tuple<int, int, int> Q[M];
void take_input() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
cin >> q;
for (int i = 1; i <= q; i++) {
auto& [l, r, x] = Q[i];
cin >> l >> r >> x;
}
}
void step1_1() {
take_input();
}
Mint pow2[M + M];
void calculate_pow2() {
pow2[0] = 1;
for (int i = 1; i < M + M; i++) pow2[i] = pow2[i - 1] + pow2[i - 1];
}
void step1_2() {
calculate_pow2();
}
int G[7][N];
void calculate_G() {
for (int s = 0; s < 7; s++) {
for (int i = 1; i <= q; i++) {
auto& [l, r, x] = Q[i];
if ((x >> s) & 1) G[s][l] += 1, G[s][r + 1] -= 1;
}
for (int i = 1; i <= n + 1; i++) G[s][i] += G[s][i - 1];
}
}
void step1_3() {
calculate_G();
}
Mint A[1 << 4][4];
int basis[2];
void calculate_A() {
for (int m = 0; m < (1 << 4); m++) {
basis[0] = basis[1] = 0;
int b = 0; // basis size
for (int j = 0; j < 4; j++) if ((1 << j) & m) {
// insert j into the basis
int x = j;
for (int k = 0; k < 2; k++) if ((1 << k) & x) {
if (!basis[k]) {
basis[k] = x;
b += 1;
break;
}
x ^= basis[k];
}
}
if (basis[0] == 0) basis[0] = basis[1];
for (int w = 0; w < (1 << b); w++) {
int x = 0;
for (int k = 0; k < b; k++) if ((1 << k) & w) x ^= basis[k];
A[m][x] = pow2[q - b];
}
}
}
void step1_4() {
calculate_A();
}
int F[N][N];
void calculate_F(int s, int t) {
for (int i = 1; i <= q; i++) {
auto [l, r, x] = Q[i];
int bit_s = (x >> s) & 1;
int bit_t = (x >> t) & 1;
if (bit_s && bit_t)
F[l][r] += 1;
}
for (int x = 1; x <= n; x++)
for (int y = n; y >= 1; --y)
F[x][y] += F[x - 1][y] + F[x][y + 1] - F[x - 1][y + 1];
}
void step2(int s, int t) {
calculate_F(s, t);
}
Mint H[N][N];
int convert(int x, int y) {
return (x << 1) | y;
}
int convert2(int x, int y, int z, int l) {
l -= (x + y + z);
x = !!x; y = !!y; z = !!z; l = !!l;
return (x << 3) | (y << 2) | (z << 1) | l;
}
void calculate_H(int s, int t) {
for (int x = 1; x <= n; x++)
for (int y = 1; y <= n; y++) {
int F_val = F[min(x, y)][max(x, y)];
int m = convert2(F_val, (G[s][x] - F_val), (G[t][y] - F_val), q);
int bx = (a[x] >> s) & 1;
int by = (a[y] >> t) & 1;
H[x][y] = A[m][convert(1 - bx, 1 - by)];
}
}
void step3(int s, int t) {
calculate_H(s, t);
}
Mint J[N][N];
void calculate_J(int s, int t) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
J[i][j] = pow2[s + t] * H[i][j];
J[i][j] += J[i - 1][j] + J[i][j - 1] - J[i - 1][j - 1];
}
}
void step4(int s, int t) {
calculate_J(s, t);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
for (auto func : {step1_1, step1_2, step1_3, step1_4}) {
func();
}
Mint X = 0;
Mint Y = 0;
for (int s = 0; s < 7; s++)
for (int t = 0; t < 7; t++) {
memset(F, 0, sizeof(F));
memset(H, 0, sizeof(H));
memset(J, 0, sizeof(J));
for (auto func : {step2, step3, step4}) {
func(s, t);
}
for (int i = 1; i <= n; i++) {
X += J[i][i];
for (int j = 1; j <= n; j++) Y += J[i][j];
}
}
cout << Mint(n + 1) * X - Y << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |