#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define N 20003
using namespace std;
int tree[N+2],n,A[N+2];
struct pai
{
int v,x;
};
pai all[N];
int cmp(const void *a,const void *b)
{
return (*(pai *)a).v - (*(pai *)b).v;
}
int lowbit(int k)
{
return k&(-k);
}
void Modify(int num,int v)
{//两个树状数组,赛用不同的函数。本可以合成一个写的,人懒。
while (num < N)
{
tree[num] += v;
num += lowbit(num);
}
}
void Modify2(int num)
{
while(num < N)
{
A[num] ++;
num += lowbit(num);
}
}
int sum(int num)
{
int ans = 0;
while(num > 0)
{
ans += tree[num];
num -= lowbit(num);
}
return ans;
}
int sum2(int num)
{
int ans = 0;
while (num > 0)
{
ans += A[num];
num -= lowbit(num);
}
return ans;
}
int main()
{
int i,j,m,k,t,a,b,c,d;
__int64 ans;
while (scanf("%d",&n)!=EOF)
{
memset(A,0,sizeof(A));
memset(tree,0,sizeof(tree));
for(i = 0;i < n;i++)
{
scanf("%d%d",&all[i].v,&all[i].x);
}
qsort(all,n,sizeof(all[0]),cmp);//按音量排序,题中提到采用大的音量
//所以这里要从小到大排序
ans = 0;
Modify(all[0].x,all[0].x);
Modify2(all[0].x);
for(i = 1;i < n;i++)
{
a = sum(N);
b = sum(all[i].x);
c = sum2(N);
d = sum2(all[i].x);//分开计算便于理解
ans += (__int64)(d*all[i].x-b)*(__int64)all[i].v;//必须强制转化
ans += (__int64)(a-b-(c-d)*all[i].x)*(__int64)all[i].v;//否则出错
Modify(all[i].x,all[i].x);
Modify2(all[i].x);
}
printf("%I64d\n",ans);
}
return 0;
}
poj 1990
最新推荐文章于 2020-03-29 11:41:41 发布