bzoj1145: [CTSC2008]图腾totem

数学+树状数组

真是一道神题233果然符合CTSC

用来练习思维与树状数组很不错的

膜拜hzwer.com/5357.html

#include
#include
#include
#include
#include
#include
using namespace std;
int op[200005];
int a[200005];
int lc[200005],rc[200005];
int n;
int xa=0,xb=0,xc=0,xd=0;
int ans=0;
const int P=16777216;
inline int lowbit(int x)
{
       return x&(-x);
}
int find(int x)
{
    int sum=0;
    while(x>0){
         sum+=op[x];
         x-=lowbit(x);
         }
    return sum;
}
void add(int x,int val)
{
     while(x<=n){
          op[x]+=val;
          x+=lowbit(x);
          }
}
int main()
{
    int i,j,k,l;
    scanf("%d",&n);
    for(i=1;i<=n;i++)scanf("%d",&a[i]);
    for(i=1;i<=n;i++){
        lc[i]=find(a[i]);
        add(a[i],1);
        }
    for(i=1;i<=n;i++)
        rc[i]=a[i]-1-lc[i];
    memset(op,0,sizeof op);
    for(i=1;i<=n;i++){
        ans=(ans+find(a[i])*(n-i-rc[i]))%P;
        add(a[i],lc[i]);
        }
    memset(op,0,sizeof op);
    for(i=1;i<=n;i++){
       ans=(ans+(lc[i]*(i-1)-find(a[i])-lc[i]*(lc[i]-1)/2)*(n-i-rc[i])%P)%P;
       add(a[i],i);
       }
    memset(op,0,sizeof op);
    for(i=1;i<=n;i++){
        ans=(ans+(lc[i]*(a[i]-1)-find(a[i])-lc[i]*(lc[i]-1)/2)*(n-i-rc[i])%P)%P;
        add(a[i],a[i]);
        }
    for(i=1;i<=n;i++){
        xa=n-i-rc[i];
        ans=(ans-((long long)xa*(xa-1)*(xa-2)/6)%P+P)%P;
        }
    printf("%d",ans);
    return 0;
}


你可能感兴趣的:(bzoj,数学,树状数组)