Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef long double ld;
  7. typedef vector<int> vi;
  8. typedef vector<ll> vl;
  9. typedef pair<int, int> pii;
  10. typedef pair<ll, ll> pll;
  11. typedef priority_queue<int> pq;
  12. typedef priority_queue<int, vi, greater<int>> mpq;
  13.  
  14. const ll INF = 1e18;
  15. const int mod = 1e9+7;
  16. const int dX[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  17.  
  18. #define f first
  19. #define s second
  20. #define pb push_back
  21. #define mp make_pair
  22. #define ft front()
  23. #define bk back()
  24. #define sz(x) (int)x.size()
  25. #define all(x) (x).begin(), (x).end()
  26. #define FOR(i, a) for(int i = 0; i < a; i++)
  27. #define FoR(i, a) for(int i = 1; i <= a; i++)
  28. #define ROF(i, a) for (int i = (a)-1; i >= 0; i--)
  29.  
  30. void readIn(string fileName) { freopen(fileName.c_str(), "r", stdin); }
  31. void writeOut(string fileName) { freopen(fileName.c_str(), "w", stdout); }
  32. void syncIO() { ios_base::sync_with_stdio(0); cin.tie(0); }
  33. void setIO(string str = ""){ syncIO(); if(sz(str)) { readIn(str+".in"), writeOut(str+".out"); }}
  34.  
  35. int n; int arr[100001];
  36. int numInversions[100001];
  37. int bit[100001];
  38.  
  39. void update(int x, int y){
  40.     for(;x <= n+1;x += x&-x){
  41.         bit[x] += y;
  42.     }
  43. }
  44.  
  45. int query(int x){
  46.     int sum = 0;
  47.     for(;x > 0; x -= x&-x){
  48.         sum += bit[x];
  49.     }
  50.     return sum;
  51. }
  52.  
  53. int main(){
  54.     syncIO;
  55.     /* string fileName = "haircut"; setIO(fileName); */
  56. //-------------------------------------------------
  57.     cin >> n;
  58.     for(int i = 0; i < n; i++){
  59.         cin >> arr[i];
  60.         arr[i]++;
  61.     }
  62.     for(int i = 0; i < n; i++){
  63.         int curr = arr[i];
  64.         //now we want to find the number uf inversions for this element
  65.         numInversions[curr] += (query(n+1) - query(curr));
  66.         /* cout << " numInv " << query(n+1) << " " << query(curr) << endl; */
  67.         update(curr, 1);
  68.     }
  69.     //now we create the psums in the numInversions array
  70.     for(int i = 0; i <= n+1; i++){
  71.         cout << numInversions[i] << " ";
  72.     }
  73.     cout << endl;
  74.     for(int i = 1; i <= n + 1; i++){
  75.         numInversions[i] += numInversions[i-1];
  76.     }
  77.     for(int i = 0; i < n; i++){
  78.         cout << numInversions[i] << endl;
  79.     }
  80.    
  81.     return 0;
  82.     // Read notes to self
  83. }
  84.  
  85. /* NOTES TO SELF:
  86.  * Check fur int vs ll, worst case just switch everything to ll
  87.  * Check for array bounds if you SEGFAULT, especially in range based loops
  88.  * Always initialize everything in global
  89.  * Edge cases such as n = 1 or 0
  90.  * Make sure to calculate big O complexity
  91.  * You are geniosity you got this
  92.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement