hdu 5273 Dylans loves sequence

题目连接

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

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<map>
 8 #include<set>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::set;
15 using std::map;
16 using std::pair;
17 using std::vector;
18 #define sz(c) (int)(c).size()
19 #define all(c) (c).begin(), (c).end()
20 #define iter(c) __typeof((c).begin())
21 #define cls(arr,val) memset(arr,val,sizeof(arr))
22 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
23 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
24 #define fork(i, k, n) for(int i = (int)k; i<= (int)n; i++)
25 #define forp(i, k, p) for(int i = (int)k; i > p; i--)
26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
27 #define pb(e) push_back(e)
28 #define mp(a, b) make_pair(a, b)
29 const int Max_N = 1010;
30 typedef unsigned long long ull;
31 int dp[Max_N][Max_N], arr[Max_N];
32 int main() {
33 #ifdef LOCAL
34     freopen("in.txt","r",stdin);
35     freopen("out.txt","w+",stdout);
36 #endif
37     int n, q, x, y;
38     while(~scanf("%d %d",&n, &q)) {
39         rep(i,n) scanf("%d",&arr[i + 1]);
40         cls(dp, 0);
41         fork(i, 1, n) {
42             fork(j, i + 1, n) dp[i][j] += dp[i][j - 1] + (int)(arr[i] > arr[j]);
43         }
44         forp(j, n, 0) {
45             forp(i, j ,0) dp[i][j] += dp[i + 1][j];
46         }
47         rep(i, q) scanf("%d %d",&x, &y), printf("%d\n",dp[x][y]);
48     }
49     return 0;
50 }
View Code

 

posted @ 2015-06-21 19:19  GadyPu  阅读(130)  评论(0编辑  收藏  举报