//Sa~ka~na~
#include<bits/stdc++.h>
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#define rep(i,a,b) for (int i=(a);i<=(b);i++)
#define Rep(i,a,b) for (int i=(a);i>=(b);i--)
#define grep(i,a,b) for (auto i : a[b])
using namespace std;
namespace fast_IO {
#define IOSIZE 100000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
typedef long long ll;
const int N=1e5+10;
int n,m,e[N];
ll res[N];
int bel[N],pre[N],nex[N];
struct Ques {
int l,r,idx;
ll ans;
}q[N];
namespace BIT {
ll t[N];
#define lowbit(x) (x&-x)
inline void modify(int x,int val) {
while (x<=n) {
t[x]+=val;
x+=lowbit(x);
}
return ;
}
inline ll query(int x) {
ll res=0;
while (x) {
res+=t[x];
x-=lowbit(x);
}
return res;
}
}using namespace BIT;
int b[N],len,S,T;
int L[N],R[N],bl[N];
inline void init() {
T=sqrt(n);
rep(i,1,n) bel[i]=(i-1)/S+1;
rep(i,1,n) bl[i]=(i-1)/T+1;
rep(i,1,bl[n]) L[i]=(i-1)*T+1,R[i]=i*T;R[bl[n]]=n;
sort(b+1,b+n+1);
len=unique(b+1,b+n+1)-b-1;
rep(i,1,n) e[i]=lower_bound(b+1,b+len+1,e[i])-b;
rep(i,1,n) {
pre[i]=query(e[i]-1);
nex[i]=query(len)-query(e[i]);
modify(e[i],1);
}
return ;
}
struct node {
int l,r,idx,op1,op2;
};
vector<node> a[N];
ll sum1[N],pr[N],suf[N],sum2[N];
inline void modify(int x) {
int loc=bl[x];
rep(i,loc+1,bl[n]) sum1[i]++;
Rep(i,loc-1,1) sum2[i]++;
rep(i,L[loc],x-1) suf[i]++;
rep(i,x+1,R[loc]) pr[i]++;
return ;
}
inline ll query(int x,int op,int op2) {//op2:0/1 小/大
ll res=0,loc=bl[x];
if (!op2) res=sum1[loc]+pr[x];
else res=sum2[loc]+suf[x];
return op*res;
}
signed main() {
cin>>n>>m;S=n/sqrt(m);
rep(i,1,n) cin>>e[i],b[i]=e[i];
init();
rep(i,1,m) {
cin>>q[i].l>>q[i].r;
q[i].idx=i;
}
sort(q+1,q+m+1,[](Ques x,Ques y){return bel[x.l]==bel[y.l]?x.r<y.r:bel[x.l]<bel[y.l];});
int l=1,r=0;
rep(i,1,m) {
if (r<q[i].r) a[l-1].pb({r+1,q[i].r,i,-1,1});
while (r<q[i].r) q[i].ans+=nex[++r];
if (l>q[i].l) a[r].pb({q[i].l,l-1,i,1,0});
while (l>q[i].l) q[i].ans-=pre[--l];
if (r>q[i].r) a[l-1].pb({q[i].r+1,r,i,1,1});
while (r>q[i].r) q[i].ans-=nex[r--];
if (l<q[i].l) a[r].pb({l,q[i].l-1,i,-1,0});
while (l<q[i].l) q[i].ans+=pre[l++];
}
rep(i,1,n) {
modify(e[i]);
for (auto [l,r,idx,op1,op2] : a[i]) {
rep(j,l,r) {
int tmp=query(e[j],op1,op2);
q[idx].ans+=tmp;
}
}
}
rep(i,1,n) q[i].ans+=q[i-1].ans,res[q[i].idx]=q[i].ans;
rep(i,1,m) cout<<res[i]<<endl;
return (0-0);
}剪贴板
剪贴板 c8ohpdc0
剪贴板信息
剪贴板信息
- 收录
- 更新