fork download
  1. #include <bits/stdc++.h>
  2.  
  3.  
  4. //_ ************************** Advanced PBDS ***********************************
  5.  
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8.  
  9. using namespace std;
  10. using namespace __gnu_pbds;
  11.  
  12. template<class T>
  13. using ordered_set = tree<
  14. T,
  15. null_type,
  16. less<T>,
  17. rb_tree_tag,
  18. tree_order_statistics_node_update
  19. >;
  20.  
  21.  
  22. //_ ****************************************************************************
  23.  
  24.  
  25. #define int long long int
  26. #define double long double
  27. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  28.  
  29.  
  30. const int M = 1000000007;
  31. const int N = 3e5+9;
  32. const int INF = 2e9+1;
  33. const int LINF = 2000000000000000001;
  34.  
  35. inline int power(int a, int b, int mod=M) {
  36. int x = 1;
  37. a %= mod;
  38. while (b) {
  39. if (b & 1) x = (x * a) % mod;
  40. a = (a * a) % mod;
  41. b >>= 1;
  42. }
  43. return x;
  44. }
  45.  
  46.  
  47. //_ ***************************** START Below *******************************
  48.  
  49. // 6
  50. // 0 1 1 0 1 1
  51.  
  52.  
  53. vector<int> a;
  54.  
  55. int consistency(int n){
  56.  
  57. ordered_set<pair<int,int>> st;
  58. st.insert({0,-1}); //* insert index for uniqueness
  59.  
  60. int ans = 0;
  61. int sum = 0;
  62.  
  63. for(int i=0; i<n; i++){
  64. if(a[i] == 0) sum--;
  65. else sum++;
  66.  
  67. int ct = st.order_of_key({sum, -INF});
  68.  
  69. st.insert({sum,i});
  70. ans += ct;
  71.  
  72. }
  73.  
  74. return ans;
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. int practice(int n){
  93.  
  94.  
  95. return 0;
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. void solve() {
  103.  
  104. int n;
  105. cin>> n;
  106.  
  107. a.resize(n);
  108. for(int i=0; i<n; i++) cin >> a[i];
  109.  
  110. cout << consistency(n) << endl;
  111.  
  112.  
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119. int32_t main() {
  120. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  121.  
  122. int t = 1;
  123. // cin >> t;
  124. while (t--) {
  125. solve();
  126. }
  127.  
  128. return 0;
  129. }
Success #stdin #stdout 0s 5288KB
stdin
6
0 1 1 0 1 1
stdout
15