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

线段树+离散化,800+MS

Posted by Panamera at 2014-04-10 16:51:19 on Problem 2299
#include<stdio.h>
#include<algorithm>
#define MAX_N 500090
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
long long tree[MAX_N<<2];
int arr[MAX_N],a[MAX_N];
void build(int l,int r,int rt)
{
    tree[rt] = 0 ;
    if(l==r)
        return ;
    int m = l + ( r - l ) / 2 ;
    build(lson);
    build(rson);
}
long long query(int val,int l,int r,int rt)
{
    ++tree[rt];
    if(l==r)
        return 0;
    int m = l + ( r - l ) / 2 ;
    return val <= a[m] ? query(val,lson)+tree[rt<<1|1] : query(val,rson);
}
int main()
{
    int n ;
    while(scanf("%d",&n) && n)
    {
        for(int i=0;i<n;++i)
        {
            scanf("%d",arr+i);
            a[i] = arr[i];
        }
        sort(a,a+n);
        int len = unique(a,a+n) - a ;
        long long res = 0 ;
        for(int i=0;i<n;++i)
            res +=query(arr[i],0,len-1,1);
        printf("%lld\n",res);
        build(0,len-1,1);
    }
    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