给一些询问,每个询问给出区间[L, R] , 求这段区间的逆序数。
先分块排序, 然后对于每次更改, 如果是更改L, 那么应该查询区间内比他小的数的个数, 如果更改R, 查区间内比他大的数的个数。
记得离散化。
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = ;
int a[maxn], ans[maxn], sum[maxn<<], b[maxn];
void pushUp(int rt) {
sum[rt] = sum[rt<<]+sum[rt<<|];
}
void update(int p, int l, int r, int rt, int val) {
if(l == r) {
sum[rt]+=val;
return ;
}
int m = l+r>>;
if(p<=m)
update(p, lson, val);
else
update(p, rson, val);
pushUp(rt);
}
int query(int L, int R, int l, int r, int rt) {
if(R<L)
return ;
if(L<=l&&R>=r) {
return sum[rt];
}
int m = l+r>>, ret = ;
if(L<=m)
ret += query(L, R, lson);
if(R>m)
ret += query(L, R, rson);
return ret;
}
struct node
{
int block, r, l, id;
bool operator < (node a)const
{
if(block == a.block)
return r<a.r;
return block<a.block;
}
}q[maxn];
int main()
{
int n, m;
while(~scanf("%d", &n)) {
for(int i = ; i<=n; i++) {
scanf("%d", &a[i]);
b[i-] = a[i];
}
sort(b, b+n);
int cnt = unique(b, b+n)-b;
for(int i = ; i<=n; i++)
a[i] = lower_bound(b, b+cnt, a[i])-b+;
scanf("%d", &m);
int BLOCK = sqrt(n*1.0);
for(int i = ; i<m; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
q[i].block = q[i].l/BLOCK;
}
mem(sum);
sort(q, q+m);
int tmp = ;
for(int i = q[].l; i<=q[].r; i++) {
tmp += query(a[i]+, n, , n, );
update(a[i], , n, , );
}
ans[q[].id] = tmp;
for(int i = ; i<m; i++) {
for(int j = q[i-].l; j<q[i].l; j++) {
update(a[j], , n, , -);
tmp -= query(, a[j]-, , n, );
}
for(int j = q[i-].l-; j>=q[i].l; j--) {
tmp += query(, a[j]-, , n, );
update(a[j], , n, , );
}
for(int j = q[i-].r+; j<=q[i].r; j++) {
tmp += query(a[j]+, n, , n, );
update(a[j], , n, , );
}
for(int j = q[i-].r; j>q[i].r; j--) {
update(a[j], , n, , -);
tmp -= query(a[j]+, n, , n, );
}
ans[q[i].id] = tmp;
}
for(int i = ; i<m; i++) {
printf("%d\n", ans[i]);
}
}
return ;
}