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

    Mato的文件管理

    Time Limit: 40 Sec  Memory Limit: 128 MB
    Submit: 3394  Solved: 1394
    [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

    Sample Output

    0
    2


    HINT

    Hint

    n,q <= 50000

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

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

    这道提听说可以用主席树来做,然而我并不会
    用莫队+树状数组水过了,一遍AC,开心
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int pos[50005],a[50005],b[50005],n,now,ans[50005],m,tree[50005];
    struct note{
        int l,r,id;
        friend bool operator <(note a,note b)
        {
            if(pos[a.l]==pos[b.l]) return a.r<b.r;
            return a.l<b.l;
        }
    }edge[50005];
    #define lowbit(x) x&-x
    void update(int u,int c)
    {
        for(int p=u;p<=n;tree[p]+=c,p+=lowbit(p));
    }
    int query(int u)
    {
        int sum=0;
        for(int p=u;p>0;sum+=tree[p],p-=lowbit(p));
        return sum;
    }
    void prepare()
    {
        int block=sqrt(n);
        for(int i=1;i<=n;i++)
        pos[i]=(i-1)/block+1;
        for(int i=1;i<=n;i++)
        b[i]=lower_bound(a+1,a+1+n,b[i])-a;    
    }
    void solve()
    {
        int l=1,r=0;
        for(int i=1;i<=m;i++)
        {
            while(l<edge[i].l){update(b[l],-1),now-=query(b[l]-1),l++;}
            while(r>edge[i].r){update(b[r],-1),now-=r-l-query(b[r]),r--;}
            while(l>edge[i].l){l--,update(b[l],1),now+=query(b[l]-1);}
            while(r<edge[i].r){r++,update(b[r],1),now+=r-l+1-query(b[r]);}
            ans[edge[i].id]=now;
        }
        
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        scanf("%d",&a[i]),b[i]=a[i];
        sort(a+1,a+1+n);    
        prepare();
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d",&edge[i].l,&edge[i].r);
            edge[i].id=i;
        }
        sort(edge+1,edge+1+m);
        solve();
        for(int i=1;i<=m;i++)
        printf("%d
    ",ans[i]);
    }
  • 相关阅读:
    设计一个栈,除了pop与push方法,还支持Min方法,可返回栈元素中的最小值,push、pop、min三个方法的时间复杂度必须是O(1)
    删除单向链表中间的某个节点,假定你只能访问该节点
    找出单向链表中倒数第K个节点
    关于ssh纠错笔记
    3维图像分割显示 标签: 图像分割level set3dmatlab 2015-04-16 17:59 228人阅读 评论(0)
    EM 算法 标签: clusteringalgorithm图像分割EM算法 2015-03-24 21:26 426人阅读 评论(0)
    kmeans算法分割图像 标签: kmeans图像分割算法 2015-03-23 20:02 509人阅读 评论(0)
    随机数
    IDL 日期时间函数整理
    显示程序运行时间_matlab
  • 原文地址:https://www.cnblogs.com/dancer16/p/7346693.html
Copyright © 2011-2022 走看看