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*sqrt(n)*logn).


    /**************************************************************
        Problem: 3289
        Language: C++
        Result: Accepted
        Time:6752 ms
        Memory:3048 kb
    ****************************************************************/
     
    #include<stdio.h>
    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define inf 99999999
    typedef long long ll;
    #define pi acos(-1.0)
    #define maxn 50050
    int a[maxn],unit,d[maxn],pos[maxn];
    struct node{
        int l,r,idx;
    }c[maxn];
     
    bool cmp(node a,node b)
    {
        if(a.l/unit == b.l/unit)return a.r<b.r;
        return a.l/unit < b.l/unit;
    }
    int b[maxn];
    ll ans[maxn];
    int lowbit(int x){
        return x&(-x);
    }
    void update(int pos,int num){
        while(pos<maxn){
            b[pos]+=num;pos+=lowbit(pos);
        }
     
    }
    int getsum(int pos){
        int num=0;
        while(pos>0){
            num+=b[pos];pos-=lowbit(pos);
        }
        return num;
     
    }
    int main()
    {
        int n,m,i,j,tot;
        while(scanf("%d",&n)!=EOF)
        {
            for(i=1;i<=n;i++){
                scanf("%d",&d[i]);
                pos[i]=d[i];
            }
            sort(pos+1,pos+1+n);
            tot=1;
            for(i=2;i<=n;i++){
                if(pos[i]!=pos[tot]){
                    tot++;pos[tot]=pos[i];
                }
            }
            for(i=1;i<=n;i++){
                a[i]=lower_bound(pos+1,pos+1+tot,d[i])-pos;
            }
            unit=(int)sqrt(n);
            scanf("%d",&m);
            for(i=1;i<=m;i++){
                scanf("%d%d",&c[i].l,&c[i].r);
                c[i].idx=i;
            }
            sort(c+1,c+1+m,cmp);
            //memset(b,0,sizeof(b));
            int l=1,r=0;
            ll num=0;
            for(i=1;i<=m;i++){
                while(r<c[i].r){
                    r++;
                    num+=getsum(50050)-getsum(a[r]);
                    update(a[r],1);
                }
                while(r>c[i].r){
                    num-=getsum(50050)-getsum(a[r]);
                    update(a[r],-1);
                    r--;
                }
                while(l<c[i].l){
                    num-=getsum(a[l]-1);
                    update(a[l],-1);
                    l++;
                }
                while(l>c[i].l){
                    l--;
                    num+=getsum(a[l]);
                    update(a[l],1);
                }
                ans[c[i].idx ]=num;
            }
            for(i=1;i<=m;i++){
                printf("%lld
    ",ans[i]);
            }
        }
        return 0;
    }
     
    /*
    4
    1 4 2 3
    2
    1 2
    2 4
    */


  • 相关阅读:
    【扫盲】i++和++i的区别
    java 字符串String.intern()方法学习
    随机访问和快速访问
    Semaphore信号量深度解析
    CyclicBarrier回环屏障深度解析
    CountDownLatch深度剖析
    静态代理和装饰者模式的区别
    AspectJ之@DeclareParents注解为对象添加新方法
    C#开发上位机常用
    使用Charles进行抓包、篡改请求、设置代理
  • 原文地址:https://www.cnblogs.com/herumw/p/9464573.html
Copyright © 2011-2022 走看看