#include<bits/stdc++.h>
#define taskname "C"
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int lim = 1e3 + 5;
void add(int& a, int b){
if((a += b) >= mod){
a -= mod;
}
}
int n, q, a[lim];
bool x_32 = true;
vector<tuple<int, int, int>>query;
namespace sub125{
void solve(){
for(auto& [l, r, x] : query){
cin >> l >> r >> x;
}
int ans = 0;
vector<int>A(n + 1);
for(int mask = (1 << q) - 1; mask > -1; mask--){
for(int i = 1; i <= n; i++){
A[i] = a[i];
}
for(int i = 0; i < q; i++){
if(1 << i & mask){
auto& [l, r, x] = query[i];
for(int j = l; j <= r; j++){
A[j] ^= x;
}
}
}
for(int i = 1; i <= n; i++){
for(int j = i, sum = 0; j <= n; j++){
sum += A[j];
add(ans, 1LL * sum * sum % mod);
}
}
}
cout << ans;
}
}
namespace sub67{
int state(int x, int y, int i, int j){
return (i >> x & 1) | ((j >> y & 1) << 1);
}
void solve(){
int ans = 0;
vector<int>dp(4), ndp;
for(int x = 0; x < 7; x++){
for(int y = 0; y < 7; y++){
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
dp[0] = dp[1] = dp[2] = dp[3] = 0;
dp[state(x, y, a[i], a[j])] = 1;
for(auto& [l, r, X] : query){
ndp = dp;
for(int mask = 0; mask < 4; mask++){
int n_mask = mask;
if(l <= i && r >= i && (1 << x & X)){
n_mask ^= 1;
}
if(l <= j && r >= j && (1 << y & X)){
n_mask ^= 2;
}
add(ndp[n_mask], dp[mask]);
}
swap(dp, ndp);
}
add(ans, ll(1 + int(i != j)) * i * (n - j + 1) * dp[3] * (1 << (x + y)) % mod);
}
}
}
}
cout << ans;
}
}
namespace sub3489{
const int lim = 1e5 + 5;
int pw_2[lim];
int get(int C, bool p){
return C == 0 ? int(!p) : pw_2[C - 1];
}
void solve(){
for(int i = pw_2[0] = 1; i < lim; i++){
pw_2[i] = (pw_2[i - 1] << 1) % mod;
}
int ans = 0;
for(int x = 0; x < 7; x++){
for(int y = 0; y < 7; y++){
vector<vector<int>>dp(n + 2, vector<int>(n + 2, 0)), p(n + 2, vector<int>(2, 0));
for(auto& [l, r, X] : query){
bool is_x = bool(1 << x & X), is_y = bool(1 << y & X);
if(is_x){
p[l][0]++;
p[r + 1][0]--;
}
if(is_y){
p[l][1]++;
p[r + 1][1]--;
}
if(is_x && is_y){
dp[l][l]++;
dp[l][r + 1]--;
dp[r + 1][l]--;
dp[r + 1][r + 1]++;
}
}
for(int i = 1; i <= n; i++){
p[i][0] += p[i - 1][0];
p[i][1] += p[i - 1][1];
for(int j = 1; j <= n; j++){
dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
}
}
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
int O = dp[i][j], X = p[i][0] - O, Y = p[j][1] - O, coef = (i << int(i != j)) * (n - j + 1);
bool is_x = bool(1 << x & a[i]), is_y = (1 << y & a[j]);
for(int px = 0; px < 2; px++){
for(int py = 0; py < 2; py++){
if((is_x ^ px) && (is_y ^ py)){
add(ans, 1LL * (1LL * get(O, false) * get(X, px) % mod * get(Y, py) % mod + 1LL * get(O, true) * get(X, px ^ 1) % mod * get(Y, py ^ 1) % mod) * pw_2[q - O - X - Y] % mod * coef % mod * (1 << (x + y)) % mod);
}
}
}
}
}
}
}
cout << ans;
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
cin >> q;
query.resize(q);
for(auto& [l, r, x] : query){
cin >> l >> r >> x;
if(x > 31){
x_32 = false;
}
}
if((n <= 100 && q <= 10) || (n <= 30 && q <= 20)){
sub125::solve();
}
else if((n <= 30 && q <= 5000 && x_32 && *max_element(a + 1, a + n + 1) < 32) || (n <= 300 && q <= 300)){
sub67::solve();
}
else{
sub3489::solve();
}
}
Compilation message (stderr)
xorsum.cpp: In function 'int main()':
xorsum.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |