• POJ 2299Ultra-QuickSort


    题意:线段树求逆序对经典题目,需要离散处理,但是用stl处理的话会T,手动二分处理即可;

    #include<algorithm>
    #include<iostream>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<cstring>
    #include<cstdio>
    #define N 500005
    using namespace std;
    typedef struct node{
    	int x;int y;int date;
    }node;
    bool cmp(int a,int b){
    	return a>b;
    }
    node a[4*N];
    void built(int root,int first,int end){
    	if(first==end){
    		a[root].x=first;a[root].y=end;a[root].date=0;
    		return ;
    	}
    	int mid=(first+end)/2;
    	built(root*2,first,mid);
    	built(root*2+1,mid+1,end);
    	a[root].x=a[root*2].x;a[root].y=a[root*2+1].y;a[root].date=0;
    }
    void U(int root,int first,int end,int e){
    	if(first==end){
    		a[root].date++;
    		return ;
    	}
    	int mid=(first+end)/2;
    	if(e<=mid)  U(root*2,first,mid,e);
    	else U(root*2+1,mid+1,end,e);
    	a[root].date=a[root*2].date+a[root*2+1].date;
    }
    long long sum=0;
    void Q(int root,int first,int end,int l,int r){
    	if(l<=first&&end<=r){
    		sum+=a[root].date;
    		return ;
    	}
    	int mid=(first+end)/2;
    	if(l<=mid)  Q(root*2,first,mid,l,r);
    	if(r>mid)  Q(root*2+1,mid+1,end,l,r);
    }
    long long b[N];
    long long c[N];
    long long e[N];
    long long p[N];
    int main(){
    	int m;
    	while(scanf("%d",&m)==1&&m!=0){
    		for(int i=1;i<=m;i++){
    			scanf("%d",&b[i]);
    			p[i]=b[i];
    		}
    		sort(b+1,b+m+1,cmp);
    		e[1]=b[1];
    		int k=1;
    		for(int i=2;i<=m;i++){
    			if(b[i]!=b[i-1]){
    				k++;
    				e[k]=b[i];
    			}
    		}
    		for(int i=1;i<=m;i++){
    			int first=1;int end=k;
    			while(first<=end){
    				int mid=(first+end)/2;
    				if(p[i]==e[mid]){
    					c[i]=mid;
    					break;
    				}
    				else if(p[i]<e[mid]){
    					first=mid+1;
    				}
    				else{
    					end=mid-1;
    				}
    			}
    		}
    		built(1,1,k);
    		long long ans=0;
    	  for(int i=1;i<=m;i++){
    	  	sum=0;
    	  	if(c[i]==1){
    	  		U(1,1,k,c[i]);
    		  }
    		  else{
    	  	Q(1,1,k,1,c[i]-1);
    	  	ans+=sum;
    	  	U(1,1,k,c[i]);}
    	  }
    	  printf("%lld
    ",ans);
    	}
    	return 0;
    }


  • 相关阅读:
    在javaWeb 工程中 tomcat 的 web.xml 文件配置
    Spring 框架详解
    构建工具 Maven和Gradle对比
    idea 创建Javaweb 动态工程
    服务器后端 项目代码常用目录图
    ET模式下的EPOLLOUT
    linux recv 参数len设置为0
    fork 和 exec 对子进程继承父进程处理信号处理函数的影响
    lua元表以及元方法
    Linux网络编程“惊群”问题总结
  • 原文地址:https://www.cnblogs.com/wang9897/p/7624402.html
Copyright © 2020-2023  润新知