Advertisement
lalalalalalalaalalla

Untitled

Oct 5th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <iomanip>
  5. #include <queue>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <tuple>
  9. #include <iomanip>
  10. #include <stdio.h>
  11. #include <numeric>
  12. #include <map>
  13. #include <bitset>
  14. #include <set>
  15. #include <stack>
  16. #include <queue>
  17. #include <unordered_set>
  18.  
  19.  
  20. //#pragma GCC optimize("Ofast,no-stack-protector")
  21. #pragma GCC optimize("O3")
  22. //#pragma GCC target("avx2")
  23. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
  24. //#pragma GCC optimize("unroll-loops")
  25. //#pragma GCC optimize("fast-math")
  26. //#pragma GCC optimize("section-anchors")
  27. //#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
  28. //#pragma GCC optimize("vpt")
  29. //#pragma GCC optimize("rename-registers")
  30. //#pragma GCC optimize("move-loop-invariants")
  31. //#pragma GCC optimize("unswitch-loops")
  32. //#pragma GCC optimize("function-sections")
  33. //#pragma GCC optimize("data-sections")
  34. //#pragma GCC optimize("branch-target-load-optimize")
  35. //#pragma GCC optimize("branch-target-load-optimize2")
  36. //#pragma GCC optimize("btr-bb-exclusive")
  37.  
  38.  
  39. #define int long long
  40. #define ll long long
  41. #define ull unsigned long long
  42. #define all(a) a.begin(), a.end()
  43. #define pii pair<int, int>
  44. #define pb push_back
  45. #define ld long double
  46.  
  47.  
  48. using namespace std;
  49.  
  50. //const int INF = 1e13;
  51. //const int mod = 2600000069;
  52. //const int p = 179;
  53.  
  54. int n;
  55. vector<int> a, t, dif, last;
  56. map<int, int> ind;
  57.  
  58. void update(int v, int l, int r, int pos) {
  59. if (r - l == 1) {
  60. t[v] = 1;
  61. return;
  62. }
  63. int m = (l + r) / 2;
  64. if (pos < m) {
  65. update(2 * v + 1, l, m, pos);
  66. } else {
  67. update(2 * v + 2, m, r, pos);
  68. }
  69. t[v] = t[2 * v + 1] + t[2 * v + 2];
  70. }
  71.  
  72. int sum(int v, int l, int r, int askl) {
  73. if (r <= askl) {
  74. return 0;
  75. }
  76. if (l >= askl) {
  77. return t[v];
  78. }
  79. int m = (l + r) / 2;
  80. return sum(2 * v + 1, l, m, askl) + sum(2 * v + 2, m, r, askl);
  81. }
  82.  
  83. vector<int> t1;
  84.  
  85. void update1(int v, int l, int r, int pos, int val) {
  86. if (r - l == 1) {
  87. t1[v] = val;
  88. return;
  89. }
  90. int m = (l + r) / 2;
  91. if (pos < m) {
  92. update1(2 * v + 1, l, m, pos, val);
  93. } else {
  94. update1(2 * v + 2, m, r, pos, val);
  95. }
  96. t1[v] = t1[2 * v + 1] + t1[2 * v + 2];
  97. }
  98.  
  99. int get_sum(int v, int l, int r, int askl) {
  100. if (r <= askl) {
  101. return 0;
  102. }
  103. if (l >= askl) {
  104. return t1[v];
  105. }
  106. int m = (l + r) / 2;
  107. return get_sum(2 * v + 1, l, m, askl) + get_sum(2 * v + 2, m, r, askl);
  108. }
  109.  
  110. signed main() {
  111. ios_base::sync_with_stdio(0);
  112. cin.tie(0);
  113. cout.tie(0);
  114. cin >> n;
  115. a.resize(n);
  116. for (auto &k : a) cin >> k;
  117. t.resize(4 * n);
  118. dif.assign(n, 0);
  119. last.assign(n, 0);
  120. t1.assign(4 * n, 0);
  121. vector<int> b = a;
  122. sort(all(b));
  123. for (int i = 0; i < n; i++) {
  124. ind[b[i]] = i;
  125. }
  126. int ans = 0, x;
  127. for (int i = 0; i < n; i++) {
  128. last[ind[a[i]]] = sum(0, 0, n, ind[a[i]]);
  129. x = get_sum(0, 0, n, ind[a[i]]);
  130. ans += x;
  131. update1(0, 0, n, ind[a[i]], last[ind[a[i]]]);
  132. update(0, 0, n, ind[a[i]]);
  133. }
  134. // for (auto k : last) cout << k << " ";
  135. // cout << "\n";
  136. cout << ans;
  137.  
  138. }
  139. /*
  140. 3
  141. 3 2 1
  142. -> 1
  143.  
  144. 3
  145. 2 3 1
  146. -> 0
  147.  
  148. 4
  149. 10 8 3 1
  150. -> 4
  151.  
  152. 4
  153. 1 5 4 3
  154. -> 1
  155. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement