Advertisement
lalalalalalalaalalla

Untitled

Oct 6th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 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_was, t_inv2;
  56.  
  57. void update(vector<int>& t, int v, int l, int r, int pos, int val) {
  58. if (r - l == 1) {
  59. t[v] = val;
  60. return;
  61. }
  62. int m = (l + r) / 2;
  63. if (pos < m) {
  64. update(t, 2 * v + 1, l, m, pos, val);
  65. } else {
  66. update(t, 2 * v + 2, m, r, pos, val);
  67. }
  68. t[v] = t[2 * v + 1] + t[2 * v + 2];
  69. }
  70.  
  71. int get_sum(vector<int>& t, int v, int l, int r, int askl) {
  72. if (r <= askl) {
  73. return 0;
  74. }
  75. if (l >= askl) {
  76. return t[v];
  77. }
  78. int m = (l + r) / 2;
  79. return get_sum(t, 2 * v + 1, l, m, askl) + get_sum(t, 2 * v + 2, m, r, askl);
  80. }
  81.  
  82. signed main() {
  83. ios_base::sync_with_stdio(0);
  84. cin.tie(0);
  85. cout.tie(0);
  86. cin >> n;
  87. a.resize(n);
  88. t_was.assign(4 * n, 0);
  89. t_inv2.assign(4 * n, 0);
  90. for (int i = 0; i < n; i++) {
  91. cin >> a[i];
  92. }
  93. map<int, int> ind;
  94. vector<int> b = a;
  95. sort(all(b));
  96. for (int i = 0; i < n; i++) ind[b[i]] = i;
  97. vector<int> was(n, 0), inv2(n, 0);
  98. int ans = 0;
  99. for (int i = 0; i < n; i++) {
  100. inv2[i] += get_sum(t_was, 0, 0, n, ind[a[i]]);
  101. ans += get_sum(t_inv2, 0, 0, n, ind[a[i]]);
  102. update(t_was, 0, 0, n, ind[a[i]], 1);
  103. update(t_inv2, 0, 0, n, ind[a[i]], inv2[i]);
  104. }
  105. cout << ans;
  106. }
  107. /*
  108. 3
  109. 3 2 1
  110. -> 1
  111.  
  112. 3
  113. 2 3 1
  114. -> 0
  115.  
  116. 4
  117. 10 8 3 1
  118. -> 4
  119.  
  120. 4
  121. 1 5 4 3
  122. -> 1
  123. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement