BZOJ 3289 Mato的文件管理(莫队+离散化求逆序数)

3289: Mato的文件管理

Time Limit: 40 Sec  Memory Limit: 128 MB
Submit: 2171  Solved: 891
[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次移到最后。

 

 

题目链接:BZOJ 3289

囧啊排序规则里把==打成了!=从莫队式暴力变成了n*n爆炸狂T(已经不止一次打错了),还以为是数据量的锅加了个外挂还是T,TN次之后发现了这个错误……

离散化一下就行了喔最近用了大牛那学来的vector方式的离散化真是方便嘿嘿

有一个小优化,求getsum(N)的时候其实大部分的值都是0,用区间代替是一样的,比如5 4 3 2 向右增加X,此时逆序数会增加getsumsum[x+1~N]即先add(x,1)再计算getsum(N)-getsum(X),那这里的getsum(N)求的就是1~N的所有数,因为更新的本来就只是当前区间的数,那不如直接R-L+1效果一样,把+1用add实现也行,顺序换一下即可

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=50010;
struct info
{
    int l,r;
    int b,id;
    bool operator<(const info &t)const
    {
        if(b==t.b)
            return r<t.r;
        return b<t.b;
    }
}Q[N];
int arr[N],c[N],ans[N];
vector<int>pos;
void add(int k,int val)
{
    while (k<N)
    {
        c[k]+=val;
        k+=(k&-k);
    }
}
int getsum(int k)
{
    int ret=0;
    while (k)
    {
        ret+=c[k];
        k-=(k&-k);
    }
    return ret;
}

int main(void)
{
    int n,i,q;
    while (~scanf("%d",&n))
    {
        CLR(c,0);
        pos.clear();
        for (i=1; i<=n; ++i)
        {
            scanf("%d",&arr[i]);
            pos.push_back(arr[i]);
        }

        sort(pos.begin(),pos.end());
        pos.erase(unique(pos.begin(),pos.end()),pos.end());
        for (i=1; i<=n; ++i)
            arr[i]=lower_bound(pos.begin(),pos.end(),arr[i])-pos.begin()+1;

        scanf("%d",&q);
        int unit=(int)sqrt(n);
        for (i=0; i<q; ++i)
        {
            scanf("%d%d",&Q[i].l,&Q[i].r);
            Q[i].id=i;
            Q[i].b=Q[i].l/unit;
        }
        sort(Q,Q+q);
        int L=1,R=0,Ans=0;
        for (i=0; i<q; ++i)
        {
            while (L>Q[i].l)
            {
                --L;
                Ans+=getsum(arr[L]-1);
                add(arr[L],1);
            }
            while (L<Q[i].l)
            {
                Ans-=getsum(arr[L]-1);
                add(arr[L],-1);
                ++L;
            }
            while (R>Q[i].r)
            {
                Ans-=(R-L+1-getsum(arr[R]));
                add(arr[R],-1);
                --R;
            }
            while (R<Q[i].r)
            {
                ++R;
                Ans+=(R-L-getsum(arr[R]));
                add(arr[R],1);
            }
            ans[Q[i].id]=Ans;
        }
        for (i=0; i<q; ++i)
            printf("%d\n",ans[i]);
    }
    return 0;
}
posted @ 2016-09-16 22:32  Blackops  阅读(318)  评论(0编辑  收藏  举报