Advertisement
Dang_Quan_10_Tin

CỘNG VÀ TRỪ

Aug 3rd, 2022 (edited)
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using ll = long long;
  5.  
  6. constexpr int N = 5e5 + 5;
  7. int n, q;
  8. ll a[N], b[N];
  9.  
  10. void Read()
  11. {
  12.     cin >> n >> q;
  13.     for (int i = 1; i <= n; ++i)
  14.         cin >> a[i];
  15. }
  16.  
  17. ll Get(ll v)
  18. {
  19.     int id = lower_bound(a + 1, a + n + 1, v) - a;
  20.  
  21.     return v * (id - 1) - b[id - 1] + (b[n] - b[id - 1]) - v * (n - id + 1);
  22. }
  23.  
  24. void Solve()
  25. {
  26.     ll sum(0);
  27.  
  28.     sort(a + 1, a + n + 1);
  29.  
  30.     for (int i = 1; i <= n; ++i)
  31.         b[i] = b[i - 1] + a[i];
  32.  
  33.     b[n + 1] = b[n];
  34.  
  35.     cout << Get(sum) << "\n";
  36.  
  37.     for (int i = 1; i <= q; ++i)
  38.     {
  39.         char c;
  40.         ll v;
  41.         cin >> c >> v;
  42.  
  43.         if (c == 'A')
  44.             sum -= v;
  45.         else
  46.             sum += v;
  47.  
  48.         cout << Get(sum) << "\n";
  49.     }
  50. }
  51.  
  52. int32_t main()
  53. {
  54.     ios::sync_with_stdio(0);
  55.     cin.tie(0);
  56.     cout.tie(0);
  57.     Read();
  58.     Solve();
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement