Advertisement
Ryan4253

F315 V1

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