CF788E New task
Description
On the 228-th international Uzhlyandian Wars strategic game tournament teams from each country are called. The teams should consist of $5$ participants.
The team of Uzhlyandia will consist of soldiers, because there are no gamers.
Masha is a new minister of defense and gaming. The prime duty of the minister is to calculate the efficiency of the Uzhlandian army. The army consists of nn soldiers standing in a row, enumerated from $1$ to $n$ . For each soldier we know his skill in Uzhlyandian Wars: the $i$ -th soldier's skill is $a_i$ .
It was decided that the team will consist of three players and two assistants. The skills of players should be same, and the assistants' skills should not be greater than the players' skill. Moreover, it is important for Masha that one of the assistants should stand in the row to the left of the players, and the other one should stand in the row to the right of the players. Formally, a team is five soldiers with indexes $i,j,k,l,p$ , such that $1 \leq i < j < k < l < p \leq n$ and $a_i \leq a_j=a_k=a_l \geq a_p$ .
The efficiency of the army is the number of different teams Masha can choose. Two teams are considered different if there is such ii such that the ii -th soldier is a member of one team, but not a member of the other team.
Initially, all players are able to be players. For some reasons, sometimes some soldiers become unable to be players. Sometimes some soldiers, that were unable to be players, become able to be players. At any time any soldier is able to be an assistant. Masha wants to control the efficiency of the army, so she asked you to tell her the number of different possible teams modulo $1000000007(10^9+7)$after each change.
Solution
对于每种权值维护一棵线段树,在每个区间维护该种权值的个数,AB组合,DE组合,ABC组合,CDE组合,ABCDE组合的数量
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> using namespace std; int n,m,a[100005],sav[100005],tot,bits[100005],pre[100005],suf[100005],rt[100005],cnt; long long ans; const int mod=1e9+7; inline int read(){ int f=1,w=0; char ch=0; while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9')w=(w<<1)+(w<<3)+ch-'0',ch=getchar(); return f*w; } struct SGT{ int lc[4000005],rc[4000005]; long long B[4000005],D[4000005],BC[4000005],CD[4000005],BCD[4000005],sum[4000005]; void pushup(int i){sum[i]=(sum[lc[i]]+sum[rc[i]])%mod,B[i]=(B[lc[i]]+B[rc[i]])%mod,D[i]=(D[lc[i]]+D[rc[i]])%mod,BC[i]=(BC[lc[i]]+BC[rc[i]]+B[lc[i]]*sum[rc[i]]%mod)%mod,CD[i]=(CD[lc[i]]+CD[rc[i]]+D[rc[i]]*sum[lc[i]]%mod)%mod,BCD[i]=(BCD[lc[i]]+BCD[rc[i]]+B[lc[i]]*CD[rc[i]]%mod+BC[lc[i]]*D[rc[i]]%mod)%mod;} void update(int &i,int l,int r,int pos,int x,int y,int z){ if(!i)i=++cnt; if(l==r){B[i]=x,D[i]=y,sum[i]=z;return;} int mid=l+r>>1; if(pos<=mid)update(lc[i],l,mid,pos,x,y,z); else update(rc[i],mid+1,r,pos,x,y,z); pushup(i); } }tr; inline int lowbit(int x){return x&-x;} void add(int pos,int v){while(pos<=n)bits[pos]+=v,pos+=lowbit(pos);} int ask(int pos){ int ret=0; while(pos)ret+=bits[pos],pos-=lowbit(pos); return ret; } int main(){ n=read(); for(int i=1;i<=n;i++)a[i]=sav[i]=read(); sort(sav+1,sav+n+1),tot=unique(sav+1,sav+n+1)-sav-1; for(int i=1;i<=n;i++)a[i]=lower_bound(sav+1,sav+tot+1,a[i])-sav; for(int i=1;i<=n;i++)pre[i]=ask(a[i]),add(a[i],1); memset(bits,0,sizeof(bits)); for(int i=n;i;i--)suf[i]=ask(a[i]),add(a[i],1); for(int i=1;i<=n;i++)tr.update(rt[a[i]],1,n,i,pre[i],suf[i],1); for(int i=1;i<=tot;i++)(ans+=tr.BCD[rt[i]])%=mod; m=read(); for(;m;m--){ int opt=read(),x=read(); (ans+=mod-tr.BCD[rt[a[x]]])%=mod; if(opt==1)tr.update(rt[a[x]],1,n,x,0,0,0); else tr.update(rt[a[x]],1,n,x,pre[x],suf[x],1); (ans+=tr.BCD[rt[a[x]]])%=mod,printf("%lld\n",ans); } return 0; }