HDU 5273Dylans loves sequence

来源:互联网 发布:360网页广告拦截软件 编辑:程序博客网 时间:2024/06/06 20:12


Problem 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 Lx,yR 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.

N1000,Q100000,LR,1a[i]2311
 

Output
For each query,print the numbers of "inversions”
 

Sample Input
3 23 2 11 21 3
 

Sample Output
13
直接树状数组预处理出所有情况,然后输出即可
#include<iostream>#include<cstdio>#include<vector>#include<iostream>#include<queue>#include<cstdlib>#include<map>using namespace std;const int maxn = 1005;const int low(int x){ return x&-x; }int f[maxn];int a[maxn], b[maxn], c[maxn][maxn];map<int, int> M;int n, q, tot, l, r;void add(int x){    for (int i = x; i <= tot; i += low(i)) f[i]++;}int sum(int x){    int ans = 0;    for (int i = x; i; i -= low(i)) ans += f[i];    return ans;}int main(){    while (scanf("%d%d", &n, &q) != EOF)    {        for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i - 1] = a[i];        sort(b, b + n);        for (int i = tot = 0; i < n; i++) M[b[i]] = ++tot;        for (int i = 1; i <= n; i++)        {            for (int j = 1; j <= tot; j++) f[j] = 0;            c[i][i] = 0;    add(M[a[i]]);            for (int j = i + 1; j <= n; j++)            {                c[i][j] = c[i][j - 1] + j - i - sum(M[a[j]]);                add(M[a[j]]);            }        }        while (q--)        {            scanf("%d%d", &l, &r);            printf("%d\n", c[l][r]);        }    }    return 0;}


0 0
原创粉丝点击