当前位置:网站首页>2022 Henan Mengxin League game (3): Henan University B - reverse pair count
2022 Henan Mengxin League game (3): Henan University B - reverse pair count
2022-07-26 04:32:00 【WA_ automata】
B - Count in reverse order
solution : First , For a permutation , If the interval is reversed , The number in reverse order is equal to all the plants in the area
Logarithm minus the current reverse logarithm , That is, the original positive sequence pair becomes the reverse sequence pair , Original inverse
Sequence pairs become positive sequence pairs . If for each position, record its inverse to all subsequent positions
This problem can be solved by pairing the numbers in sequence , Consider from [ l , r ] → [ l , r + 1 ] [l, r] → [l, r + 1] [l,r]→[l,r+1] The number of reverse pairs increases
Add interval [ l , r ] [l, r] [l,r] Middle ratio a r + 1 a_r+1 ar+1 The number of , So you can make statistics in advance for each
Location , To each position in front of him, how many numbers are bigger than him , Directly after statistics
Calculate the reverse order pair .
- Time complexity O ( n 2 ) O(n^2) O(n2)
Of course, you can also use a tree array to count reverse pairs , You can pass this question well .
- Time complexity approximation O ( n 2 ) O(n^2) O(n2)
#include<cstdio>
#include<cstring>
const int N = 6010;
int a[N],tr[N],f[N][N],g[N][N];
int n;
inline int lowbit(int x)
{
return x&-x;
}
inline void add(int x,int y)
{
for(int i=x;i<=n;i+=lowbit(i))
tr[i]+=y;
}
inline int query(int x)
{
int res=0;
for(int i=x;i>0;i-=lowbit(i))
res+=tr[i];
return res;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
f[i][j]=f[i][j-1]+query(n)-query(a[j]-1);
g[i][j]=g[i][j-1]+query(a[j]);
add(a[j],1);
}
memset(tr,0,sizeof tr);
}
int q;scanf("%d",&q);
while(q--)
{
int l,r;scanf("%d%d",&l,&r);
printf("%d\n",f[1][n]-f[l][r]+g[l][r]);
}
return 0;
}
边栏推荐
- Low cost, fast and efficient construction of digital collection app and H5 system, professional development of scallop technology is more assured!
- Ffmpeg video coding
- 11、 Exception handler
- Why is mongodb fast
- 生活相关——十年的职业历程(转)
- Analyzing the curriculum design evaluation system of steam Education
- mongoDB为什么快
- Dijango learning
- UE4 two ways to obtain player control
- Li Kou daily question - day 42 -661. Picture smoother
猜你喜欢

Unable to find sygwin.s file during vscode debugging

UE4 controls the rotation of objects by pressing keys

Dijango learning
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VIII)](/img/a0/b2b0f5fb63301f5b7dd14302aa39e2.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VIII)
![[C language foundation] 13 preprocessor](/img/4c/ab25d88e9a0cf29bde6e33a2b14225.jpg)
[C language foundation] 13 preprocessor

ASP. Net core actionfilter filter details

idea插件离线安装(持续更新)

Scroll view pull-down refresh and pull-up load (bottom)

Acwing brush questions

What are the consequences and problems of computer system restoration
随机推荐
Weights & biases (II)
性能和成本的综合架构:单元化架构
Life related -- the heartfelt words of a graduate tutor of Huake (mainly applicable to science and Engineering)
Low cost, fast and efficient construction of digital collection app and H5 system, professional development of scallop technology is more assured!
1. If function of Excel
P-norm (2-norm is Euclidean norm)
QT compilation error sorting and remote module Download
2022河南萌新联赛第(三)场:河南大学 B - 逆序对计数
生活相关——十年的职业历程(转)
How does win11 set the theme color of the status bar? Win11 method of setting theme color of status bar
远坂凛壁纸
数组排序2
Ffmpeg video coding
Graph translation model
十一、异常处理器
Day26 job
Sangi diagram of machine learning (for user behavior analysis)
Scroll view pull-down refresh and pull-up load (bottom)
机器学习之信用卡欺诈检测
Yuansaka Lin wallpaper