// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;}
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#pragma GCC optimize("Ofast")
#define debug(...) ((void)0)
#endif
const int kN = 1001;
const int kQ = 100001;
const int kL = 7;
const int MOD = 1e9 + 7;
int n, q;
vector<int> a, po2;
vector<tuple<int, int, int>> qs;
vector<vector<int>> cov1d, cov2d;
void build(int i, int j){
int m = (1<<i) | (1<<j);
for(int i = 1; i <= n; ++i) fill(AI(cov2d[i]), 0);
for(auto [l, r, x]: qs) if((m & x) == m) ++cov2d[l][r];
for(int l = 1; l <= n; ++l){
for(int r = n; r >= l; --r) cov2d[l][r] += cov2d[l][r+1];
for(int r = n; r >= l; --r) cov2d[l][r] += cov2d[l-1][r];
}
}
int solve(int b1, int b2){
int m1 = 1<<b1, m2 = 1<<b2, m = m1 | m2;
vector<int> &covl = cov1d[b1], &covr = cov1d[b2];
ll ans = 0;
for(int l = 1; l <= n; ++l)
for(int r = l; r <= n; ++r){
int bo = cov2d[l][r];
int ql = covl[l] - bo;
int qr = covr[r] - bo;
int no = q - bo - ql - qr;
ll l0 = (ql == 0 ? 1 : po2[ql - 1]), l1 = po2[ql] - l0;
ll r0 = (qr == 0 ? 1 : po2[qr - 1]), r1 = po2[qr] - r0;
ll b0 = (bo == 0 ? 1 : po2[bo - 1]), b1 = po2[bo] - b0;
if(a[l] & m1) swap(l0, l1);
if(a[r] & m2) swap(r0, r1);
ll tmp = 0;
tmp += l1 * r1 % MOD * b0;
tmp += l0 * r0 % MOD * b1;
tmp = tmp % MOD * po2[no] * (l != r ? 2 : 1) % MOD;
ans += tmp * l * (n - r + 1) % MOD;
}
ans = ans * m1 * m2 % MOD;
return ans;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
a.assign(n + 1, 0);
cov1d.assign(kL, vector<int>(n + 2, 0));
cov2d.assign(n + 1, vector<int>(n + 2, 0));
for(int i = 1; i <= n; ++i) cin >> a[i];
cin >> q;
for(int l, r, x, i = 1; i <= q; ++i){
cin >> l >> r >> x;
qs.pb(l, r, x);
for(int j = 0; j < kL; ++j)
if(x >> j & 1) ++cov1d[j][l], --cov1d[j][r+1];
}
po2.assign(q + 1, 1);
for(int i = 1; i <= q; ++i){
po2[i] *= po2[i-1] * 2;
po2[i] -= po2[i] >= MOD ? MOD : 0;
}
for(int j = 0; j < kL; ++j){
for(int i = 1; i <= n; ++i)
cov1d[j][i] += cov1d[j][i-1];
}
ll ans = 0;
for(int i = 0; i < kL; ++i)
for(int j = i; j < kL; ++j){
build(i, j);
ans += solve(i, j);
if(i != j) ans += solve(j, i);
}
ans %= MOD;
cout << ans << '\n';
return 0;
}
Compilation message
xorsum.cpp: In function 'int solve(int, int)':
xorsum.cpp:44:30: warning: unused variable 'm' [-Wunused-variable]
44 | int m1 = 1<<b1, m2 = 1<<b2, m = m1 | m2;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
6 ms |
332 KB |
Output is correct |
7 |
Correct |
6 ms |
356 KB |
Output is correct |
8 |
Correct |
6 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
1940 KB |
Output is correct |
2 |
Correct |
10 ms |
588 KB |
Output is correct |
3 |
Correct |
6 ms |
332 KB |
Output is correct |
4 |
Correct |
7 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
594 ms |
4300 KB |
Output is correct |
2 |
Correct |
595 ms |
4300 KB |
Output is correct |
3 |
Correct |
569 ms |
4300 KB |
Output is correct |
4 |
Correct |
553 ms |
4300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
6 ms |
332 KB |
Output is correct |
7 |
Correct |
6 ms |
356 KB |
Output is correct |
8 |
Correct |
6 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
6 ms |
372 KB |
Output is correct |
13 |
Correct |
6 ms |
368 KB |
Output is correct |
14 |
Correct |
6 ms |
332 KB |
Output is correct |
15 |
Correct |
6 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
6 ms |
332 KB |
Output is correct |
7 |
Correct |
6 ms |
356 KB |
Output is correct |
8 |
Correct |
6 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
460 KB |
Output is correct |
14 |
Correct |
3 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
6 ms |
372 KB |
Output is correct |
17 |
Correct |
6 ms |
368 KB |
Output is correct |
18 |
Correct |
6 ms |
332 KB |
Output is correct |
19 |
Correct |
6 ms |
332 KB |
Output is correct |
20 |
Correct |
197 ms |
2968 KB |
Output is correct |
21 |
Correct |
184 ms |
2876 KB |
Output is correct |
22 |
Correct |
172 ms |
2956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
6 ms |
332 KB |
Output is correct |
7 |
Correct |
6 ms |
356 KB |
Output is correct |
8 |
Correct |
6 ms |
332 KB |
Output is correct |
9 |
Correct |
45 ms |
1940 KB |
Output is correct |
10 |
Correct |
10 ms |
588 KB |
Output is correct |
11 |
Correct |
6 ms |
332 KB |
Output is correct |
12 |
Correct |
7 ms |
332 KB |
Output is correct |
13 |
Correct |
594 ms |
4300 KB |
Output is correct |
14 |
Correct |
595 ms |
4300 KB |
Output is correct |
15 |
Correct |
569 ms |
4300 KB |
Output is correct |
16 |
Correct |
553 ms |
4300 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
3 ms |
460 KB |
Output is correct |
21 |
Correct |
3 ms |
460 KB |
Output is correct |
22 |
Correct |
3 ms |
460 KB |
Output is correct |
23 |
Correct |
3 ms |
460 KB |
Output is correct |
24 |
Correct |
6 ms |
372 KB |
Output is correct |
25 |
Correct |
6 ms |
368 KB |
Output is correct |
26 |
Correct |
6 ms |
332 KB |
Output is correct |
27 |
Correct |
6 ms |
332 KB |
Output is correct |
28 |
Correct |
197 ms |
2968 KB |
Output is correct |
29 |
Correct |
184 ms |
2876 KB |
Output is correct |
30 |
Correct |
172 ms |
2956 KB |
Output is correct |
31 |
Correct |
617 ms |
5912 KB |
Output is correct |
32 |
Correct |
600 ms |
5876 KB |
Output is correct |
33 |
Correct |
598 ms |
5912 KB |
Output is correct |
34 |
Correct |
601 ms |
5952 KB |
Output is correct |
35 |
Correct |
603 ms |
5872 KB |
Output is correct |
36 |
Correct |
616 ms |
5880 KB |
Output is correct |