Advertisement
Ryan4253

f315 v2

Jan 8th, 2021
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define endl '\n'
  5. #define mp make_pair
  6. #define pb push_back
  7. #define ll long long
  8. #define pii pair<int, int>
  9. #define F first
  10. #define S second
  11.  
  12. struct Node{
  13. int val;
  14. int l, r;
  15. int lson, rson;
  16. };
  17.  
  18. const int N = 100000 + 5;
  19. int a[2 * N];
  20. Node st[2 * N];
  21. int sol[N];
  22. int st_ptr = 0;
  23. bool visit[N];
  24.  
  25. void build(int l, int r, int index){
  26. st[index].l = l; st[index].r = r;
  27.  
  28. if(l + 1 == r){
  29. st[index].val = 0;
  30. return;
  31. }
  32.  
  33. st[index].lson = ++st_ptr;
  34. st[index].rson = ++st_ptr;
  35.  
  36. int mid = (l + r) / 2;
  37. build(l, mid, st[index].lson);
  38. build(mid, r, st[index].rson);
  39.  
  40. st[index].val = st[st[index].lson].val + st[st[index].rson].val;
  41. }
  42.  
  43. void increment(int x, int index){
  44. if(st[index].l + 1 == st[index].r){
  45. st[index].val++;
  46. return;
  47. }
  48.  
  49. int mid = (st[index].l + st[index].r) / 2;
  50.  
  51. if(x <= mid){
  52. increment(x, st[index].lson);
  53. }
  54. else{
  55. increment(x, st[index].rson);
  56. }
  57.  
  58. st[index].val = st[st[index].lson].val + st[st[index].rson].val;
  59. }
  60.  
  61. int query(int l, int r, int index){
  62. if(st[index].l == l && st[index].r == r){
  63. return st[index].val;
  64. }
  65.  
  66. int mid = (st[index].l + st[index].r) / 2;
  67.  
  68. if(r <= mid){
  69. return query(l, r, st[index].lson);
  70. }
  71. else if(l >= mid){
  72. return query(l, r, st[index].rson);
  73. }
  74. else{
  75. return query(l, mid, st[index].lson) + query(mid, r, st[index].rson);
  76. }
  77. }
  78.  
  79. void solve(){
  80. int n; cin >> n;
  81. for(int i = 0; i < 2 * n; i++){
  82. cin >> a[i];
  83. }
  84.  
  85. build(0, n + 1, 0);
  86.  
  87.  
  88.  
  89. for(int i = 0; i < 2 * n; i++){
  90. int x = a[i];
  91. if(!visit[x]){
  92. sol[x] -= query(0, x, 0);
  93. }
  94. else{
  95. sol[x] += query(0, x, 0)-1;
  96. }
  97.  
  98. increment(x, 0);
  99.  
  100. visit[x] = true;
  101. }
  102.  
  103.  
  104. ll int solution = 0;
  105. for(int i = 1; i <= n; i++){
  106. solution += sol[i];
  107. }
  108.  
  109. cout << solution << endl;
  110. }
  111.  
  112. int main(){
  113. ios::sync_with_stdio(0); cin.tie(0);
  114. solve();
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement