# Description

Bakser神犇与他打算研究一下这个妹子序列,于是Bakser神犇问道:"你知道区间$[l,r]$中妹子们美丽度的逆序对数吗?"

# Input

$l,r$要分别异或上一次询问的答案$(lastans)$,最开始时$lastans=0$

4
1 4 2 3
1
2 4

# Sample Output

2

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;

#define maxn 50010
const int len = 230;
int n,m,ans,A[maxn],bac[maxn],have[len][maxn];
int rev[maxn],tree[maxn],ref2[len][maxn],ref3[len][len],tot;

inline int begin(int ord) { return (ord-1)*len+1; }
inline int end(int ord) { return min(ord*len,n); }
inline int belong(int ord) { return (ord+len-1)/len; }
inline int size(int ord) { return end(ord)-begin(ord)+1; }

inline int lowbit(int x) { return x & -x; }
inline void ins(int x,int y) { for (;x <= n;x += lowbit(x)) tree[x] += y; }
inline int calc(int x) { int ret = 0; for (;x;x -= lowbit(x)) ret += tree[x]; return ret; }

{
for (int i = 1;i <= n;++i) bac[i] = A[i];
sort(bac+1,bac+n+1); tot = unique(bac+1,bac+n+1)-bac-1;
for (int i = 1;i <= n;++i) A[i] = lower_bound(bac+1,bac+tot+1,A[i])-bac;
tot = (n + len-1)/len;
for (int i = 1;i <= tot;++i)
{
int l = begin(i),r = end(i);
for (int j = l;j <= r;++j) have[i][A[j]]++;
for (int j = 1;j <= n;++j) have[i][j] += have[i][j-1];
for (int j = l;j <= r;++j) for (int k = j+1;k <= r;++k) rev[i] += A[j] > A[k];
}
for (int i = 1;i <= tot;++i)
{
int l = begin(i),r = end(i);
for (int j = 1;j < l;++j)
ref2[i][j] += ref2[i][j-1],ref2[i][j] += have[i][A[j]-1];
for (int j = r+1;j <= n;++j)
ref2[i][j] += ref2[i][j-1],ref2[i][j] += size(i)-have[i][A[j]];
}
for (int i = 1;i <= n;++i)
{
int now = belong(i);
for (int j = 1;j < now;++j)
ref3[now][j] += size(j)-have[j][A[i]];
}
for (int i = 1;i <= tot;++i)
for (int j = 2;j < i;++j)
ref3[i][j] += ref3[i][j-1];
}

inline int work(int l,int r)
{
int tot = 0,L = belong(l),R = belong(r),p = end(L),q = begin(R);
for (int i = L+1;i < R;++i)
{
tot += rev[i];
tot += ref3[i][i-1] - ref3[i][L];
tot += ref2[i][p]-ref2[i][l-1];
tot += ref2[i][r]-ref2[i][q-1];
}
if (L != R)
{
for (int i = r;i >= q;--i) tot += calc(A[i]-1),ins(A[i],1);
for (int i = p;i >= l;--i) tot += calc(A[i]-1),ins(A[i],1);
for (int i = r;i >= q;--i) ins(A[i],-1);
for (int i = p;i >= l;--i) ins(A[i],-1);
}
else
{
for (int i = r;i >= l;--i) tot += calc(A[i]-1),ins(A[i],1);
for (int i = r;i >= l;--i) ins(A[i],-1);
}
}

int main()
{
freopen("3744.in","r",stdin);
freopen("3744.out","w",stdout);
scanf("%d",&n);
for (int i = 1;i <= n;++i) scanf("%d",A+i);