Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<algorithm>
- using namespace std;
- typedef long long int ll;
- typedef pair<ll,ll> pll;
- bool mycmp(pll a,pll b){
- return a.second < b.second;
- }
- double fenwick[100010];
- double fenwickcnt[100010];
- int cnt = 0;
- double query(int x)
- {
- double sum = 0;
- for(int i = x ; i >= 1 ; i -= (i&-i)){
- // printf("%d\n",i);
- cnt += fenwickcnt[i];//cnt++;
- sum += fenwick[i];
- }
- return sum;
- }
- void update(int x){
- for(int i = x ; i <= 100000 ; i += (i&-i)){
- // printf("%d\n",i);
- fenwick[i] += x;
- fenwickcnt[i]++;
- }
- }
- int main()
- {
- int n;
- scanf("%d",&n);
- pll all[n];
- for(int i = 0 ; i < n ; i ++)
- {
- scanf("%lld %lld",&all[i].first,&all[i].second);
- }
- sort(all,all+n,mycmp);
- double sum = 0;
- for(int i = n - 1 ; i >= 0 ; i --){
- cnt = 0;
- // printf("now (%lld,%lld)\n",all[i].first,all[i].second);
- double q = query(all[i].first-1);
- double add = (cnt*all[i].first);
- // printf("sum += (%.0lf + %.0lf) = %.0lf\n",q,add,q + add);
- sum += q + add;
- // printf("sum = %.0lf\n",sum);
- update(all[i].first);
- }
- printf("%.0lf",sum);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement