jainbot27

Untitled

Sep 9th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #pragma GCC optimize("Ofast")
  2. #pragma GCC target("sse4,avx,avx2,fma")
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6.  
  7. #define f first
  8. #define s second
  9. #define pb push_back
  10. #define mp make_pair
  11. #define ts to_string
  12. #define ub upper_bound
  13. #define lb lower_bound
  14. #define ar array
  15. #define all(x) x.begin(), x.end()
  16. #define siz(x) (int)x.size()
  17. #define FOR(x, y, z) for(int x = (y); x < (z); x++)
  18. #define ROF(x, y, z) for(int x = (y-1); x >= (z); x--)
  19. #define F0R(x, z) FOR(x, 0, z)
  20. #define R0F(x, z) ROF(x, z, 0)
  21. #define trav(x, y) for(auto&x:y)
  22.  
  23.  
  24. using ll = int64_t;
  25. using vi = vector<int>;
  26. using vl = vector<ll>;
  27. using pii = pair<int, int>;
  28. using pll = pair<ll, ll>;
  29. using str = string;
  30. using vpii = vector<pii>;
  31. using vpll = vector<pll>;
  32.  
  33.  
  34. template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
  35. template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
  36. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  37. void usaco(string x=""){
  38.     ios_base::sync_with_stdio(0); cin.tie(0); x=x;
  39. #ifndef D
  40.     if(siz(x)){
  41.         freopen((x+".in").c_str(), "r", stdin);
  42.         freopen((x+".out").c_str(), "w", stdout);
  43.     }
  44. #endif
  45. }
  46.  
  47.  
  48. str ts(char c) { return str(1,c); }
  49. str ts(bool b) { return b ? "true" : "false"; }
  50. str ts(const char* s) { return (str)s; }
  51. str ts(str s) { return s; }
  52. template<class A> str ts(complex<A> c) {
  53.     stringstream ss; ss << c; return ss.str(); }
  54. str ts(vector<bool> v) {
  55.     str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
  56.     res += "}"; return res; }
  57. template<size_t SZ> str ts(bitset<SZ> b) {
  58.     str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
  59.     return res; }
  60. template<class A, class B> str ts(pair<A,B> p);
  61. template<class T> str ts(T v) {
  62.     bool fst = 1; str res = "{";
  63.     for (const auto& x: v) {
  64.         if (!fst) res += ", ";
  65.         fst = 0; res += ts(x);
  66.     }
  67.     res += "}"; return res;
  68. }
  69. template<class A, class B> str ts(pair<A,B> p) {
  70.     return "("+ts(p.f)+", "+ts(p.s)+")"; }
  71. void DBG() { cerr << "]" << endl; }
  72. template<class H, class... T> void DBG(H h, T... t) {
  73.     cerr << ts(h); if (sizeof...(t)) cerr << ", ";
  74.     DBG(t...); }
  75.  
  76.  
  77. #ifdef D
  78. #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
  79. #else
  80. #define dbg(...) 0
  81. #endif
  82.  
  83. const int mxN = 1e5 + 10;
  84. int ans[mxN], bit[mxN], n, a[mxN], cnt[mxN]; vi q[mxN];
  85. int qry(int r){
  86.     int res = 0;
  87.     for(; r; r-=r&-r){
  88.         res += bit[r];
  89.     }
  90.     return res;
  91. }
  92. void update(int x, int v){
  93.     for(; v < mxN; v += v & -v){
  94.         bit[v] += x;
  95.     }
  96. }
  97.  
  98. signed main(){
  99.     ifstream cin("haircut.in"); ofstream cout("haircut.out");
  100.     cin >> n;
  101.     FOR(i, 1, n+1) {cin >> a[i]; q[a[i]].pb(i);}
  102.     FOR(i, 1, n+1){
  103.         if(a[i])
  104.             cnt[i] = qry(n)-qry(a[i]);
  105.         else
  106.             cnt[i] = qry(n);
  107.         if(a[i])
  108.             update(1, a[i]);
  109.     }
  110.     int res = 0;
  111.     FOR(i, 1, n+1) res += cnt[i];
  112.     ROF(i, n, 1){
  113.         trav(v, q[i]){
  114.             res -= cnt[v];
  115.         }
  116.         ans[i] = res;
  117.     }
  118.     F0R(i, n) cout << ans[i] << endl;
  119. }
  120. /*
  121.  * Stuff I should try:
  122.  * Is it Monotonic? -> Binary Search
  123.  * Is N small? -> Bitsets or other exponential
  124.  * Edge Cases? Look Carefully
  125.  * Can't figure anything out? Brute Force and see patterns
  126.  * Try to prove greedies
  127.  * Prefix Sums, Amortization, DP, Data Structures
  128. */
  129.  
  130.  
Advertisement
Add Comment
Please, Sign In to add comment