Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

线段树1700表示鸭梨很大!!!

Posted by wocha at 2012-08-17 23:08:28 on Problem 2299
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
#define int1  __int64
#define M 850000
using namespace std;
struct no{
	int1 left,right,s;
}tree[M*6];
struct kl{
	int1 x;
	int1 y;
}a[M];
bool cmp(kl s,kl d1){
	return s.x<d1.x;
}
void build(int1 l,int1 r,int1 i){
	tree[i].left=l;
	tree[i].right=r;
	tree[i].s=0;
	if(l==r) return;
	int1 mid=(l+r)>>1;
	build(l,mid,i<<1);
	build(mid+1,r,i<<1|1);
}
void insert(int1 l,int1 r,int1 i){
	if(tree[i].left==l&&tree[i].right==r){
		tree[i].s++;
		return;
	}
	int1 mid=(tree[i].left+tree[i].right)>>1;
	if(r<=mid)  insert(l,r,i<<1);
	else if(l>mid) insert(l,r,i<<1|1);
	else  insert(l,mid,i<<1),insert(mid+1,r,i<<1|1);
	tree[i].s=tree[i<<1].s+tree[i<<1|1].s;
}
int ans;
void add(int1 l,int1 r,int1 i){
	if(tree[i].left>=l&&tree[i].right<=r){
		 ans+=tree[i].s;
		 return;
	}
	if(tree[i].left==tree[i].right)  return;
	int1 mid=(tree[i].left+tree[i].right)>>1;
	if(r<=mid)   add(l,r,i<<1);
	else if(l>mid)    add(l,r,i<<1|1);
	else add(l,mid,i<<1),add(mid+1,r,i<<1|1);
  }
int1 sorted[M];
int	  main(){
	int1 n;
	while(cin>>n,n){
	for(int1 i=1;i<=n;i++){
		scanf("%I64d",&a[i].x);
		a[i].y=i;
	}
	sort(a+1,a+1+n,cmp);
	int1 u=1;sorted[0]=1;a[n+1].y=-5455;
	for(int1 i=1;i<=n;i++){
		if(a[i].x!=a[i+1].x){
		sorted[a[i].y]=(u++);
		}
		else {
			 sorted[a[i].y]=u;
		}	
	}
    build(1,n,1);
    int1 sum=0;
    for(int1 i=1;i<=n;i++){
    	ans=0;
    	add(1,sorted[i],1);
    	sum+=(i-ans-1);
    	insert(sorted[i],sorted[i],1);
    }
    printf("%I64d\n",sum);
	}
	return 0;
	
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator