当前位置:网站首页>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;
}
边栏推荐
- 2、 Internationally renowned project HelloWorld
- 十、拦截器
- MapReduce中分区数与ReduceTask个数关系比较
- YAPI安装
- Day26 job
- MySQL only checks the reasons for the slow execution of one line statements
- Postman 导入curl 、导出成curl、导出成对应语言代码
- What if win11 cannot wake up from sleep? Solution of win11 unable to wake up during sleep
- 远坂凛壁纸
- Network Security Learning - permission promotion 2
猜你喜欢

Codeforces Round #807 (Div. 2)

Embedded practice -- CPU utilization statistics based on rt1170 FreeRTOS (24)

TIA botu WinCC Pro controls the display and hiding of layers through scripts

qt编译报错整理及Remote模块下载

Phaser(一):平台跳跃收集游戏

1. If function of Excel

egg-ts-sequelize-CLI

Life related - ten years of career experience (turn)

理性认知教育机器人寓教于乐的辅助作用

性能和成本的综合架构:单元化架构
随机推荐
Phaser(一):平台跳跃收集游戏
UE4 switching of control rights of multiple roles
Weights & biases (II)
Dijikstra (preprocessing first) +dfs, relocation truncated to fit
远坂凛壁纸
Rotate array minimum number
低成本、快速、高效搭建数字藏品APP、H5系统,扇贝科技专业开发更放心!
Day24 job
Steam science education endows classroom teaching with creativity
FFmpeg 视频编码
Acwing_ 12. Find a specific solution for the knapsack problem_ dp
2022杭电多校 DOS Card(线段树)
View and modify the number of database connections
Analyzing the curriculum design evaluation system of steam Education
How to download the supplementary literature?
二、国际知名项目-HelloWorld
Postman 导入curl 、导出成curl、导出成对应语言代码
A series of problems about the number of DP paths
Throttling anti shake function of JS handwritten function
FFmpeg 视频添加水印