一个树状数组的模版题,点对距离,没什么意思,前段时间做过一个类似的,没想到是这么普遍的一个题..
记一个事情.
树状数组从1开始,别用0犯贱了.
加难版兄弟题
这题真的很随意,随便谢谢就过了.
/* xzppp */
#include <iostream>
#include <vector>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <stack>
#include <iomanip>
using namespace std;
#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define PB push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int > pii;
typedef pair<double,double > pdd;
typedef pair<double,int > pdi;
const int MAXN = 20000+17;
const int MAXM = 20;
const int MAXV = 2*1e3+17;
const int INF = 0x7fffffff;
const int MOD = 1e9+7;
int bit[2][MAXN],n;
void add(int k,int p,int x)
{
while(p<=n)
{
bit[k][p] += x;
p += p&-p;
}
}
int sum(int k,int p)
{
int res = 0;
while(p>0)
{
res += bit[k][p];
p -= p&-p;
}
return res;
}
struct dap
{
int num,p,id;
dap(int a,int b,int c)
{
num = a;
p = b;
id = c;
}
dap(){}
};
bool cmp1(dap x,dap y)
{
return x.p<y.p;
}
bool cmp2(dap x,dap y)
{
return x.num<y.num;
}
vector<dap > v;
int main()
{
#ifndef ONLINE_JUDGE
FFF
#endif
cin>>n;
for (int i = 0; i < n; ++i)
{
int a,b;
scanf("%d%d",&a,&b);
v.PB(dap(a,b,i));
}
sort(v.begin(), v.end(),cmp1);
for (int i = 0; i < v.size(); ++i)
v[i].id = i+1;
sort(v.begin(), v.end(),cmp2);
LL ans = 0;
for (int i = 0; i < v.size(); ++i)
{
int id = v[i].id;
int s1,s2,c1,c2;
s1 = sum(1,id-1);
s2 = sum(1,n)-sum(1,id);
c1 = sum(0,id-1);
c2 = sum(0,n)-sum(0,id);
ans += 1LL*(s2-s1+(c1-c2)*v[i].p)*v[i].num;
add(0,id,1);
add(1,id,v[i].p);
}
cout<<ans<<endl;
return 0;
}