Advertisement
Guest User

Untitled

a guest
Nov 5th, 2021
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define pi pair<int, int>
  5. #define inf32 0x3f3f3f3f
  6. #define inf64 0x3f3f3f3f3f3f3f3f
  7. #define all(x) x.begin(), x.end()
  8. #define Unique(v) v.erase(unique(all(v)), v.end());
  9. #define int long long
  10. #define setinf(d) memset(d, inf32, sizeof d)
  11. #define setneg(d) memset(d, -1, sizeof d)
  12. #define set0(d) memset(d, 0, sizeof d)
  13. #define Log2(x) 63 - __builtin_clzll(x)
  14. #define oo 2e18
  15. #define mod 1000000007
  16. #define FILENAME "f"
  17.  
  18. const int maxn = 1e5 + 5;
  19. int n;
  20. int a[maxn];
  21. set<int> s[maxn];
  22.  
  23. signed main(){
  24.     ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  25.  
  26.     cin >> n;
  27.     int cur = 0;
  28.     for(int i = 1; i <= n; i++){
  29.         cur += i * (n + 1 - i);
  30.     }
  31.  
  32.     for(int i = 1; i <= 1e5; i++) s[i].insert(0), s[i].insert(n + 1);
  33.  
  34.     for(int i = 1; i <= n; i++){
  35.         cin >> a[i];
  36.         s[a[i]].insert(i);
  37.         auto it = s[a[i]].find(i);
  38.         //if(it == ++s[a[i]].begin()) continue;
  39.         auto prv = prev(it),  nx = next(it);
  40.         cur -= (*prv) * (*nx - i);
  41.     }
  42.  
  43.     int q;
  44.     cin >> q;
  45.     while(q--){
  46.         int i, x;
  47.         cin >> i >> x;
  48.         auto it = s[a[i]].find(i);
  49.         auto prv = prev(it), nx = next(it);
  50.         cur += *prv * (n + 1 - i);
  51.         if(nx != --s[a[i]].end()){
  52.             auto nxnx = next(nx);
  53.             cur += i * (n + 1 - *nx);
  54.             cur -= *prv * (n + 1 - *nx);
  55.         }
  56.         s[a[i]].erase(i);
  57.         a[i] = x;
  58.  
  59.         s[a[i]].insert(i);
  60.         it = s[a[i]].find(i);
  61.         prv = prev(it), nx = next(it);
  62.         cur -= *prv * (n + 1 - i);
  63.         if(nx != --s[a[i]].end()){
  64.             auto nxnx = next(nx);
  65.             cur -= i * (n + 1 - *nx);
  66.             cur += *prv * (n + 1  - *nx);
  67.         }
  68.        
  69.         cout << cur << '\n';
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement