bzoj 3289: Mato的文件管理 莫队+树状数组

3289: Mato的文件管理

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

 

Source

By taorunz

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3289

 

题意:一个序列,n个数,q个询问,求区间逆序对;

思路:莫队+树状数组;

   离线更新答案,树状数组求逆序对的个数即可

   注意一下更新的时候前面删除和后面删除的不一样的答案;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=5e4+10,M=4e6+10,inf=2147483647;
const ll INF=1e18+10,mod=1e9+7;

///   数组大小
struct AYT
{
    int tree[N];
    int lowbit(int x)
    {
        return x&-x;
    }
    void update(int x,int c)
    {
        while(x<N)
        {
            tree[x]+=c;
            x+=lowbit(x);
        }
    }
    int query(int x)
    {
        int ans=0;
        while(x)
        {
            ans+=tree[x];
            x-=lowbit(x);
        }
        return ans;
    }
};
AYT tree;
int n,pos[N],k,a[N],b[N];
struct is
{
    int l,r,now;
    bool operator <(const is &b)const
    {
        if(pos[l]!=pos[b.l])
        return pos[l]<pos[b.l];
        return r<b.r;
    }
}p[N];
ll out[N],ans;
int getpos(int x)
{
    int pos=lower_bound(b+1,b+1+n,x)-b;
    return pos;
}
void addp(int x)
{
    int z=getpos(a[x]);
    ans+=tree.query(z-1);
    tree.update(z,1);
}
void addn(int x)
{
    int z=getpos(a[x]);
    ans+=tree.query(N-5)-tree.query(z);
    tree.update(z,1);
}
void delp(int x)
{
    int z=getpos(a[x]);
    ans-=tree.query(z-1);
    tree.update(z,-1);
}
void deln(int x)
{
    int z=getpos(a[x]);
    ans-=tree.query(N-5)-tree.query(z);
    tree.update(z,-1);
}
int main()
{
    scanf("%d",&n);
    k=sqrt(n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]),pos[i]=(i-1)/k+1,b[i]=a[i];
    sort(b+1,b+n+1);
    int q;
    scanf("%d",&q);
    for(int i=1;i<=q;i++)
        scanf("%d%d",&p[i].l,&p[i].r),p[i].now=i;
    sort(p+1,p+1+q);
    int L=1,R=0;
    for(int i=1;i<=q;i++)
    {
        while(L<p[i].l)
        {
            delp(L);
            L++;
        }
        while(L>p[i].l)
        {
            L--;
            addp(L);
        }
        while(R>p[i].r)
        {
            deln(R);
            R--;
        }
        while(R<p[i].r)
        {
            R++;
            addn(R);
        }
        out[p[i].now]=ans;
    }
    for(int i=1;i<=q;i++)
        printf("%lld\n",out[i]);
    return 0;
}

 

posted @ 2017-04-10 12:05  jhz033  阅读(178)  评论(0编辑  收藏  举报