当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

低成本、快速、高效搭建数字藏品APP、H5系统,扇贝科技专业开发更放心!

Keil V5 installation and use

解析Steam教育的课程设计测评体系

UE4 键盘控制开关灯

1. Mx6u system migration-6-uboot graphical configuration

第三篇如何使用SourceTree提交代码

Steam science education endows classroom teaching with creativity

Spark Structured Streaming HelloWorld

数组排序1

TIA botu WinCC Pro controls the display and hiding of layers through scripts
随机推荐
九、文件上传和下载
Dijango learning
Unable to find sygwin.s file during vscode debugging
Wu Enda's machine learning after class exercises - logical regression
Several methods of realizing high-low byte or high-low word exchange in TIA botu s7-1200
MapReduce中分区数与ReduceTask个数关系比较
再获认可 | 赛宁网安连续上榜《CCSIP 2022中国网络安全产业全景图》
7、 Restful
这种是我的vs没连上数据库吗
How to write the abbreviation of the thesis?
What are the consequences and problems of computer system restoration
[learning notes] agc041
egg-ts-sequelize-CLI
Li Kou daily question - day 42 -661. Picture smoother
FFmpeg 视频编码
UE4 靠近物体时显示文字,远离时文字消失
Optimization analysis and efficiency execution of MySQL
UE4 通过按键控制物体的旋转
UE4 two ways to obtain player control
Codeforces Round #807 (Div. 2)