Advertisement
SuitNdtie

Pair

May 14th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<algorithm>
  3. using namespace std;
  4. typedef long long int ll;
  5. typedef pair<ll,ll> pll;
  6.  
  7. bool mycmp(pll a,pll b){
  8.     return a.second < b.second;
  9. }
  10.  
  11. double fenwick[100010];
  12. double fenwickcnt[100010];
  13. int cnt = 0;
  14.  
  15. double query(int x)
  16. {
  17.     double sum = 0;
  18.     for(int i = x ; i >= 1 ; i -= (i&-i)){
  19.     //  printf("%d\n",i);
  20.         cnt += fenwickcnt[i];//cnt++;
  21.         sum += fenwick[i];
  22.     }
  23.     return sum;
  24. }
  25.  
  26. void update(int x){
  27.     for(int i = x ; i <= 100000 ; i += (i&-i)){
  28.     //  printf("%d\n",i);
  29.         fenwick[i] += x;
  30.         fenwickcnt[i]++;
  31.     }
  32. }
  33.  
  34. int main()
  35. {
  36.     int n;
  37.     scanf("%d",&n);
  38.     pll all[n];
  39.     for(int i = 0 ; i < n ; i ++)
  40.     {
  41.         scanf("%lld %lld",&all[i].first,&all[i].second);
  42.     }
  43.     sort(all,all+n,mycmp);
  44.     double sum = 0;
  45.     for(int i = n - 1 ; i >= 0 ; i --){
  46.         cnt = 0;
  47.     //  printf("now (%lld,%lld)\n",all[i].first,all[i].second);
  48.         double q = query(all[i].first-1);
  49.         double add = (cnt*all[i].first);
  50.     //  printf("sum += (%.0lf + %.0lf) = %.0lf\n",q,add,q + add);
  51.         sum += q + add;
  52.     //  printf("sum = %.0lf\n",sum);
  53.         update(all[i].first);
  54.     }
  55.     printf("%.0lf",sum);
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement