首页 技术 正文
技术 2022年11月9日
0 收藏 460 点赞 4,465 浏览 1540 个字

Description

Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号。为了防止他人偷拷,这些资料都是加密过的,只能用Mato自己写的程序才能访问。Mato每天随机选一个区间[l,r],他今天就看编号在此区间内的这些资料。Mato有一个习惯,他总是从文件大小从小到大看资料。他先把要看的文件按编号顺序依次拷贝出来,再用他写的排序程序给文件大小排序。排序程序可以在1单位时间内交换2个相邻的文件(因为加密需要,不能随机访问)。Mato想要使文件交换次数最小,你能告诉他每天需要交换多少次吗?

Input

第一行一个正整数n,表示Mato的资料份数。
第二行由空格隔开的n个正整数,第i个表示编号为i的资料的大小。
第三行一个正整数q,表示Mato会看几天资料。
之后q行每行两个正整数l、r,表示Mato这天看[l,r]区间的文件。

Output

q行,每行一个正整数,表示Mato这天需要交换的次数。

Sample Input


Sample Output


HINT

n,q <= 50000

样例解释:第一天,Mato不需要交换

第二天,Mato可以把2号交换2次移到最后。

题解

莫队算法,每次处理询问时维护区间的动态逆序对

#include<cmath>
#include<cstdio>
#include<algorithm>
#define buf 50001
using namespace std;
int F(){
int x=,c=getchar(),f=;
for(;c<||c>;c=getchar())
if(!(c^))
f=-;
for(;c>&&c<;c=getchar())
x=(x<<)+(x<<)+c-;
return x*f;
}
struct ins{
int l,r,blg,id;
bool operator<(const ins h)const{
if(!(blg^h.blg))
return r<h.r;
return blg<h.blg;
}
}s[buf];
int n,q,t,f[buf],d[buf];
unsigned int ans[buf],c[buf],now;
inline void add(int x,int d){
for(;x<=n;x+=x&-x)
c[x]+=d;
}
inline unsigned int sum(int x){
unsigned int res=;
for(;x;x-=x&-x)
res+=c[x];
return res;
}
void solve(){
sort(s+,s++q);
int l=,r=;
for(int i=;i<=q;i++){
for(;l<s[i].l;l++)
add(d[l],-),now-=sum(d[l]-);
for(;r>s[i].r;r--)
add(d[r],-),now-=r-l-sum(d[r]);
for(;l>s[i].l;)
l--,add(d[l],),now+=sum(d[l]-);
for(;r<s[i].r;)
r++,add(d[r],),now+=r-l+-sum(d[r]);
ans[s[i].id]=now;
}
}
int main(){
n=F();
t=sqrt(n);
for(int i=;i<=n;i++)
f[i]=d[i]=F();
sort(f+,f++n);
for(int i=;i<=n;i++)
d[i]=lower_bound(f+,f++n,d[i])-f;
q=F();
for(int i=;i<=q;i++)
s[i].id=i,
s[i].l=F(),
s[i].r=F(),
s[i].blg=(s[i].l-)/t+;
solve();
for(int i=;i<=q;i++)
printf("%d\n",ans[i]);
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295