zoukankan      html  css  js  c++  java
  • bzoj3289: Mato的文件管理

    分块+树状数组求逆序对。复杂度O((n+m)sqrt(n)logn)。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    #define REP(i,s,t) for(int i=s;i<=t;i++)
    #define dwn(i,s,t) for(int i=s;i>=t;i--)
    #define clr(x,c) memset(x,c,sizeof(x))
    int read(){
    	int x=0;char c=getchar();bool f=true;
    	while(!isdigit(c)){
    		if(c=='-') f=false;c=getchar();
    	}
    	while(isdigit(c)) x=x*10+c-'0',c=getchar();
    	return f?x:-x;
    }
    const int nmax=50005;
    int n,m,a[nmax],b[nmax],res[nmax],t[nmax],bg[nmax];
    struct node{
    	int l,r,num;
    	bool operator<(const node&rhs)const{
    	  return bg[l]<bg[rhs.l]||bg[l]==bg[rhs.l]&&r<rhs.r;}
    };
    node nodes[nmax];
    int lowbit(int x){
    	return x&(-x);
    }
    void update(int x,int add){
    	for(int i=x;i<=n;i+=lowbit(i)) t[i]+=add;
    }
    int gs(int x){
    	int ans=0;
    	for(int i=x;i;i-=lowbit(i)) ans+=t[i];
    	return ans;
    }
    int main(){
    	n=read();
    	REP(i,1,n) a[i]=b[i]=read();
    	sort(b+1,b+n+1);
    	REP(i,1,n) a[i]=lower_bound(b+1,b+n+1,a[i])-b;
    	int block=(int)sqrt(n);
    	REP(i,1,n) bg[i]=(i-1)/block+1;
    	
    	m=read();
    	REP(i,1,m) nodes[i].l=read(),nodes[i].r=read(),nodes[i].num=i;
    	sort(nodes+1,nodes+m+1);
    	int ans=0,l=1,r=0;
    	REP(i,1,m){
    		node o=nodes[i];
    		REP(j,l,o.l-1) ans-=gs(a[j]-1),update(a[j],-1);
    		dwn(j,l-1,o.l) ans+=gs(a[j]-1),update(a[j],1);
    		REP(j,r+1,o.r) ans+=gs(n)-gs(a[j]),update(a[j],1);
    		dwn(j,r,o.r+1) ans-=gs(n)-gs(a[j]),update(a[j],-1);
    		l=o.l;r=o.r;
    		res[o.num]=ans;
    	}
    	REP(i,1,m) printf("%d
    ",res[i]);
    	return 0;
    }
    

      

    3289: Mato的文件管理

    Time Limit: 40 Sec  Memory Limit: 128 MB
    Submit: 1973  Solved: 819
    [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

    [Submit][Status][Discuss]
  • 相关阅读:
    Leetcode 238. Product of Array Except Self
    来博客园的第一天
    [LeetCode] 1020. Number of Enclaves
    [LeetCode] 921. Minimum Add to Make Parentheses Valid
    [LeetCode] 1541. Minimum Insertions to Balance a Parentheses String
    [LeetCode] 738. Monotone Increasing Digits
    [LeetCode] 1669. Merge In Between Linked Lists
    [LeetCode] 865. Smallest Subtree with all the Deepest Nodes
    [LeetCode] 376. Wiggle Subsequence
    [LeetCode] 1170. Compare Strings by Frequency of the Smallest Character
  • 原文地址:https://www.cnblogs.com/fighting-to-the-end/p/5711665.html
Copyright © 2011-2022 走看看