// 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;
int solve(int b1, int b2){
int m1 = 1<<b1, m2 = 1<<b2, m = m1 | m2;
vector<int> &covl = cov1d[b1], &covr = cov1d[b2];
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];
}
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 = 0; j < kL; ++j)
ans += solve(i, j);
ans %= MOD;
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
7 ms |
332 KB |
Output is correct |
7 |
Correct |
7 ms |
332 KB |
Output is correct |
8 |
Correct |
7 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
2756 KB |
Output is correct |
2 |
Correct |
11 ms |
716 KB |
Output is correct |
3 |
Correct |
7 ms |
332 KB |
Output is correct |
4 |
Correct |
7 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
598 ms |
4300 KB |
Output is correct |
2 |
Correct |
619 ms |
4304 KB |
Output is correct |
3 |
Correct |
595 ms |
4300 KB |
Output is correct |
4 |
Correct |
588 ms |
4300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
4 ms |
460 KB |
Output is correct |
5 |
Correct |
4 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
7 ms |
332 KB |
Output is correct |
7 |
Correct |
7 ms |
332 KB |
Output is correct |
8 |
Correct |
7 ms |
360 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 |
7 ms |
332 KB |
Output is correct |
13 |
Correct |
7 ms |
332 KB |
Output is correct |
14 |
Correct |
8 ms |
372 KB |
Output is correct |
15 |
Correct |
7 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
7 ms |
332 KB |
Output is correct |
7 |
Correct |
7 ms |
332 KB |
Output is correct |
8 |
Correct |
7 ms |
360 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 |
4 ms |
460 KB |
Output is correct |
13 |
Correct |
4 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 |
7 ms |
332 KB |
Output is correct |
17 |
Correct |
7 ms |
332 KB |
Output is correct |
18 |
Correct |
8 ms |
372 KB |
Output is correct |
19 |
Correct |
7 ms |
376 KB |
Output is correct |
20 |
Correct |
255 ms |
3936 KB |
Output is correct |
21 |
Correct |
211 ms |
3888 KB |
Output is correct |
22 |
Correct |
191 ms |
3816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
7 ms |
332 KB |
Output is correct |
7 |
Correct |
7 ms |
332 KB |
Output is correct |
8 |
Correct |
7 ms |
360 KB |
Output is correct |
9 |
Correct |
50 ms |
2756 KB |
Output is correct |
10 |
Correct |
11 ms |
716 KB |
Output is correct |
11 |
Correct |
7 ms |
332 KB |
Output is correct |
12 |
Correct |
7 ms |
360 KB |
Output is correct |
13 |
Correct |
598 ms |
4300 KB |
Output is correct |
14 |
Correct |
619 ms |
4304 KB |
Output is correct |
15 |
Correct |
595 ms |
4300 KB |
Output is correct |
16 |
Correct |
588 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 |
4 ms |
460 KB |
Output is correct |
21 |
Correct |
4 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 |
7 ms |
332 KB |
Output is correct |
25 |
Correct |
7 ms |
332 KB |
Output is correct |
26 |
Correct |
8 ms |
372 KB |
Output is correct |
27 |
Correct |
7 ms |
376 KB |
Output is correct |
28 |
Correct |
255 ms |
3936 KB |
Output is correct |
29 |
Correct |
211 ms |
3888 KB |
Output is correct |
30 |
Correct |
191 ms |
3816 KB |
Output is correct |
31 |
Correct |
687 ms |
6864 KB |
Output is correct |
32 |
Correct |
708 ms |
6840 KB |
Output is correct |
33 |
Correct |
644 ms |
6804 KB |
Output is correct |
34 |
Correct |
646 ms |
6748 KB |
Output is correct |
35 |
Correct |
683 ms |
6720 KB |
Output is correct |
36 |
Correct |
652 ms |
6720 KB |
Output is correct |