SHARE
    TWEET
    Vince14

    /<> 1517 (top down seg tree)

    Sep 21st, 2023
    147
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    C++ 2.23 KB | Source Code | 0 0
    1. #include <iostream>
    2. #include <string>
    3. #include <cstring>
    4. #include <algorithm>
    5. #include <cmath>
    6. #include <vector>
    7. #include <set>
    8. #include <map>
    9. #include <stack>
    10. #include <queue>
    11. #include <deque>
    12. #include <unordered_map>
    13. #include <numeric>
    14. #include <iomanip>
    15. using namespace std;
    16. #define pii pair<long long, long long>
    17. #define ll long long
    18. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
    19. const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
    20. const long long dl[2] = {1, -1};
    21. const long long MOD = 1000000007;
    22. const long long MAXN = 500005;
    23. int N;
    24. int loc[MAXN], arr[MAXN], assigned[MAXN];
    25. int seg[MAXN * 4];
    26. long long ans = 0;
    27. void build_seg(int x, int s, int e){
    28. if(s == e){
    29. seg[x] = 0;
    30. return;
    31. }
    32. int mid = (s + e)/2;
    33. build_seg(x * 2, s, mid);
    34. build_seg(x * 2 + 1, mid + 1, e);
    35. seg[x] = seg[x * 2] + seg[x * 2 + 1];
    36. }
    37. int query(int x, int s, int e, int a, int b){
    38. if(e < a or s > b) return 0;
    39. if(a <= s and e <= b) return seg[x];
    40. int mid = (s + e) / 2;
    41. return query(x * 2, s, mid, a, b) + query(x * 2 + 1, mid + 1, e, a, b);
    42. }
    43. void update(int x, int s, int e, int idx, int val){
    44. if(idx < s or idx > e) return;
    45. if(s == e){
    46. seg[x] = val;
    47. return;
    48. }
    49. int mid = (s + e) / 2;
    50. update(x * 2, s, mid, idx, val);
    51. update(x * 2 + 1, mid + 1, e, idx, val);
    52. seg[x] = seg[x * 2] + seg[x * 2 + 1];
    53. }
    54. vector<int> tocomp;
    55. vector<vector<int>> num(MAXN);
    56. int main() {
    57. FAST;
    58. cin >> N;
    59. for(int i = 1; i <= N; i++){
    60. cin >> arr[i];
    61. tocomp.push_back(arr[i]);
    62. }
    63. sort(tocomp.begin(), tocomp.end());
    64. for(int i = 1; i <= N; i++){
    65. assigned[i] = (int) (std::lower_bound(tocomp.begin(), tocomp.end(), arr[i]) - tocomp.begin()) + 1;
    66. num[assigned[i]].push_back(i);
    67. }
    68. build_seg(1, 1, N);
    69. for(int k = 1; k <= N; k++){
    70. for(auto i : num[k]){
    71. int q = (i - 1) - query(1, 1, N, 1, i);
    72. // cout << i << " " << loc[i] << " " << q << "\n";
    73. ans += q;
    74. update(1, 1, N, i, 1);
    75. }
    76. }
    77. cout << ans << "\n";
    78. }
    79. /*
    80. 5
    81. 5 4 3 2 1
    82. 6
    83. 2 1 3
    84. 2 1 4
    85. 1 5 3
    86. 2 3 5
    87. 1 4 3
    88. 2 3 5
    89. */
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /