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

    3289: 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次移到最后。

    Source

    By taorunz

    这道题求区间值,显然O(n^2)是过不了的于是我们就想到了莫队

    我们可以用树状数组来解决逆序对,从而求出交换次数。在O(logn)的时间内知道附近区间的答案。

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<cstdlib>
     7 #include<vector>
     8 using namespace std;
     9 typedef long long ll;
    10 typedef long double ld;
    11 typedef pair<int,int> pr;
    12 const double pi=acos(-1);
    13 #define rep(i,a,n) for(int i=a;i<=n;i++)
    14 #define per(i,n,a) for(int i=n;i>=a;i--)
    15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
    16 #define clr(a) memset(a,0,sizeof(a))
    17 #define pb push_back
    18 #define mp make_pair
    19 #define fi first
    20 #define sc second
    21 #define pq priority_queue
    22 #define pqb priority_queue <int, vector<int>, less<int> >
    23 #define pqs priority_queue <int, vector<int>, greater<int> >
    24 #define vec vector
    25 ld eps=1e-9;
    26 ll pp=1000000007;
    27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
    28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
    29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
    30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
    31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
    32 ll read(){ ll ans=0; char last=' ',ch=getchar();
    33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
    34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    35 if(last=='-')ans=-ans; return ans;
    36 }
    37 #define N 50005
    38 #define lowbit(i) (i&(-i))
    39 int n,num,ans,T[N],pos[N];
    40 struct node_{
    41     int i,id;
    42 }nu[N];
    43 struct node{
    44     int l,r,id,ans;
    45 }a[N];
    46 int cmp_nu(node_ a,node_ b){
    47     return a.i<b.i;
    48 }
    49 int cmp_nu_(node_ a,node_ b){
    50     return a.id<b.id;
    51 }
    52 int cmp (node a,node b){
    53     return pos[a.l]<pos[b.l]||pos[a.l]==pos[b.l]&&a.r<b.r;
    54 } 
    55 int cmp_(node a,node b){
    56     return a.id<b.id;
    57 }
    58 void add(int x,int v){
    59     num=v>0?num+1:num-1;
    60     for (int i=x;i<=n;i+=lowbit(i)) T[i]+=v;
    61 }
    62 int sum(int x){
    63     int Sum=0;
    64     for (int i=x;i>0;i-=lowbit(i)) Sum+=T[i];
    65     return Sum;
    66 }
    67 void Update(int x,int v,int f){
    68     ans+=(f?(num-sum(nu[x].i)):(sum(nu[x].i-1)))*v; add(nu[x].i,v); 
    69 }
    70 int main()
    71 {
    72     n=read();
    73     for (int i=1;i<=n;i++) nu[i].i=read(),nu[i].id=i;
    74     sort(nu+1,nu+n+1,cmp_nu);
    75     for (int i=1,e=0;i<=n;i++) 
    76         if (nu[i].i!=nu[i-1].i) nu[i].i=++e;
    77         else nu[i].i=e; 
    78     sort(nu+1,nu+n+1,cmp_nu_);
    79     int q=read();
    80     for (int i=1;i<=q;i++) a[i].l=read(),a[i].r=read(),a[i].id=i;
    81     int block=(int)(sqrt(n));
    82     for (int i=1;i<=n;i++)
    83         pos[i]=(i-1)/block+1;
    84     sort(a+1,a+q+1,cmp); 
    85     for (int i=1,l=1,r=0;i<=q;i++){
    86         for (;r<a[i].r;r++) Update(r+1,1,1);
    87         for (;r>a[i].r;r--) Update(r,-1,1);
    88         for (;l>a[i].l;l--) Update(l-1,1,0);
    89         for (;l<a[i].l;l++) Update(l,-1,0);
    90         a[i].ans=ans;
    91     }
    92     sort(a+1,a+q+1,cmp_);
    93     for (int i=1;i<=q;i++)
    94         printf("%d
    ",a[i].ans);
    95     return 0;
    96  }  
    View Code
  • 相关阅读:
    Saltstack module apache 详解
    Saltstack module ip 详解
    Saltstack module iosconfig 详解
    Saltstack module introspect 详解
    Saltstack module inspector 详解
    Saltstack module ini 详解
    Saltstack module incron 详解
    Modbus 指令 RS485指令规则
    停车系统对接第三方在线支付平台(二)
    停车系统对接第三方在线支付平台
  • 原文地址:https://www.cnblogs.com/SXia/p/6785250.html
Copyright © 2011-2022 走看看