Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- typedef vector<int> vi;
- typedef vector<ll> vl;
- typedef pair<int, int> pii;
- typedef pair<ll, ll> pll;
- typedef priority_queue<int> pq;
- typedef priority_queue<int, vi, greater<int>> mpq;
- const ll INF = 1e18;
- const int mod = 1e9+7;
- const int dX[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
- #define f first
- #define s second
- #define pb push_back
- #define mp make_pair
- #define ft front()
- #define bk back()
- #define sz(x) (int)x.size()
- #define all(x) (x).begin(), (x).end()
- #define FOR(i, a) for(int i = 0; i < a; i++)
- #define FoR(i, a) for(int i = 1; i <= a; i++)
- #define ROF(i, a) for (int i = (a)-1; i >= 0; i--)
- void readIn(string fileName) { freopen(fileName.c_str(), "r", stdin); }
- void writeOut(string fileName) { freopen(fileName.c_str(), "w", stdout); }
- void syncIO() { ios_base::sync_with_stdio(0); cin.tie(0); }
- void setIO(string str = ""){ syncIO(); if(sz(str)) { readIn(str+".in"), writeOut(str+".out"); }}
- int n; int arr[100001];
- int numInversions[100001];
- int bit[100001];
- void update(int x, int y){
- for(;x <= n+1;x += x&-x){
- bit[x] += y;
- }
- }
- int query(int x){
- int sum = 0;
- for(;x > 0; x -= x&-x){
- sum += bit[x];
- }
- return sum;
- }
- int main(){
- syncIO;
- /* string fileName = "haircut"; setIO(fileName); */
- //-------------------------------------------------
- cin >> n;
- for(int i = 0; i < n; i++){
- cin >> arr[i];
- arr[i]++;
- }
- for(int i = 0; i < n; i++){
- int curr = arr[i];
- //now we want to find the number uf inversions for this element
- numInversions[curr] += (query(n+1) - query(curr));
- /* cout << " numInv " << query(n+1) << " " << query(curr) << endl; */
- update(curr, 1);
- }
- //now we create the psums in the numInversions array
- for(int i = 0; i <= n+1; i++){
- cout << numInversions[i] << " ";
- }
- cout << endl;
- for(int i = 1; i <= n + 1; i++){
- numInversions[i] += numInversions[i-1];
- }
- for(int i = 0; i < n; i++){
- cout << numInversions[i] << endl;
- }
- return 0;
- // Read notes to self
- }
- /* NOTES TO SELF:
- * Check fur int vs ll, worst case just switch everything to ll
- * Check for array bounds if you SEGFAULT, especially in range based loops
- * Always initialize everything in global
- * Edge cases such as n = 1 or 0
- * Make sure to calculate big O complexity
- * You are geniosity you got this
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement