Ankit_132

D

Nov 25th, 2023
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll     long long
  6. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  7.  
  8. class SegTree
  9. {
  10.     public:
  11.     const int N = 1000005;
  12.     int n;
  13.     ll int  *__;
  14.     ll int ___ = 0;
  15.  
  16.     SegTree()
  17.     {__ = new ll int[N];}
  18.     void init(vector<int> &d, int n)
  19.     {this->n = n; c(d);}
  20.     void c(vector<int> &d)
  21.     {
  22.         for (int i = 0; i < n; ++i) __[n+i]=d[i];
  23.         for (int i = n - 1; i > 0; --i) __[i] = __[i<<1] + __[i<<1|1];
  24.     }
  25.     void ___ult(int l, int r)
  26.     {
  27.         for (l += n, r += n; l < r; l >>= 1, r >>= 1)
  28.         {
  29.             if (l&1)___ = ___ + __[l++];
  30.             if (r&1)___ = ___ + __[--r];
  31.         }
  32.     }
  33.     ll int getSum(int l, int r)
  34.     {
  35.         ___ = 0; ___ult(l, r);
  36.         return ___;
  37.     }
  38.     void update(int p, ll int _)
  39.     {
  40.         for(__[p += n] = _; p > 1; p >>= 1)
  41.         {
  42.             __[p>>1] = (__[p] + __[p^1]);
  43.         }
  44.     }
  45. };
  46.  
  47. int main()
  48. {
  49.     SegTree sgt;
  50.  
  51.     _test
  52.     {
  53.         int n, q;
  54.         cin >> n >> q;
  55.  
  56.         set<int> s;
  57.  
  58.         vector<int> a(n+1);
  59.  
  60.         for (int i = 1; i <= n; i++)
  61.         {
  62.             cin >> a[i];
  63.  
  64.             if (a[i] == 1)
  65.                 s.insert(i);
  66.         }
  67.  
  68.         sgt.init(a, n+1);
  69.  
  70.         while (q--)
  71.         {
  72.             int t;
  73.             cin >> t;
  74.  
  75.             if (t == 2)
  76.             {
  77.                 int i, v;
  78.                 cin >> i >> v;
  79.  
  80.                 if (a[i] == 1)
  81.                     s.erase(i);
  82.  
  83.                 a[i] = v;
  84.                 if (a[i] == 1)
  85.                     s.insert(i);
  86.  
  87.                 sgt.update(i, v);
  88.             }
  89.             else
  90.             {
  91.                 int v;
  92.                 cin >> v;
  93.  
  94.                 int sum = sgt.getSum(1, n+1);
  95.  
  96.                 int d = sum - v;
  97.  
  98.                 if (v > sum)
  99.                 {
  100.                     cout << "NO" << endl;
  101.                     continue;
  102.                 }
  103.  
  104.                 if (d % 2 == 0)
  105.                 {
  106.                     cout << "YES" << endl;
  107.                     continue;
  108.                 }
  109.  
  110.                 ll int mx = 0;
  111.  
  112.                 if (s.size())
  113.                 {
  114.                     int f = *s.begin(), l = *(--s.end());
  115.                     mx = max(mx, sgt.getSum(f+1, n+1));
  116.                     mx = max(mx, sgt.getSum(1, l));
  117.                 }
  118.  
  119.                 if (v <= mx)
  120.                     cout << "YES" << endl;
  121.                 else
  122.                     cout << "NO" << endl;
  123.             }
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment