Advertisement
SorahISA

AA TOI 23 #2 pC

Feb 22nd, 2023 (edited)
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | Jokes | 0 0
  1. #ifdef local
  2. #define _GLIBCXX_DEBUG 1
  3. #endif
  4. #pragma GCC optimize("Ofast", "unroll-loops")
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7.  
  8. #define int int64_t
  9. #define double __float80
  10. using pii = pair<int, int>;
  11. template <typename T> using Prior = std::priority_queue<T>;
  12. template <typename T> using prior = std::priority_queue<T, vector<T>, greater<T>>;
  13.  
  14. #define X first
  15. #define Y second
  16. #define ee emplace
  17. #define eb emplace_back
  18. #define ef emplace_front
  19. #define pb pop_back
  20. #define pf pop_front
  21. #define ALL(x) begin(x), end(x)
  22. #define RALL(x) rbegin(x), rend(x)
  23. #define SZ(x) ((int)(x).size())
  24.  
  25. #ifdef local
  26. #define fastIO() void()
  27. #define debug(...) \
  28.     fprintf(stderr, "\u001b[33mAt [%s], line %d: (%s) = ", __FUNCTION__, __LINE__, #__VA_ARGS__), \
  29.     _do(__VA_ARGS__), fprintf(stderr, "\u001b[0m")
  30. template <typename T> void _do(T &&_t) {cerr << _t << "\n";}
  31. template <typename T, typename ...U> void _do(T &&_t, U &&..._u) {cerr << _t << ", ", _do(_u...);}
  32. #else
  33. #define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)
  34. #define debug(...) void()
  35. #endif
  36.  
  37. template <typename T, typename U> bool chmin(T &lhs, U rhs) {return lhs > rhs ? lhs = rhs, 1 : 0;}
  38. template <typename T, typename U> bool chmax(T &lhs, U rhs) {return lhs < rhs ? lhs = rhs, 1 : 0;}
  39.  
  40. void solve() {
  41.     int N, C, Q; cin >> N >> C >> Q;
  42.    
  43.     int sum = 0;
  44.     vector<int> A(N+1);
  45.     for (int i = 1; i <= N; ++i) cin >> A[i], sum += A[i];
  46.    
  47.     int sum_large = sum, delta = 0;
  48.     prior<int> large;
  49.     Prior<int> small;
  50.     multiset<int> nums, to_delete;
  51.    
  52.     auto balance = [&]() -> void {
  53.         static auto it = end(to_delete);
  54.         while (SZ(large) > sum / C + delta) {
  55.             if ((it = to_delete.find(large.top())) != end(to_delete)) to_delete.erase(it), large.pop(), --delta;
  56.             else small.ee(large.top()), sum_large -= large.top(), large.pop();
  57.         }
  58.         while (SZ(large) < sum / C + delta) {
  59.             if ((it = to_delete.find(small.top())) != end(to_delete)) to_delete.erase(it), small.pop();
  60.             else large.ee(small.top()), sum_large += large.top(), small.pop();
  61.         }
  62.         while (SZ(large) and (it = to_delete.find(large.top())) != end(to_delete)) to_delete.erase(it), large.pop(), --delta;
  63.         while (SZ(small) and (it = to_delete.find(small.top())) != end(to_delete)) to_delete.erase(it), small.pop();
  64.     };
  65.    
  66.     auto get_ans = [&]() -> int {
  67.         int diff = sum % C;
  68.         if (SZ(small)) {
  69.             if (sum % C >= small.top()) return sum - sum_large - small.top();
  70.             auto it_hi = nums.lower_bound(sum % C);
  71.             auto it_lo = (it_hi != begin(nums) ? prev(it_hi) : end(nums));
  72.             if (it_hi != end(nums) and *it_hi <= small.top()) chmin(diff, *it_hi - (sum % C)); //, debug(*it_hi - (sum % C));
  73.             if (it_lo != end(nums) and *it_lo <= small.top()) chmin(diff, (sum % C) - *it_lo); //, debug((sum % C) - *it_lo);
  74.         }
  75.         // debug(sum, sum_large, diff);
  76.         return sum - sum_large - (sum % C) + diff;
  77.     };
  78.    
  79.     for (int i = 1; i <= N; ++i) large.ee(A[i]), nums.ee(A[i]);
  80.     balance();
  81.    
  82.     cout << get_ans() << "\n";
  83.    
  84.     for (int q = 1, xi, vi; q <= Q; ++q) {
  85.         cin >> xi >> vi;
  86.        
  87.         sum -= A[xi];
  88.         if (SZ(large) and A[xi] >= large.top()) sum_large -= A[xi], ++delta;
  89.         to_delete.ee(A[xi]);
  90.         nums.erase(nums.find(A[xi]));
  91.        
  92.         A[xi] = vi;
  93.         sum += A[xi];
  94.         if (SZ(large) and A[xi] >= large.top()) sum_large += A[xi], large.ee(A[xi]);
  95.         else small.ee(A[xi]);
  96.         nums.ee(A[xi]);
  97.        
  98.         balance();
  99.         cout << get_ans() << "\n";
  100.     }
  101. }
  102.  
  103. int32_t main() {
  104.     fastIO();
  105.    
  106.     int t = 1; // cin >> t;
  107.     for (int _ = 1; _ <= t; ++_) {
  108.         solve();
  109.     }
  110.    
  111.     return 0;
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement