6
12
2014
1

3289: Mato的文件管理

题面:

3289: Mato的文件管理

Time Limit: 40 Sec  Memory Limit: 128 MB
Submit: 304  Solved: 123
[Submit][Status]

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√n)的题……

莫队+树状数组维护逆序对……

莫队不解释了……

转移时树状数组维护逆序对,log n……

时间效率O(n√n*log n)……

n=50000居然过了……

代码:

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstdlib>
#include <set>
using namespace std;
typedef long long ll;
struct czf{int l,r,id;}wk[500005];
int data[500005],a[500005],b,c[500005],d,e,g,i,j,k,l,m,n,r,cnt,X,f[500005];
ll ans[500005],h;
inline int maxx(const int &a,const int &b){if (a>b)return a;return b;}
inline int minn(const int &a,const int &b){if (a<b)return a;return b;}
inline void add(int x,int s){for(int i=x;i<=cnt;i+=(i&(-i)))f[i]+=s;}
inline int sum(int x){int s=0;for(int i=x;i;i-=(i&(-i)))s+=f[i];return s;}
inline int ef(int x){int l=1,r=cnt,mid;while (l<r){mid=(l+r+1)/2;if (c[mid]>x)r=mid-1;else l=mid;}return l;}
inline bool cmp(czf a,czf b){if (a.l/X!=b.l/X) return a.l<b.l;else if ((a.l/X)&1)return a.r<b.r;else return a.r>b.r;}
void read(int &x){
    char ch;
    for(ch=getchar();ch<'0'||ch>'9';ch=getchar());
    for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
}
int main(){
    read(n);
    X=(int)sqrt(n);
    for(i=1;i<=n;i++)read(a[i]);
    for(i=1;i<=n;i++)c[i]=a[i];
    sort(c+1,c+1+n);
    for(i=1;i<=n;i++)if (c[i]!=c[i+1])c[++cnt]=c[i];
    for(i=n;i;i--){
        data[i]=ef(a[i]);
    }
    read(m);
    for(i=1;i<=m;i++){
        read(wk[i].l);read(wk[i].r);
        wk[i].id=i;
    }
    sort(wk+1,wk+1+m,cmp);
    l=1;r=1;h=0;add(data[1],1);
    for(i=1;i<=m;i++){
        int wr=wk[i].r,wl=wk[i].l;
        while (r<wr){
            r++;
            h+=ll((r-l)-sum(data[r]));
            add(data[r],1);
        }
        while (r>wr){
            h=h-ll((r-l)-sum(data[r]))-1LL;
            add(data[r],-1);
            r--;
        }
        while (l<wl){
            h=h-ll(sum(data[l]-1));
            add(data[l],-1);
            l++;
        }
        while (l>wl){
            l--;
            h+=ll(sum(data[l]-1));
            add(data[l],1);
        }
        ans[wk[i].id]=h;
    }
    for(i=1;i<=m;i++)printf("%lld\n",ans[i]);
    return 0;
}
Category: BZOJ | Tags: | Read Count: 896
Raymond 说:
2020年3月21日 16:15

Yes, the improvement of the goal is defined for the elements for the fitness for humans. The things of the use of the british essay writers reviews are punctuated for the fitness for the serious looks for the field. Improvement of the joy is done for humans. the ting is issued for humans. nature is defined for the help of the elements and elevation for the things.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com