当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
Weights & Biases (二)
十、拦截器
7、 Restful
MySQL日志分类:错误日志、二进制日志、查询日志、慢查询日志
Throttling anti shake function of JS handwritten function
数组排序1
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VIII)
Integrated architecture of performance and cost: modular architecture
Js手写函数之节流防抖函数
Array sort 2
What are the consequences and problems of computer system restoration
Yapi installation
【UOJ 429】串串划分(Runs)(容斥)+ 有关 Lyndon Tree 及其应用的小小记录
Solve the error string value: '\xf0\x9f\x98\xad',... 'for column' commentcontent 'at row 1
Life related -- the heartfelt words of a graduate tutor of Huake (mainly applicable to science and Engineering)
How to download the supplementary literature?
RTSP/Onvif协议视频平台EasyNVR服务一键升级功能的使用教程
[uoj 429] runs (inclusive) + a little record about Lyndon tree and its application
1. Mx6u system migration-6-uboot graphical configuration
低成本、快速、高效搭建数字藏品APP、H5系统,扇贝科技专业开发更放心!








