#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG
#include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
#define debug(...) void(23)
#endif
template<typename T>
T power(T a, i64 b) {
T res {1};
while (b) {
if (b & 1) {
res *= a;
}
a *= a;
b >>= 1;
}
return res;
}
constexpr int md = int(1E9) + 7;
struct MInt {
int val;
MInt() : val(0) {}
template<typename T>
MInt(T x) {
if (-md <= x && x < md) {
val = x;
} else {
val = x % md;
}
if (val < 0) {
val += md;
}
}
int operator()() { return val; }
MInt& operator+= (MInt rhs) {
if ((val += rhs.val) >= md) {
val -= md;
}
return *this;
}
MInt& operator-= (MInt rhs) {
if ((val -= rhs.val) < 0) {
val += md;
}
return *this;
}
MInt& operator*= (MInt rhs) {
val = (1LL * val * rhs.val) % md;
return *this;
}
MInt inv() {
return power(*this, md - 2);
}
MInt& operator/= (MInt rhs) {
return *this *= rhs.inv();
}
bool operator== (MInt rhs) {
return val == rhs.val;
}
bool operator!= (MInt rhs) {
return val != rhs.val;
}
};
MInt operator+ (MInt lhs, MInt rhs) {
return lhs += rhs;
}
MInt operator- (MInt lhs, MInt rhs) {
return lhs -= rhs;
}
MInt operator* (MInt lhs, MInt rhs) {
return lhs *= rhs;
}
MInt operator/ (MInt lhs, MInt rhs) {
return lhs /= rhs;
}
std::ostream& operator<< (std::ostream& os, MInt x) {
return os << x.val;
}
using Z = MInt;
constexpr int K = 7;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
std::vector<int> A(N);
for (int i = 0; i < N; ++i) {
std::cin >> A[i];
}
int Q;
std::cin >> Q;
std::vector<int> L(Q), R(Q), X(Q);
for (int i = 0; i < Q; ++i) {
std::cin >> L[i] >> R[i] >> X[i];
--L[i], --R[i];
}
std::vector<std::vector<std::array<std::array<int, K>, K>>> intvlbit(N, std::vector<std::array<std::array<int, K>, K>>(N));
std::vector<std::array<int, K>> cbit(N + 1);
for (int i = 0; i < Q; ++i) {
for (int k = 0; k < K; ++k) {
if (X[i] >> k & 1) {
cbit[L[i]][k]++;
cbit[R[i] + 1][k]--;
}
}
for (int k = 0; k < K; ++k) {
for (int l = 0; l < K; ++l) {
if (X[i] >> k & 1 && X[i] >> l & 1) {
intvlbit[L[i]][R[i]][k][l]++;
}
}
}
}
for (int i = 0; i < N; ++i) {
for (int k = 0; k < K; ++k) {
cbit[i + 1][k] += cbit[i][k];
}
}
for (int i = 0; i < N; ++i) {
for (int j = N - 1; j >= i; --j) {
for (int a = 0; a < K; ++a) {
for (int b = 0; b < K; ++b) {
if (i + 1 < N) intvlbit[i + 1][j][a][b] += intvlbit[i][j][a][b];
if (j) intvlbit[i][j - 1][a][b] += intvlbit[i][j][a][b];
if (i + 1 < N && j) intvlbit[i + 1][j - 1][a][b] -= intvlbit[i][j][a][b];
}
}
}
}
std::vector<std::array<Z, 2>> ways(Q + 1);
ways[0][0] = 1;
for (int i = 1; i <= Q; ++i) {
ways[i][0] = ways[i - 1][0] + ways[i - 1][1];
ways[i][1] = ways[i - 1][0] + ways[i - 1][1];
}
std::vector<Z> pow2(Q + 1);
pow2[0] = 1;
for (int i = 1; i <= Q; ++i) {
pow2[i] = pow2[i - 1] * 2;
}
std::vector<std::vector<Z>> contr(N, std::vector<Z>(N));
for (int i = 0; i < N; ++i) {
for (int j = i; j < N; ++j) {
for (int a = 0; a < K; ++a) {
for (int b = 0; b < K; ++b) {
int cntboth = intvlbit[i][j][a][b];
int cnti = cbit[i][a] - cntboth;
int cntj = cbit[j][b] - cntboth;
int others = Q - cntboth - cnti - cntj;
// debug(i, j, a, b, cntboth, cnti, cntj, others);
assert(cntboth >= 0 && cnti >= 0 && cntj >= 0 && others >= 0);
int initi = A[i] >> a & 1;
int initj = A[j] >> b & 1;
for (int tboth : {0, 1}) {
for (int ti : {0, 1}) {
for (int tj : {0, 1}) {
int ni = initi ^ tboth ^ ti;
int nj = initj ^ tboth ^ tj;
if (ni == 0 || nj == 0) {
continue;
}
contr[i][j] += ways[cntboth][tboth] * ways[cnti][ti] * ways[cntj][tj] * pow2[others] * (1 << (a + b));
}
}
}
}
}
}
}
Z ans = 0;
for (int i = 0; i < N; ++i) {
for (int j = i; j < N; ++j) {
ans += contr[i][j] * (i + 1) * (N - j) * (i == j ? 1 : 2);
}
}
std::cout << ans << '\n';
return 0;
}
# | 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... |