#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
//mt19937_64 rng(1);
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int MOD = 998244353;
struct Modular{
ll x;
Modular(ll _x = 0){
x = _x % MOD;
if (x < 0) x += MOD;
}
Modular& operator += (Modular y){
x += y.x;
if (x >= MOD) x -= MOD;
return *this;
}
Modular operator + (Modular y) {
Modular tmp = *this;
return tmp += y;
}
Modular& operator -= (Modular y){
x -= y.x;
if (x < 0) x += MOD;
return *this;
}
Modular operator - (Modular y) {
Modular tmp = *this;
return tmp -= y;
}
Modular& operator *= (Modular y){
x *= y.x;
if (x >= MOD) x %= MOD;
return *this;
}
Modular operator * (Modular y) {
Modular tmp = *this;
return tmp *= y;
}
// use at your own risk
bool operator == (Modular y){
return x == y.x;
}
bool operator != (Modular y){
return x != y.x;
}
};
ostream& operator << (ostream& out, Modular x){
out << x.x;
return out;
}
Modular fast_pow(Modular a, int n){
Modular ans = 1;
while(n > 0){
if (n & 1) ans *= a;
a *= a;
n >>= 1;
}
return ans;
}
Modular inverse(Modular a){return fast_pow(a, MOD - 2);}
struct Query{
int l, r, w;
Query(int l = 0, int r = 0, int w = 0): l(l), r(r), w(w){}
};
const int N = 1010, Q = 1e5 + 69;
int n;
Query queries[Q];
int a[N];
int cnt[N][N][3];
Modular po2[Q];
void add(int u, int v, int x, int y, int idx){
cnt[u][v][idx]++;
cnt[u][y+1][idx]--;
cnt[x+1][v][idx]--;
cnt[x+1][y+1][idx]++;
}
void prepare(){
po2[0] = 1;
for(int i = 1; i< Q; ++i) po2[i] = po2[i-1] * 2;
}
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
int q; cin >> q;
for(int i = 0; i < q; ++i){
int l, r, w; cin >> l >> r >> w;
queries[i] = {l, r, w};
}
Modular ans = 0;
for(int x = 0; x < 7; ++x) for(int y = 0; y < 7; ++y){
memset(cnt, 0, sizeof cnt);
for(int i = 0; i< q; ++i){
int l = queries[i].l, r = queries[i].r, w = queries[i].w;
array<pair<int, int>, 3> sigma;
sigma[0] = {1, l-1}; sigma[1] = {l, r}; sigma[2] = {r+1, n};
for(int b1 = 0; b1 <= 2; ++b1) for(int b2 = 0; b2 <= 2; ++b2){
int v1 = (b1 == 1) && GETBIT(w, x), v2 = (b2 == 1) && GETBIT(w, y);
int idx = v1 + v2 * 2;
if (idx == 3) continue;
add(sigma[b1].first, sigma[b2].first, sigma[b1].second, sigma[b2].second, idx);
}
}
for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) {
for(int idx = 0; idx < 3; ++idx){
cnt[i][j][idx] += cnt[i-1][j][idx] + cnt[i][j-1][idx] - cnt[i-1][j-1][idx];
}
if (i <= j){
Modular cur = 0;
array<int, 4> sigma;
for(int idx = 0; idx < 3; ++idx) sigma[idx] = cnt[i][j][idx];
sigma[3] = q - sigma[0] - sigma[1] - sigma[2];
int v1 = GETBIT(a[i], x), v2 = GETBIT(a[j], y);
int v = v1 + v2 * 2;
array<Modular, 4> dp; dp[v] = 1;
for(int it = 0; it < 4; ++it){
if (sigma[it] == 0) continue;
array<Modular, 4> tmp;
for(int k = 0; k < 4; ++k){
tmp[k] += dp[k] * po2[sigma[it]-1];
tmp[k^it] += dp[k] * po2[sigma[it]-1];
}
dp = tmp;
}
//
cur = dp[3];
if (i < j) cur *= 2;
ans += cur * po2[x + y] * i * (n + 1 - j);
}
}
}
cout << ans << "\n";
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t start = clock();
prepare();
solve();
cerr << "Time elapsed: " << clock() - start << "ms!\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... |