HDU 5273 Dylans loves sequence
2015-06-21 00:16
537 查看
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 L≤x,y≤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≤1000,Q≤100000,L≤R,1≤a[i]≤231−1
Output
For each query,print the numbers of "inversions”
Sample Input
Sample Output
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 L≤x,y≤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≤1000,Q≤100000,L≤R,1≤a[i]≤231−1
Output
For each query,print the numbers of "inversions”
Sample Input
Sample Output
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≤x,y≤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≤1000,Q≤100000,L≤R,1≤a[i]≤231−1
Output
For each query,print the numbers of "inversions”
Sample Input
3 2 3 2 1 1 2 1 3
Sample Output
1 3 直接树状数组预处理出所有情况,然后输出即可#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; }
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 L≤x,y≤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≤1000,Q≤100000,L≤R,1≤a[i]≤231−1
Output
For each query,print the numbers of "inversions”
Sample Input
3 2 3 2 1 1 2 1 3
Sample Output
1 3
相关文章推荐
- UIActionSheet的使用
- request操作的几个容易混淆的请求路径
- UICollectionView 的使用详解
- 【BC#24 1002 HDOJ 5273】Dylans loves sequence
- java queue 实战
- continue:结束本次循环,继续下一次循环
- AndroidStudio新建项目时一直处于building“xxx”gradle project info状态
- 隐藏指定的UIView区域
- 自己实现基于key-value的NoSQL数据库(三)—— B+树和Hash算法
- @requestparam怎么用
- 机房重构--UI设计与单例思考
- Java并发编程-31-阻塞式优先级列表-PriorityBlockingQueue
- hdu 5273 Dylans loves sequence
- HDU 5273 Dylans loves sequence(区间DP)
- UISegmentedControl的详细使用
- hdu 5273 Dylans loves sequence
- UVa1152 - 4 Values whose Sum is 0(hash)
- UVa1605 - Building for UN(构造)
- GitLab:解决Merge Request中Commits不更新的问题
- ABP 初探 之基于EasyUI的CURD