[bzoj3289]Mato的文件管理 莫队+树状数组
来源:互联网 发布:模糊图片修复软件 编辑:程序博客网 时间:2024/06/05 10:39
3289: Mato的文件管理
Time Limit: 40 Sec Memory Limit: 128 MB[Submit][Status][Discuss]
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
4
1 4 2 3
2
1 2
2 4
1 4 2 3
2
1 2
2 4
Sample Output
0
2
2
HINT
Hint
n,q <= 50000
样例解释:第一天,Mato不需要交换
第二天,Mato可以把2号交换2次移到最后。
Source
树状数组维护逆序对就行了
#include<algorithm>#include<iostream>#include<cstdio>#include<cmath>using namespace std;const int N = 50000 + 5;struct data{ int l,r,id,ans; }q[N];int ans,ans2[N];int a[N],bel[N],c[N],b[N],n,m;bool cmp( data a, data b ){ return bel[a.l] == bel[b.l] ? a.r < b.r : bel[a.l] < bel[b.l]; }void modify( int x, int val ){ for( int i = x; i <= n; i += i&-i ) c[i] += val; }int query( int x ){ int res = 0; for( int i = x; i; i -= i&-i ) res += c[i]; return res; }void modui(){sort(q+1,q+m+1,cmp);int l = 1, r = 0;for( int i = 1; i <= m; i++ ){while( l < q[i].l ) modify(a[l],-1),ans-=query(a[l]-1),l++;while( l > q[i].l ) l--,modify(a[l],1),ans+=query(a[l]-1);while( r < q[i].r ) r++,modify(a[r],1),ans+=r-l+1-query(a[r]);while( r > q[i].r ) modify(a[r],-1),ans-=r-l-query(a[r]),r--;ans2[q[i].id] = ans;}}int main(){scanf("%d", &n); int block = sqrt(n);for( int i = 1; i <= n; i++ ) bel[i] = (i-1)/block+1;for( int i = 1; i <= n; i++ ) scanf("%d", &a[i]), b[i]=a[i];sort(b+1,b+n+1);for( int i = 1; i <= n; i++ ) a[i] = lower_bound(b+1,b+n+1,a[i])-b;scanf("%d", &m);for( int i = 1; i <= m; i++ ) scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i;modui();for( int i = 1; i <= m; i++ ) printf("%d\n", ans2[i]);return 0;}
阅读全文
0 0
- [BZOJ3289]Mato的文件管理(莫队+树状数组)
- 【BZOJ3289】Mato的文件管理,莫队+树状数组
- 莫队+树状数组 题解【bzoj3289】Mato的文件管理
- bzoj3289 Mato的文件管理 莫队+树状数组
- [bzoj3289]Mato的文件管理 莫队+树状数组
- BZOJ3289 Mato的文件管理 【莫队 + 树状数组】
- bzoj3289 Mato的文件管理 莫队算法 树状数组
- 【BZOJ3289】Mato的文件管理(莫队算法+树状数组)
- bzoj3289 Mato的文件管理(树状数组求逆序对+莫队+离散化)
- BZOJ3289: Mato的文件管理 莫队
- bzoj3289: Mato的文件管理
- bzoj3289 : Mato的文件管理
- Bzoj3289: Mato的文件管理
- [bzoj3289]Mato的文件管理
- bzoj3289 Mato的文件管理
- 【BZOJ3289】 Mato的文件管理
- 【bzoj3289】 Mato的文件管理
- bzoj3289 Mato的文件管理
- 学习Hadoop第三十课(Hive简单入门)
- java求图形的面积(两种比较)
- 欢迎使用CSDN-markdown编辑器
- Ehcache的应用
- [Algorithm] beam search(集束搜索)
- [bzoj3289]Mato的文件管理 莫队+树状数组
- 多态&多态对象模型
- 复合文档格式(四)
- E
- RESTful API总结
- Spring整合Redis之集群与故障转移
- nyoj1309 冰法斗雪龙
- 《深入理解java虚拟机》学习-第二章-自动内存管理机制
- js里面Math对象常用属性