bzoj3289 Mato的文件管理
来源:互联网 发布:sim800c 源码 编辑:程序博客网 时间:2024/05/19 06:37
题目链接:bzoj3289
题目大意:
Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号。为了防止他人偷拷,这些资料都是加密过的,只能用Mato自己写的程序才能访问。Mato每天随机选一个区间[l,r],他今天就看编号在此区间内的这些资料。Mato有一个习惯,他总是从文件大小从小到大看资料。他先把要看的文件按编号顺序依次拷贝出来,再用他写的排序程序给文件大小排序。排序程序可以在1单位时间内交换2个相邻的文件(因为加密需要,不能随机访问)。Mato想要使文件交换次数最小,你能告诉他每天需要交换多少次吗?
额就是问区间逆序对
题解:
莫队+树状数组
裸啊。。
打的时候真的要冷静额或者说清醒?
#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;#define maxn 50100struct ask{ int l,r,id;}q[maxn];int qn;bool cmp(ask x,ask y){ int xx=x.l/qn,yy=y.l/qn; if (xx!=yy) return x.l<y.l; return x.r<y.r;}struct node{int x,id;}a[maxn];bool cmp2(node x,node y){ if (x.x!=y.x) return x.x<y.x; return x.id<y.id;}int cnt,p[maxn];LL c[maxn],ans[maxn],la[maxn];LL lowbit(LL x){return (x&(-x));}void change(LL x,LL k) {for (;x!=0 && x<=cnt;x+=lowbit(x)) c[x]+=k;}LL query(LL x){ LL ret=0; for (;x>0;x-=lowbit(x)) ret+=c[x]; return ret;}int main(){ //freopen("a.in","r",stdin); //freopen("a.out","w",stdout); int n,m,i; scanf("%d",&n); qn=(int)sqrt(n); for (i=1;i<=n;i++) {scanf("%d",&a[i].x);a[i].id=i;} sort(a+1,a+1+n,cmp2); p[a[1].id]=1;cnt=1; for (i=2;i<=n;i++) {if (a[i].x!=a[i-1].x) cnt++;p[a[i].id]=cnt;} scanf("%d",&m); for (i=1;i<=m;i++) { scanf("%d%d",&q[i].l,&q[i].r); q[i].id=i; }sort(q+1,q+1+m,cmp); memset(c,0,sizeof(c)); LL sum=0;int l=0,r=0; for (i=1;i<=m;i++) { while (r<q[i].r) {r++;sum+=query(cnt)-query(p[r]);change(p[r],1);} while (l>q[i].l) {l--;sum+=query(p[l]-1);change(p[l],1);} while (r>q[i].r) {sum-=query(cnt)-query(p[r]);change(p[r],-1);r--;} while (l<q[i].l) {sum-=query(p[l]-1);change(p[l],-1);l++;} ans[q[i].id]=sum; } for (i=1;i<=m;i++) printf("%lld\n",ans[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的文件管理 莫队算法
- Linux 新的API signalfd、timerfd、eventfd使用说明
- CCFCSP201609-2火车购票
- HDU2955 Robberies(01背包)
- MATLAB遗传算法例子二:求多元函数最小值
- java之组合模式
- bzoj3289 Mato的文件管理
- UML类图
- Web网页的请求数据写入数据库时产生乱码的解决方法
- 4.18
- php笔记
- 图与邻接链表--极简形式
- 两点间距离公式--hypot函数
- WebStorm设置Ctrl+滚轮调整字体大小
- 解决IE浏览器jQuery执行ajax不响应问题