1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include"bits/stdc++.h" #define re register #define int long long #define lb(x) (x&(-x)) using namespace std; const int maxn=5e5+10,V=1e5,maxm=350; int n,m,siz,tot; int a[maxn],ans[maxn]; int pre1[maxn],pre2[maxn],pre3[maxn]; int st[maxm],ed[maxm],bel[V+10],idx[V+10]; int cnt[V+10],tag1[maxm]; int sum[V+10],tag2[maxm]; struct query{ int l,r,id,ans; }q[maxn]; struct node{ int l,r,id,op; }; vector<node> g[maxn]; struct BIT{ int tr[V+10]; inline void clear(){memset(tr,0,sizeof tr);} inline void add(int x,int val){while(x<=V) tr[x]+=val,x+=lb(x);} inline int query(int x){int res=0;while(x) res+=tr[x],x-=lb(x);return res;} }s1,s2; inline bool cmp(query a,query b){return ((a.l/siz)^(b.l/siz))?(a.l/siz)<(b.l/siz):((a.l/siz)&1)?a.r<b.r:a.r>b.r;} inline void init(){ siz=320; tot=V/siz; for(re int i=1;i<=tot;++i) st[i]=(i-1)*siz+1,ed[i]=i*siz; if(ed[tot]<V) ++tot,st[tot]=ed[tot-1]+1,ed[tot]=V; for(re int i=1;i<=tot;++i) for(re int j=st[i];j<=ed[i];++j) bel[j]=i,idx[j]=j-st[i]+1; } inline void add(int x){ if(tag1[bel[x]]){ for(re int i=st[bel[x]];i<=ed[bel[x]];++i) cnt[i]+=tag1[bel[x]]; tag1[bel[x]]=0; } if(tag2[bel[x]]){ for(re int i=st[bel[x]];i<=ed[bel[x]];++i) sum[i]+=tag2[bel[x]]; tag2[bel[x]]=0; } for(re int i=x;i<=ed[bel[x]];++i) ++cnt[i],sum[i]+=x; for(re int i=bel[x]+1;i<=tot;++i) ++tag1[i],tag2[i]+=x; } inline int query1(int x){return tag1[bel[x]]+cnt[x];} inline int query2(int x){return tag2[bel[x]]+sum[x];} inline void solve(){ for(re int i=1;i<=n;++i){ add(a[i]); for(auto x:g[i]){ for(re int j=x.l;j<=x.r;++j){ q[x.id].ans+=x.op*(query1(a[j]-1)*a[j]+query2(V)-query2(a[j])); } } } } signed main(){ #ifndef ONLINE_JUDGE freopen("1.in","r",stdin); freopen("1.out","w",stdout); #endif ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>n>>m; for(re int i=1;i<=n;++i) cin>>a[i]; for(re int i=1;i<=n;++i){ pre1[i]=pre1[i-1]+s1.query(a[i]-1)*a[i]; pre2[i]=pre2[i-1]+s2.query(V)-s2.query(a[i]); pre3[i]=pre3[i-1]+a[i]; s1.add(a[i],1),s2.add(a[i],a[i]); } for(re int i=1;i<=m;++i) cin>>q[i].l>>q[i].r,q[i].id=i; siz=sqrt(n); sort(q+1,q+m+1,cmp); int l=1,r=0; for(re int i=1;i<=m;++i){ if(r<q[i].r){ g[l-1].push_back({r+1,q[i].r,i,-1}); q[i].ans+=(pre1[q[i].r]-pre1[r]+pre2[q[i].r]-pre2[r]); q[i].ans+=(pre3[q[i].r]-pre3[r]); r=q[i].r; } if(l>q[i].l){ g[r].push_back({q[i].l,l-1,i,1}); q[i].ans-=(pre1[l-1]-pre1[q[i].l-1]+pre2[l-1]-pre2[q[i].l-1]); q[i].ans+=(pre3[l-1]-pre3[q[i].l-1]); l=q[i].l; } if(r>q[i].r){ g[l-1].push_back({q[i].r+1,r,i,1}); q[i].ans-=(pre1[r]-pre1[q[i].r]+pre2[r]-pre2[q[i].r]); q[i].ans-=(pre3[r]-pre3[q[i].r]); r=q[i].r; } if(l<q[i].l){ g[r].push_back({l,q[i].l-1,i,-1}); q[i].ans+=(pre1[q[i].l-1]-pre1[l-1]+pre2[q[i].l-1]-pre2[l-1]); q[i].ans-=(pre3[q[i].l-1]-pre3[l-1]); l=q[i].l; } } init(); solve(); for(re int i=1;i<=m;++i) q[i].ans+=q[i-1].ans; for(re int i=1;i<=m;++i) ans[q[i].id]=q[i].ans; for(re int i=1;i<=m;++i) cout<<ans[i]<<'\n'; return 0; }
|