Sherlock and Inversions
After a long series of jobless days, Sherlock and Watson frustated by the lack of new cases, decide to turn their minds toward something interesting like solving few logical problems. One of the problems they have to solve now is as follows:
Given an array A of size N and a list of Q number of queries, where each query has two numbers L and R, find for each query the number of inversions in the subarray from Lth to Rth position (both inclusive) of the array A( array has 1-based index).
For an array A two elements A[i] and A[j] form an inversion if A[i] > A[j] and i < j. Help them to solve this problem.
Input
- First line contains a single integer, length N of the array.
- Second line has N space separated numbers, where ith element is A[i].
- Third line has a single integer Q.
- Next Q lines have 2 space separated integers L and R of that query.
Output
- Output Q lines, ith line having a single integer, the answer to the ith query
Constraints
- 1 ≤ N ≤ 20000
- 0 ≤ A[i] ≤ 10^9
- 1 ≤ Q ≤ 20000
- 1 ≤ L ≤ R ≤ N
Example
Input:题意是给了一个固定的数组,然后给出Q个区间查询,询问每一个区间内有多少对元素,满足大的元素在前面,小的元素在后面。7 7 9 3 5 1 6 4 4 1 4 3 5 1 2 1 7
Output:4 2 0 14
#pragma comment(linker, "/STACK:102400000,102400000")
#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3fffffff
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 20005;
struct no
{
int le, ri, id;
}qu[maxn];
map<int, int>has;
int n, q, nx, bk;
int A[maxn], B[maxn], res[maxn], ans[maxn];
bool cmp(no n1, no n2)
{
if (n1.le / bk == n2.le / bk)
{
return n1.ri < n2.ri;
}
else
{
return n1.le / bk < n2.le / bk;
}
}
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int value)
{
while (x <= nx)
{
ans[x] += value;
x = x + lowbit(x);
}
}
int sum(int x)
{
int res = 0;
while (x>0)
{
res += ans[x];
x = x - lowbit(x);
}
return res;
}
void input()
{
int i, u, v;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
scanf("%d", &A[i]);
B[i] = A[i];
}
sort(B + 1, B + n + 1);
nx = 0;
for (i = 1; i <= n; i++)
{
if (has[B[i]] == 0)
{
nx++;
has[B[i]] = nx;
}
}
scanf("%d", &q);
for (i = 1; i <= q; i++)
{
scanf("%d%d", &u, &v);
qu[i].id = i;
qu[i].le = u;
qu[i].ri = v;
}
}
void solve()
{
bk = sqrt(1.0*n);
sort(qu + 1, qu + q + 1, cmp);
int i, j, id, pl = 1, pr = 0, an = 0;
for (i = 1; i <= q; i++)
{
id = qu[i].id;
if (qu[i].le == qu[i].ri)
{
res[id] = 0;
continue;
}
if (pr < qu[i].ri)
{
for (j = pr + 1; j <= qu[i].ri; j++)
{
int re = sum(has[A[j]]);
an += ((j - pl) - re);
add(has[A[j]], 1);
}
}
else
{
for (j = pr; j > qu[i].ri; j--)
{
int re = sum(has[A[j]]) - 1;
an -= ((j - pl) - re);
add(has[A[j]], -1);
}
}
pr = qu[i].ri;
if (pl < qu[i].le)
{
for (j = pl; j < qu[i].le; j++)
{
add(has[A[j]], -1);
int re = sum(has[A[j]] - 1);
an -= re;
}
}
else
{
for (j = pl - 1; j >= qu[i].le; j--)
{
int re = sum(has[A[j]] - 1);
an += re;
add(has[A[j]], 1);
}
}
pl = qu[i].le;
res[id] = an;
}
for (i = 1; i <= q; i++)
{
printf("%d\n", res[i]);
}
}
int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout);
input();
solve();
return 0;
}