当前位置:网站首页>2022河南萌新联赛第(三)场:河南大学 B - 逆序对计数
2022河南萌新联赛第(三)场:河南大学 B - 逆序对计数
2022-07-26 04:29:00 【WA_自动机】
B - 逆序对计数
解法:首先, 对于一个排列, 如果区间反转, 逆序数等于区间种所有的
对数减去当前的逆序对数, 即原本的正序对变为逆序对, 原本的逆
序对变为正序对。如果对于每个位置记录其到后面所有位置的逆
序对个数即可解决本题, 考虑从 [ l , r ] → [ l , r + 1 ] [l, r] → [l, r + 1] [l,r]→[l,r+1] 逆序对的个数增
加区间 [ l , r ] [l, r] [l,r] 中比 a r + 1 a_r+1 ar+1 的数的个数, 所以可以事前统计对于每一个
位置, 到他的前面的每一个位置比他大的数有多少个, 统计完直接
计算逆序对即可。
- 时间复杂度 O ( n 2 ) O(n^2) O(n2)
当然也可以使用树状数组统计逆序对, 可以很好的通过本题。
- 时间复杂度近似 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;
}
边栏推荐
- Analyzing the curriculum design evaluation system of steam Education
- 旋转数组最小数字
- Life related - ten years of career experience (turn)
- How to write the introduction and conclusion of an overview paper?
- Segger embedded studio cannot find xxx.c or xxx.h file
- Solution: runtimeerror: expected object of scalar type int but got scalar type double
- Under the high debt of Red Star Macalline, is it eyeing new energy?
- How does win11 set the theme color of the status bar? Win11 method of setting theme color of status bar
- egg-sequelize TS编写
- Network Security Learning - permission promotion 2
猜你喜欢

数组排序1

How to download the supplementary literature?

UE4 获取玩家控制权的两种方式

Acwing_12. 背包问题求具体方案_dp

Keil V5 installation and use

Steam science education endows classroom teaching with creativity

Tutorial on using the one click upgrade function of the rtsp/onvif protocol video platform easynvr service

p-范数(2-范数 即 欧几里得范数)

Matlab drawing

Sangi diagram of machine learning (for user behavior analysis)
随机推荐
Getting started with mongodb Basics
[learning notes] agc041
Which websites can I visit to check the latest medical literature?
What are the duplicate check rules for English papers?
2022杭电多校 DOS Card(线段树)
解决:RuntimeError: Expected object of scalar type Int but got scalar type Double
数组排序2
建设面向青少年的创客教育实验室
P-norm (2-norm is Euclidean norm)
UE4 键盘控制开关灯
What are the consequences and problems of computer system restoration
综合评价与决策方法
MySQL only checks the reasons for the slow execution of one line statements
UE4 获取玩家控制权的两种方式
Scroll view pull-down refresh and pull-up load (bottom)
FFmpeg 视频编码
MySQL - multi table query - Cartesian product sum, correct multi table query, equivalent connection and unequal connection, inner connection and outer connection
Under the high debt of Red Star Macalline, is it eyeing new energy?
qt编译报错整理及Remote模块下载
Why is mongodb fast