jainbot27

asdfds

Sep 9th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define all(x) x.begin(), x.end()
  5. #define siz(x) (int)x.size()
  6. #define FOR(x, y, z) for(int x = (y); x < (z); x++)
  7. #define ROF(x, y, z) for(int x = (y-1); x >= (z); x--)
  8. #define F0R(x, z) FOR(x, 0, z)
  9. #define R0F(x, z) ROF(x, z, 0)
  10. #define trav(x, y) for(auto&x:y)
  11.  
  12.  
  13. using ll = int64_t;
  14. using vi = vector<int>;
  15. using vl = vector<ll>;
  16. using pii = pair<int, int>;
  17. using pll = pair<ll, ll>;
  18. using str = string;
  19. using vpii = vector<pii>;
  20. using vpll = vector<pll>;
  21.  
  22.  
  23. const int mxN = 2e5 + 10;
  24. int ans[mxN], bit[mxN], n, a[mxN], cnt[mxN]; vi q[mxN];
  25. int qry(int r){
  26.     int res = 0;
  27.     for(; r; r-=r&-r){
  28.         res += bit[r];
  29.     }
  30.     return res;
  31. }
  32. void update(int x, int v){
  33.     for(; v < mxN; v += v & -v){
  34.         bit[v] += x;
  35.     }
  36. }
  37. signed main(){
  38.     ifstream cin("haircut.in"); ofstream cout("haircut.out");
  39.     cin >> n;
  40.     FOR(i, 1, n+1) {cin >> a[i]; q[a[i]].push_back(i);}
  41.     FOR(i, 1, n+1){
  42.         if(a[i])
  43.             cnt[i] = qry(n)-qry(a[i]);
  44.         else
  45.             cnt[i] = qry(n);
  46.         if(a[i])
  47.             update(1, a[i]);
  48.     }
  49.     int res = 0;
  50.     FOR(i, 1, n+1) res += cnt[i];
  51.     ROF(i, n, 1){
  52.         trav(v, q[i]){
  53.             res -= cnt[v];
  54.         }
  55.         ans[i] = res;
  56.     }
  57.     F0R(i, n) cout << ans[i] << endl;
  58. }
  59. /*
  60.  * Stuff I should try:
  61.  * Is it Monotonic? -> Binary Search
  62.  * Is N small? -> Bitsets or other exponential
  63.  * Edge Cases? Look Carefully
  64.  * Can't figure anything out? Brute Force and see patterns
  65.  * Try to prove greedies
  66.  * Prefix Sums, Amortization, DP, Data Structures
  67. */
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment