玄学事情。。。 把第一个样例下载下来和输出一模一样,但却是wa1 求聚聚帮忙看看,人都快调傻了
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
int read() {
char cc = getchar(); int cn = 0, flus = 1;
while(cc < '0' || cc > '9') { if( cc == '-' ) flus = -flus; cc = getchar(); }
while(cc >= '0' && cc <= '9') cn = cn * 10 + cc - '0', cc = getchar();
return cn * flus;
}
int n,m;
struct node
{
int val,l,r,id;
};
int a[maxn],sq;
vector<int> v;
struct Quary
{
int l,r,id;
}q[maxn];
bool cmp(Quary x,Quary y)
{
return x.l/sq==y.l/sq?x.r<y.r:x.l<y.l;
}
ll suml[maxn],sumr[maxn],ans[maxn];
int t[maxn];
int lowerbit(int x) {return x&(-x);}
void update(int x,int val) {while(x<=n) t[x]+=val,x+=lowerbit(x);}
int quary(int x) {int c=0; while(x) c+=t[x],x-=lowerbit(x); return c;}
vector<node> vl[maxn],vr[maxn];
int cnt[500],cur[maxn];
void add1(int x)
{
int si=x/sq;
for(int i=0;i<si;i++) cnt[i]++;
for(int i=(x/sq)*sq;i<=x;i++) cur[i]++;
}
void add2(int x)
{
int si=n/sq+1;
for(int i=x/sq+1;i<=si;i++) cnt[i]++;
si=(x/sq+1)*sq;
for(int i=x;i<si;i++) cur[i]++;
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++) a[i]=read(),v.push_back(a[i]);
sort(v.begin(),v.end());
v.resize(unique(v.begin(),v.end())-v.begin());
for(int i=1;i<=n;i++) a[i]=lower_bound(v.begin(),v.end(),a[i])-v.begin()+1;
sq=350;
for(int i=1;i<=n;i++) suml[i]=suml[i-1]+i-1-quary(a[i]),update(a[i],1);
memset(t,0,sizeof t);
for(int i=n;i>=1;i--) sumr[i]=sumr[i+1]+quary(a[i]-1),update(a[i],1);
//for(int i=1;i<=n;i++) printf("sum=%lld %lld\n",suml[i],sumr[i]);
for(int i=1;i<=m;i++)
{
q[i].l=read(),q[i].r=read();
q[i].id=i;
}
sort(q+1,q+m+1,cmp);
int l=1,r=0;
for(int i=1;i<=m;i++)
{
if(r<q[i].r) vl[l].emplace_back((node){-1,r+1,q[i].r,q[i].id}),ans[q[i].id]+=suml[q[i].r]-suml[r],r=q[i].r;
if(r>q[i].r) vl[l].emplace_back((node){1,q[i].r+1,r,q[i].id}),ans[q[i].id]-=suml[r]-suml[q[i].r],r=q[i].r;
if(l<q[i].l) vr[r].emplace_back((node){1,l,q[i].l-1,q[i].id}),ans[q[i].id]-=sumr[l]-sumr[q[i].l],l=q[i].l;
if(l>q[i].l) vr[r].emplace_back((node){-1,q[i].l,l-1,q[i].id}),ans[q[i].id]+=sumr[q[i].l]-sumr[l],l=q[i].l;
}
for(int i=1;i<=n;i++)
{
for(node j:vl[i])
{
for(int k=j.l;k<=j.r;k++)
{
ans[j.id]+=1LL*j.val*(cnt[(a[k]+1)/sq]+cur[a[k]+1]);
}
}
add1(a[i]);
}
memset(cnt,0,sizeof cnt);
memset(cur,0,sizeof cur);
for(int i=n;i>=1;i--)
{
for(node j:vr[i])
{
for(int k=j.l;k<=j.r;k++)
{
ans[j.id]+=1LL*j.val*(cnt[(a[k]-1)/sq]+cur[a[k]-1]);
}
}
add2(a[i]);
}
for(int i=2;i<=m;i++) ans[q[i].id]+=ans[q[i-1].id];
for(int i=1;i<=m;i++) printf("%lld\n",ans[i]);
return 0;
}