zoukankan      html  css  js  c++  java
  • bzoj 3289 Mato的文件管理 树状数组+莫队

     Mato的文件管理

    Time Limit: 40 Sec  Memory Limit: 128 MB
    Submit: 4325  Solved: 1757
    [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]区间的文件。
    n,q <= 50000

    Output

    q行,每行一个正整数,表示Mato这天需要交换的次数。

    Sample Input

    4
    1 4 2 3
    2
    1 2
    2 4

    Sample Output

    0
    2
    //样例解释:第一天,Mato不需要交换
    第二天,Mato可以把2号交换2次移到最后。

    HINT

     

    题解:树状数组维护逆序对,然后莫队降低复杂度,要换绝对要换

     1 #include<cstring>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<iostream>
     5 #include<cmath>
     6 #include<queue>
     7 
     8 #define N 50007
     9 using namespace std;
    10 inline int read()
    11 {
    12     int x=0,f=1;char ch=getchar();
    13     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    14     while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    15     return x*f;
    16 }
    17 
    18 unsigned int now,ans[N];
    19 int n,m;
    20 int a[N],disc[N],belong[N],t[N];
    21 struct query
    22 {
    23     int l,r,id;
    24 }q[N];
    25 
    26 bool operator<(query a,query b)
    27 {
    28     if(belong[a.l]==belong[b.l])return a.r<b.r;
    29     return belong[a.l]<belong[b.l];
    30 }
    31 void add(int x,int val)
    32 {
    33     for(int i=x;i<=n;i+=i&-i)
    34         t[i]+=val;
    35 }
    36 unsigned int query(int x)
    37 {
    38     unsigned int sum=0;
    39     for(int i=x;i;i-=i&-i)
    40         sum+=t[i];
    41     return sum;
    42 }
    43 void solve()
    44 {
    45     sort(q+1,q+m+1);
    46     int l=1,r=0;
    47     for(int i=1;i<=m;i++)
    48     {
    49         while(l<q[i].l)
    50             add(a[l],-1),now-=query(a[l]-1),l++;
    51         while(r>q[i].r)
    52             add(a[r],-1),now-=r-l-query(a[r]),r--;
    53         while(l>q[i].l)
    54             l--,add(a[l],1),now+=query(a[l]-1);
    55         while(r<q[i].r)
    56             r++,add(a[r],1),now+=r-l+1-query(a[r]);
    57         ans[q[i].id]=now;
    58     }
    59 }
    60 int main()
    61 {
    62     n=read();int t=sqrt(n);
    63     for(int i=1;i<=n;i++)disc[i]=a[i]=read();
    64     sort(disc+1,disc+n+1);
    65     for(int i=1;i<=n;i++)a[i]=lower_bound(disc+1,disc+n+1,a[i])-disc;
    66     m=read();
    67     for(int i=1;i<=m;i++)
    68         q[i].l=read(),q[i].r=read(),q[i].id=i;
    69     for(int i=1;i<=n;i++)
    70         belong[i]=(i-1)/t+1;
    71     solve();
    72     for(int i=1;i<=m;i++)
    73         printf("%d
    ",ans[i]);
    74 }
  • 相关阅读:
    Ionic开发手机App常用的软件
    Windows的Ionic环境配置
    百度静态资源公共库
    我的程序员之路
    angular.bind
    响应式布局之使用bootstrap
    初识bootstrap
    使用css3实现响应式布局
    mysql-阅读笔记1
    mysql优化
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8847903.html
Copyright © 2011-2022 走看看