#include <bits/stdc++.h>
#define int long long
using namespace std;
bool st;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int a[N];
int cnt[N*4][3][3],ct[N*4][3];
int dy[N*4][3];
void push_up(int u) {
for(int i:{0,1,2}) ct[u][i]=ct[u<<1][i]+ct[u<<1|1][i];
for(int i:{0,1,2}) {
for(int j:{0,1,2}) {
cnt[u][i][j]=cnt[u<<1][i][j]+cnt[u<<1|1][i][j];
cnt[u][i][j]+=ct[u][i]*ct[u<<1|1][j];
}
}
}
void opt(int u,int a,int b,int c) {
dy[u][0]=a,dy[u][1]=b,dy[u][2]=c;
int newcnt[3][3]={{0,0,0},{0,0,0},{0,0,0}},newct[3]={0,0,0};
for(int i:{0,1,2}) {
for(int j:{0,1,2}) {
newcnt[dy[u][i]][dy[u][j]]+=cnt[u][i][j];
} newct[dy[u][i]]+=ct[u][i];
}
for(int i:{0,1,2}) {
for(int j:{0,1,2}) {
cnt[u][i][j]=newcnt[i][j];
} ct[u][i]=newct[i];
}
}
void push_down(int u) {
if(dy[u][0]==0&&dy[u][1]==1&&dy[u][2]==2) return;
opt(u<<1,dy[u][0],dy[u][1],dy[u][2]);
opt(u<<1|1,dy[u][0],dy[u][1],dy[u][2]);
dy[u][0]=0,dy[u][1]=1,dy[u][2]=2;
push_up(u);
}
void build(int u,int l,int r) {
dy[u][0]=0,dy[u][1]=1,dy[u][2]=2;
if(l==r) {
ct[u][a[l]]++;
return;
} int mid=l+r>>1;
build(u<<1,l,mid);
build(u<<1|1,mid+1,r);
push_up(u);
}
void update(int u,int l,int r,int ql,int qr,int a,int b,int c) {
if(ql<=l&&r<=qr) {
opt(u,a,b,c);
return;
} int mid=l+r>>1;
push_down(u);
if(ql<=mid) update(u<<1,l,mid,ql,qr,a,b,c);
if(qr>mid) update(u<<1|1,mid+1,r,ql,qr,a,b,c);
push_up(u);
}
struct node {
int cn[3];
void operator +=(const node b) {
cn[0]+=b.cn[0],
cn[1]+=b.cn[1],
cn[2]+=b.cn[2];
}
};
pair<int,node> query(int u,int l,int r,int ql,int qr) {
if(ql<=l&&r<=qr) {
node zc={ct[u][0],ct[u][1],ct[u][2]};
return make_pair(cnt[u][2][1]+cnt[u][2][0]+cnt[u][1][0],zc);
}
int mid=l+r>>1,res=0;
node ans={0,0,0},zc1={0,0,0},zc2={0,0,0};
push_down(u);
if(ql<=mid) {
auto zc=query(u<<1,l,mid,ql,qr);
res+=zc.first;
ans+=zc.second;
zc1=zc.second;
}
if(qr>mid) {
auto zc=query(u<<1|1,mid+1,r,ql,qr);
res+=zc.first;
ans+=zc.second;
zc2=zc.second;
}
res+=zc1.cn[2]*zc2.cn[1];
res+=zc1.cn[2]*zc2.cn[0];
res+=zc1.cn[1]*zc2.cn[0];
return make_pair(res,ans);
}
bool ed;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr),
cout.tie(nullptr);
int n,T;
cin>>n>>T;
for(int i=1;i<=n;++i) cin>>a[i];
build(1,1,n);
while(T--) {
int op;
cin>>op;
if(op==1) {
int l,r;
cin>>l>>r;
cout<<query(1,1,n,l,r).first<<'\n';
} else {
int l,r,s,t,u;
cin>>l>>r>>s>>t>>u;
update(1,1,n,l,r,s,t,u);
}
}
return 0;
}
| Set Name | Sample | All |
|---|
| Score / Max | Score 0 / 0 | 0 / 600 |
| Status | AC × 2 | AC × 4 WA × 18 |