Codechef:Sherlock and Inversions

                                      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:

7 7 9 3 5 1 6 4 4 1 4 3 5 1 2 1 7

Output:

4 2 0 14

题意是给了一个固定的数组,然后给出Q个区间查询,询问每一个区间内有多少对元素,满足大的元素在前面,小的元素在后面。
莫队+树状数组。感觉莫队的思想就是想清楚了每一个元素对整体区间的贡献,记录好这些区间与单个元素的信息。
代码:
#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;
} 




1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值