hdu 5273 Dylans loves sequence

时间:2022-09-17 08:27:39

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5273

Dylans loves sequence

Description

Dylans is given $N$ numbers $a[1]....a[N]$

And there are $Q$ questions.

Each question is like this $(L,R)$

his goal is to find the “inversions” from number $L$ to number $R.$

more formally,his needs to find the numbers of pair$(x,y)$,
that $L \leq x,y \leq R$ and $ x < y $ and $a[x] > a[y]$

Input

In the first line there is two numbers $N$ and $Q.$

Then in the second line there are $N$ numbers:$a[1]..a[N]$

In the next $Q$ lines,there are two numbers $L,R$ in each line.

$N \leq 1000, Q \leq 100000, L \leq R, 1 \leq a[i] \leq 2^{31}-1$

Output

For each query,print the numbers of "inversions”

SampleInput

3 2
3 2 1
1 2
1 3

SampleOutput

1

3

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::set;
using std::map;
using std::pair;
using std::vector;
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fork(i, k, n) for(int i = (int)k; i<= (int)n; i++)
#define forp(i, k, p) for(int i = (int)k; i > p; i--)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
const int Max_N = ;
typedef unsigned long long ull;
int dp[Max_N][Max_N], arr[Max_N];
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w+",stdout);
#endif
int n, q, x, y;
while(~scanf("%d %d",&n, &q)) {
rep(i,n) scanf("%d",&arr[i + ]);
cls(dp, );
fork(i, , n) {
fork(j, i + , n) dp[i][j] += dp[i][j - ] + (int)(arr[i] > arr[j]);
}
forp(j, n, ) {
forp(i, j ,) dp[i][j] += dp[i + ][j];
}
rep(i, q) scanf("%d %d",&x, &y), printf("%d\n",dp[x][y]);
}
return ;
}