You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
typedef long long LL ;
const int N = 20010 ;
// 听力值v,坐标x
// 结构体+第一维、第二维由小到大排序
struct Node {
int v , x ;
const bool operator < ( const Node & t ) const {
if ( v = = t . v ) return x < t . x ;
return v < t . v ;
}
} a [ N ] ;
// 树状数组模板
int c1 [ N ] , c2 [ N ] ;
# define lowbit(x) (x & -x)
void add ( int c [ ] , int x , int d ) {
for ( int i = x ; i < N ; i + = lowbit ( i ) ) c [ i ] + = d ;
}
LL sum ( int c [ ] , int x ) {
LL res = 0 ;
for ( int i = x ; i ; i - = lowbit ( i ) ) res + = c [ i ] ;
return res ;
}
int main ( ) {
# ifndef ONLINE_JUDGE
freopen ( " P2345.in " , " r " , stdin ) ;
# endif
int n ;
scanf ( " %d " , & n ) ;
for ( int i = 1 ; i < = n ; i + + ) scanf ( " %d %d " , & a [ i ] . v , & a [ i ] . x ) ;
sort ( a + 1 , a + n + 1 ) ; // 排序
LL res = 0 ;
for ( int i = 1 ; i < = n ; i + + ) {
// c1:坐标和树状数组
// c2:牛的个数
LL s1 = sum ( c1 , a [ i ] . x - 1 ) ; // a[i]进入树状数组时,它前面所有牛的坐标和
LL s2 = sum ( c1 , 20000 ) - sum ( c1 , a [ i ] . x ) ; // a[i]进入树状数组时,它后面所有牛的坐标和
LL cnt = sum ( c2 , a [ i ] . x ) ;
/*
cnt:它之前牛的个数
i-1-cnt:它之后牛的个数
a[i].x: 它的坐标
*/
// 方法1: cout << "i=" << i << ",sum(c2,N)=" << sum(c2, N) << endl;
res + = a [ i ] . v * ( cnt * a [ i ] . x - s1 + s2 - ( sum ( c2 , N ) - cnt ) * a [ i ] . x ) ;
// 方法2:
// res += a[i].v * (cnt * a[i].x - s1 + s2 - (i - 1 - cnt) * a[i].x);
add ( c1 , a [ i ] . x , a [ i ] . x ) ; // 维护坐标前缀和
add ( c2 , a [ i ] . x , 1 ) ; // 维护个数前缀和
}
// 输出结果
printf ( " %lld \n " , res ) ;
return 0 ;
}