Advertisement
yeskendir_sultanov

РЮО - С

Apr 16th, 2025
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. int n;
  7. vector<ll> a;
  8. map<ll, int> cnt;
  9. set<ll> b, c;
  10.  
  11. ll get() {
  12.     ll ans = 0;
  13.     if (c.size() > 0) {
  14.         ll x = -(*c.begin());
  15.         ans = x * x;
  16.     }
  17.     if (b.size() > 1) {
  18.         ll x = -(*b.begin());
  19.         b.erase(-x);
  20.         ll y = -(*b.begin());
  21.         ans = max(ans, x * y);
  22.         b.insert(-x);
  23.     }
  24.     return ans;
  25. }
  26.  
  27. void update(int k, ll x) {
  28.     if (k >= n) {
  29.         return;
  30.     }
  31.     if (cnt[a[k]] == 2) {
  32.         b.erase(-a[k]);
  33.     } else if (cnt[a[k]] == 4) {
  34.         c.erase(-a[k]);
  35.     }
  36.     cnt[a[k]]--;
  37.     if (cnt[a[k]] == 0) {
  38.         cnt.erase(a[k]);
  39.     }
  40.    
  41.     a[k] = x;
  42.     cnt[a[k]]++;
  43.    
  44.     if (cnt[a[k]] == 2) {
  45.         b.insert(-a[k]);
  46.     } else if (cnt[a[k]] == 4) {
  47.         c.insert(-a[k]);
  48.     }
  49. }
  50.  
  51. int main() {
  52.     cin >> n;
  53.     a.resize(n);
  54.    
  55.     for (int i = 0; i < n; ++i) {
  56.         cin >> a[i];
  57.         cnt[a[i]]++;
  58.     }
  59.    
  60.     for (auto e : cnt) {
  61.         ll key = e.first;
  62.         int value = e.second;
  63.         if (value >= 2) {
  64.             b.insert(-key);
  65.         }
  66.         if (value >= 4) {
  67.             c.insert(-key);
  68.         }
  69.     }
  70.    
  71.     cout << get() << endl;
  72.    
  73.     int q;
  74.     cin >> q;
  75.     while (q--) {
  76.         int k;
  77.         ll x;
  78.         cin >> k >> x;
  79.         update(k - 1, x);
  80.         cout << get() << endl;
  81.     }
  82.    
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement