#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr<<"hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
using ll=int;
using ld=long double;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
using pi=pair<ll,ll>; using spi=pair<pair<int,int>,int>; using dpi=pair<pi,pi>;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (202002)
int n, A[1006], B[1002][1002][7][7], q, psum[7][1002], tmp[7][7][1002];
int mod=1e9+7, ans, p2[MAXN];
vector<spi> v, o;
#define gc getchar_unlocked
int in() {
int x=0;
char ch=gc();
while(ch<'0'||ch>'9')ch=gc();
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+(ch&15);
ch=gc();
}
return x;
}
int main() {
// FAST
n=in();
FOR(i,1,n) A[i]=in();
q=in();
v.reserve(q), v.resize(q);
for(auto&i:v)i.f.f=in(),i.f.s=in(),i.s=in();
sort(all(v)), reverse(all(v));
o=v;
FOR(i,1,n) {
while(v.size()&&v.back().f.f==i) {
FOR(b1,0,6)if(1<<b1&v.back().s)FOR(b2,0,6)if(1<<b2&v.back().s)++tmp[b1][b2][v.back().f.s];
v.pop_back();
}
FOR(b1,0,6)FOR(b2,0,6){
ll co=0;
DEC(j,n,i){
co+=tmp[b1][b2][j];
B[i][j][b1][b2]=co;
}
}
}v=o;
FOR(i,1,n)FOR(j,i+1,n)FOR(b1,0,6)FOR(b2,0,6)B[j][i][b1][b2]=B[i][j][b1][b2];
for(auto i:v){
FOR(b,0,6) if(1<<b&i.s) ++ psum[b][i.f.f], -- psum[b][i.f.s+1];
}
FOR(b,0,6) FOR(i,1,n) psum[b][i]+=psum[b][i-1];
p2[0]=1; FOR(i,1,MAXN-1){p2[i]=p2[i-1]*2;if(p2[i]>=mod)p2[i]-=mod;}
auto Cp=[&](ll x){return p2[x-1];};// make sure x!=0
FOR(i,1,n)FOR(j,i,n)FOR(b1,0,6)FOR(b2,0,6){
// ways for b1 to be activated for i, and b2 to be activated for j
// * 2 ah
ll x=psum[b1][i], y=psum[b2][j], ways1[2]={0,0}, ways2[2]={0,0}, c=B[i][j][b1][b2], ceff=(long long)i*(n-j+1)%mod;
if(i^j) { ceff*=2; if(ceff>=mod) ceff-=mod; }
x-=c, y-=c;
ll notin = q-x-y-c;
if(1<<b1&A[i]) ways1[0] = x ? Cp(x) : 0, ways1[1] = x ? Cp(x) : 1;
else ways1[0] = x ? Cp(x) : 1, ways1[1] = x ? Cp(x) : 0;
if(1<<b2&A[j]) ways2[0] = y ? Cp(y) : 0, ways2[1] = y ? Cp(y) : 1;
else ways2[0] = y ? Cp(y) : 1, ways2[1] = y ? Cp(y) : 0;
ans += (long long)ceff * ways1[0] % mod * ways2[0] % mod * (c ? Cp(c+notin+b1+b2) : 0) % mod, ans %= mod;
ans += (long long)ceff * ways1[1] % mod * ways2[1] % mod * (c ? Cp(c+notin+b1+b2) : p2[notin+b1+b2]) % mod, ans %= mod;
}
cout<<ans<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1132 KB |
Output is correct |
2 |
Correct |
2 ms |
1132 KB |
Output is correct |
3 |
Correct |
3 ms |
1388 KB |
Output is correct |
4 |
Correct |
2 ms |
1388 KB |
Output is correct |
5 |
Correct |
3 ms |
1388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1132 KB |
Output is correct |
2 |
Correct |
2 ms |
1132 KB |
Output is correct |
3 |
Correct |
3 ms |
1388 KB |
Output is correct |
4 |
Correct |
2 ms |
1388 KB |
Output is correct |
5 |
Correct |
3 ms |
1388 KB |
Output is correct |
6 |
Correct |
12 ms |
3692 KB |
Output is correct |
7 |
Correct |
11 ms |
3724 KB |
Output is correct |
8 |
Correct |
11 ms |
3564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
5996 KB |
Output is correct |
2 |
Correct |
16 ms |
3820 KB |
Output is correct |
3 |
Correct |
12 ms |
3564 KB |
Output is correct |
4 |
Correct |
13 ms |
3564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
943 ms |
193528 KB |
Output is correct |
2 |
Correct |
924 ms |
193516 KB |
Output is correct |
3 |
Correct |
898 ms |
193568 KB |
Output is correct |
4 |
Correct |
888 ms |
193644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1516 KB |
Output is correct |
2 |
Correct |
3 ms |
1516 KB |
Output is correct |
3 |
Correct |
3 ms |
1516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1516 KB |
Output is correct |
2 |
Correct |
3 ms |
1516 KB |
Output is correct |
3 |
Correct |
3 ms |
1516 KB |
Output is correct |
4 |
Correct |
5 ms |
1644 KB |
Output is correct |
5 |
Correct |
5 ms |
1664 KB |
Output is correct |
6 |
Correct |
5 ms |
1644 KB |
Output is correct |
7 |
Correct |
5 ms |
1644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1132 KB |
Output is correct |
2 |
Correct |
2 ms |
1132 KB |
Output is correct |
3 |
Correct |
3 ms |
1388 KB |
Output is correct |
4 |
Correct |
2 ms |
1388 KB |
Output is correct |
5 |
Correct |
3 ms |
1388 KB |
Output is correct |
6 |
Correct |
12 ms |
3692 KB |
Output is correct |
7 |
Correct |
11 ms |
3724 KB |
Output is correct |
8 |
Correct |
11 ms |
3564 KB |
Output is correct |
9 |
Correct |
5 ms |
1516 KB |
Output is correct |
10 |
Correct |
3 ms |
1516 KB |
Output is correct |
11 |
Correct |
3 ms |
1516 KB |
Output is correct |
12 |
Correct |
18 ms |
3692 KB |
Output is correct |
13 |
Correct |
18 ms |
3692 KB |
Output is correct |
14 |
Correct |
14 ms |
3692 KB |
Output is correct |
15 |
Correct |
15 ms |
3692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1132 KB |
Output is correct |
2 |
Correct |
2 ms |
1132 KB |
Output is correct |
3 |
Correct |
3 ms |
1388 KB |
Output is correct |
4 |
Correct |
2 ms |
1388 KB |
Output is correct |
5 |
Correct |
3 ms |
1388 KB |
Output is correct |
6 |
Correct |
12 ms |
3692 KB |
Output is correct |
7 |
Correct |
11 ms |
3724 KB |
Output is correct |
8 |
Correct |
11 ms |
3564 KB |
Output is correct |
9 |
Correct |
5 ms |
1516 KB |
Output is correct |
10 |
Correct |
3 ms |
1516 KB |
Output is correct |
11 |
Correct |
3 ms |
1516 KB |
Output is correct |
12 |
Correct |
5 ms |
1644 KB |
Output is correct |
13 |
Correct |
5 ms |
1664 KB |
Output is correct |
14 |
Correct |
5 ms |
1644 KB |
Output is correct |
15 |
Correct |
5 ms |
1644 KB |
Output is correct |
16 |
Correct |
18 ms |
3692 KB |
Output is correct |
17 |
Correct |
18 ms |
3692 KB |
Output is correct |
18 |
Correct |
14 ms |
3692 KB |
Output is correct |
19 |
Correct |
15 ms |
3692 KB |
Output is correct |
20 |
Correct |
451 ms |
53868 KB |
Output is correct |
21 |
Correct |
370 ms |
53760 KB |
Output is correct |
22 |
Correct |
422 ms |
53868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1132 KB |
Output is correct |
2 |
Correct |
2 ms |
1132 KB |
Output is correct |
3 |
Correct |
3 ms |
1388 KB |
Output is correct |
4 |
Correct |
2 ms |
1388 KB |
Output is correct |
5 |
Correct |
3 ms |
1388 KB |
Output is correct |
6 |
Correct |
12 ms |
3692 KB |
Output is correct |
7 |
Correct |
11 ms |
3724 KB |
Output is correct |
8 |
Correct |
11 ms |
3564 KB |
Output is correct |
9 |
Correct |
34 ms |
5996 KB |
Output is correct |
10 |
Correct |
16 ms |
3820 KB |
Output is correct |
11 |
Correct |
12 ms |
3564 KB |
Output is correct |
12 |
Correct |
13 ms |
3564 KB |
Output is correct |
13 |
Correct |
943 ms |
193528 KB |
Output is correct |
14 |
Correct |
924 ms |
193516 KB |
Output is correct |
15 |
Correct |
898 ms |
193568 KB |
Output is correct |
16 |
Correct |
888 ms |
193644 KB |
Output is correct |
17 |
Correct |
5 ms |
1516 KB |
Output is correct |
18 |
Correct |
3 ms |
1516 KB |
Output is correct |
19 |
Correct |
3 ms |
1516 KB |
Output is correct |
20 |
Correct |
5 ms |
1644 KB |
Output is correct |
21 |
Correct |
5 ms |
1664 KB |
Output is correct |
22 |
Correct |
5 ms |
1644 KB |
Output is correct |
23 |
Correct |
5 ms |
1644 KB |
Output is correct |
24 |
Correct |
18 ms |
3692 KB |
Output is correct |
25 |
Correct |
18 ms |
3692 KB |
Output is correct |
26 |
Correct |
14 ms |
3692 KB |
Output is correct |
27 |
Correct |
15 ms |
3692 KB |
Output is correct |
28 |
Correct |
451 ms |
53868 KB |
Output is correct |
29 |
Correct |
370 ms |
53760 KB |
Output is correct |
30 |
Correct |
422 ms |
53868 KB |
Output is correct |
31 |
Correct |
1672 ms |
195948 KB |
Output is correct |
32 |
Correct |
1417 ms |
197100 KB |
Output is correct |
33 |
Correct |
1612 ms |
196972 KB |
Output is correct |
34 |
Correct |
1427 ms |
196788 KB |
Output is correct |
35 |
Correct |
1418 ms |
196844 KB |
Output is correct |
36 |
Correct |
1420 ms |
196896 KB |
Output is correct |