-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtoi07_pair.cpp
78 lines (64 loc) · 1.13 KB
/
toi07_pair.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define MOD 1e9 + 7
vector<long long> f(100001),e(100001);
long long read(int idx)
{
long long val = 0;
while(idx>0)
{
val+=f[idx];
idx-=(idx & -idx);
}
return val;
}
void update(int idx,int val)
{
while(idx<=100000)
{
f[idx]+=val;
idx+=(idx & -idx);
}
}
long long read1(int idx)
{
long long val = 0;
while(idx>0)
{
val+=e[idx];
idx-=(idx & -idx);
}
return val;
}
void update1(int idx)
{
while(idx<=100000)
{
e[idx]++;
idx+=(idx & -idx);
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
vector<pair<int,int> > a(n);
for(int i = 0;i < n;i++)
{
int x,y;
cin >> x >> y;
a[i] = {x,y};
}
sort(a.begin(),a.end());
long long ans = 0;
for(int i = 0;i < n;i++)
{
ans+=read(100000)-read(a[i].second);
ans+=(a[i].first*(read1(100000)-read1(a[i].second)));
update(a[i].second,a[i].first);
update1(a[i].second);
}
cout << ans;
}