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

用树状数组,符合题意的编了,为什么总是WA

Posted by lixiong at 2011-08-07 22:02:32
#include<iostream>
#include<algorithm>
using namespace std;

__int64 total[10100];
long s[10100];
int lowbit(int x)
{
	return x&-x;
}
void  sum(int x,long su, __int64 b[])
{
	int i;
	for(i=x+1;i<=10000;i+=lowbit(i))
	{
		b[i]+=su;
	}
}
void  add(int x,__int64 b[],__int64 &fore)
{
	int i;
	fore=0;
	for(i=x;i>0;i-=lowbit(i))
	{
		fore+=b[i];
	}	
}

int main()
{
	int n;
	int i;
	__int64  fore,all=0,count=0;
	cin>>n;
	memset(total,sizeof(total),0);
	memset(s,sizeof(s),0);
	for( i=0;i<n;i++)
    cin>>s[i];
	sort(s,s+n);
	for(i=0;i<n;i++)
	{
		sum(i,s[i],total);
		
	}
	for(i=10000;i>0;i-=lowbit(i))
	{
		all+=total[i];
	}
	for(i=0;i<n;i++)
	{
		add(i,total,fore);  
		count+=s[i]*i-fore;
		count+=(all-fore-s[i])-s[i]*(n-1-i);	
	}
	printf("%I64d\n",count);
	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