zoukankan      html  css  js  c++  java
  • bzoj3289 Mato的文件管理

    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

    Sample Output

    0
    2


    HINT

    Hint

    n,q <= 50000

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

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

    正解:莫队算法+树状数组

    将询问分块排序,然后指针转移时用树状数组统计新增点或删除点对答案的贡献,即统计逆序对,总复杂度O(n^1.5*logn)。


    //It is made by wfj_2048~
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #define inf 1<<30
    #define il inline
    #define RG register
    #define ll long long
    #define lb(x) (x & -x)
    #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
    
    using namespace std;
    
    struct node{ int l,r,id,bl; }q[50010];
    
    int a[50010],c[50010],num[50010],hsh[50010],n,m,tot,block;
    ll ans[50010];
    
    il int gi(){
        RG int x=0,q=0; RG char ch=getchar();
        while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); if (ch=='-') q=1,ch=getchar();
        while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;
    }
    
    il int cmp(const node &a,const node &b){ return a.bl<b.bl || (a.bl==b.bl && a.r<b.r); }
    
    il void add(RG int x,RG int v){ for (;x<=n;x+=lb(x)) c[x]+=v; return; }
    
    il int query(RG int x){ RG int res=0; for (;x;x-=lb(x)) res+=c[x]; return res; }
    
    il void work(){
        n=gi(); for (RG int i=1;i<=n;++i) num[i]=a[i]=gi(); sort(num+1,num+n+1); m=gi();
        hsh[++tot]=num[1]; for (RG int i=2;i<=n;++i) if (num[i]!=num[i-1]) hsh[++tot]=num[i];
        for (RG int i=1;i<=n;++i) a[i]=lower_bound(hsh+1,hsh+tot+1,a[i])-hsh; block=sqrt(n);
        for (RG int i=1;i<=m;++i) q[i].l=gi(),q[i].r=gi(),q[i].id=i,q[i].bl=(q[i].l-1)/block+1;
        sort(q+1,q+m+1,cmp); RG int L=1,R=0; RG ll Ans=0;
        for (RG int i=1;i<=m;++i){
    	while (L>q[i].l) L--,add(a[L],1),Ans+=(ll)query(a[L]-1);
    	while (R<q[i].r) R++,add(a[R],1),Ans+=(ll)query(n)-(ll)query(a[R]);
    	while (L<q[i].l) add(a[L],-1),Ans-=(ll)query(a[L]-1),L++;
    	while (R>q[i].r) add(a[R],-1),Ans-=(ll)query(n)-(ll)query(a[R]),R--;
    	ans[q[i].id]=Ans;
        }
        for (RG int i=1;i<=m;++i) printf("%lld
    ",ans[i]); return;
    }
    
    int main(){
        File("mato");
        work();
        return 0;
    }
    

  • 相关阅读:
    win10家庭版安装Docker for Windows
    docker镜像拉取速度过慢的解决
    解决docker: error pulling image configuration: Get https://registry-1.docker.io/v2/library/mysql/: TLS handshake timeout.
    ubuntu16.04下安装docker
    Ubuntu 16.04执行 sudo apt-get update非常慢解决方案
    [转载]MySQL存储过程详解  mysql 存储过程
    除非 Windows Activation Service (WAS)和万维网发布服务(W3SVC)均处于运行状态,
    windows资源管理器已经停止工作
    Shell脚本sqlldr导入数据压缩文件截断
    oracle 日常运维
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6416596.html
Copyright © 2011-2022 走看看