willy108

ucfj

Feb 7th, 2022
1,923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <utility>
  7. #include <cmath>
  8. #include <cassert>
  9. #include <algorithm>
  10. #include <vector>
  11. #include <queue>
  12. #define cont continue
  13. #define moo printf
  14. #define oom scanf
  15. #define mool puts("")
  16. #define ll long long
  17. const ll mod = 1e9 + 7;
  18. const int MX = 2e5 +10, LOGN = 60, int_max = 0x3f3f3f3f;
  19.  
  20. using namespace std;
  21. //pst nuke gold. wooooooooooo
  22. int sum[MX * LOGN], rc[MX * LOGN], lc[MX * LOGN];
  23. int n, ind = 0, s = 1;
  24. int arr[MX], root[MX], nex[MX], occ[MX];
  25.  
  26. void dup(int &k){
  27.   ind++;
  28.   sum[ind ] =sum[k];
  29.   lc[ind] = lc[k];
  30.   rc[ind] = rc[k];
  31.   k = ind;
  32. }
  33.  
  34. void U(int p, int& k, int L, int R){
  35.   if(p < L || R <= p || R <= L) return ;
  36.   dup(k);
  37.   if(L + 1 == R){
  38.     assert(p == L);
  39.     sum[k]++;
  40.     return ;
  41.   }
  42.   int mid = (L + R)/2;
  43.   U(p, lc[k], L, mid);
  44.   U(p, rc[k], mid, R);
  45.   sum[k] = sum[lc[k]] + sum[rc[k]];
  46. }
  47.  
  48. int S(int qL, int qR, int k, int L, int R){
  49.   if(qR <= L || R <= qL || R <= L) return 0;
  50.   if(qL <= L && R <= qR) return sum[k];
  51.   int mid = (L + R)/2;
  52.   return S(qL, qR, lc[k], L, mid) + S(qL, qR, rc[k], mid, R);
  53. }
  54.  
  55.  
  56. int main(){
  57.   cin.tie(0) -> sync_with_stdio(0);
  58.   cin >> n;
  59.   while(s <= n +2) s *= 2;
  60.   for(int i = 0; i<=n; i++) nex[i] = n +1;
  61.   for(int i = 1; i<=n; i++){
  62.     cin >> arr[i];
  63.     if(occ[arr[i]]){
  64.       nex[occ[arr[i]]] = i;
  65.     }
  66.     occ[arr[i]] = i;
  67.   }
  68.   //for(int i = 1; i<=n; i++) moo("%d ", nex[i]);
  69.   for(int i = 1; i<=n; i++){
  70.     root[i] = root[i - 1];
  71.     if(nex[i] != n + 1){
  72.       U(nex[i], root[i], 0, s);
  73.     }
  74.   }
  75.   ll fail = 0ll;
  76.   for(int i = 1; i<=n; i++){
  77.     if(nex[i] != n + 1){
  78.       fail += (ll)(n + 1 - nex[i]); //all the positions after nex[i] are invalid
  79.     }
  80.     assert(nex[i] != i);
  81.     fail += (ll)(S(i, nex[i], root[n], 0, s) - S(i, nex[i], root[i], 0, s));
  82.   }
  83.   moo("%lld\n", (ll)((n*(n-1))/2ll) - fail);
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment