First enumerate each number, see it before there are several smaller than it, calculate and for sum1, behind a few bigger than it, calculate and for sum2, the contribution of the answer for A[I]*SUM1*SUM2.
After discretization, the tree array is available.
is upside down, along one side, statistics OK, discrete is for size, order (easy to go heavy)
1#include <cstdio>2#include <algorithm>3#include <cmath>4#include <iostream>5#include <cstring>6#include <map>7 #defineMoD 192608178 #defineN 3000079 #definell Long LongTen using namespacestd; One A intN,top; - ll ans; - ll A[n],tree[n],c[n]; themap<int,int>p; - structNode - { - intZhi,id; + }b[n]; - + BOOLcmp (Node x,node y) A { at returnx.zhi<Y.zhi; - } - intLowbit (intx) {returnx& (-x);} -ll query (intx) - { -ll res=0; in for(inti=x;i>=1; i-=lowbit (i)) -res+=Tree[i]; tores%=MoD; + returnRes; - } the voidAddintx,ll z) * { $ for(inti=x;i<=top;i+=lowbit (i))Panax Notoginsengtree[i]= (tree[i]+z)%MoD; - } the intMain () + { A p.clear (); thescanf"%d",&n); + for(intI=1; i<=n;i++) - { $scanf"%d",&a[i]); $a[i]%=MoD; -B[i].zhi=A[i]; -B[i].id=i; the } -Sort (b +1, b+n+1, CMP);Wuyib[0].zhi=- +; the for(intI=1; i<=n;i++) - if(b[i].zhi!=b[i-1].zhi) p[b[i].zhi]=++top; Wu for(intI=1; i<=n;i++) - { AboutC[i]=1ll*query (p[a[i]]-1) *a[i]%MoD; $ Add (P[a[i]],a[i]); - } -memset (Tree,0,sizeof(tree)); - for(inti=n;i>=1; i--) A { +ans= (ans+1ll*c[i]* (query (top)-query (P[a[i]) +mod))%MoD; the Add (P[a[i]],a[i]); - } $printf"%lld", ans); the}
Bzoj 5055: Film Mage tree array + discrete