Ankit_132

C

Oct 22nd, 2023
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8.  
  9. int main()
  10. {
  11.     _test
  12.     {
  13.         int n;
  14.         cin>>n;
  15.  
  16.         vector<int> a(n);
  17.         for(auto &e: a)     cin>>e;
  18.  
  19.         vector<int> f(n), l(n);
  20.  
  21.         map<int, int> mp;
  22.  
  23.         for(int i=0; i<n; i++)
  24.         {
  25.             if(!mp[a[i]])
  26.                 f[i] = 1;
  27.  
  28.             mp[a[i]] = 1;
  29.         }
  30.  
  31.         mp.clear();
  32.  
  33.         for(int i=n-1; i>=0; i--)
  34.         {
  35.             if(!mp[a[i]])
  36.                 l[i] = 1;
  37.  
  38.             mp[a[i]] = 1;
  39.         }
  40.  
  41.         ll int cnt = 0;
  42.         ll int ans = 0;
  43.  
  44.         for(int i=0; i<n; i++)
  45.         {
  46.             cnt += f[i];
  47.             ans += l[i]*1ll*cnt;
  48.         }
  49.  
  50.         cout<<ans<<"\n";
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment