Problem p02122 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
#define INF (1LL << 60)
using namespace std;
typedef long long ll;

ll N, A, B, Q, X, Y, Z;

template <typename T> class SegmentTree {
public:
  ll n;
  T e;
  T (*op)(T, T);
  vector<T> dat;

  SegmentTree(T _e, T (*_op)(T, T), ll _n) {
    e = _e;
    op = _op;
    n = 1;
    while (n < _n)
      n *= 2;
    REP(i, 0, 2 * n - 1) dat.push_back(e);
  }

  T query(ll a, ll b) { return rquery(a, b, 0, 0, n); };

  void update(ll k, T a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = op(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

private:
  T rquery(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l)
      return e;
    if (a <= l && r <= b)
      return dat[k];
    T vl = rquery(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = rquery(a, b, k * 2 + 2, (l + r) / 2, r);
    return op(vl, vr);
  }
};

ll opmin(ll a, ll b) { return min(a, b); };

int main(void) {
  SegmentTree<ll> sa(INF, opmin, 200000), sb(INF, opmin, 200000);

  cin >> N;
  REP(i, 0, N) {
    cin >> A;
    sa.update(i, A);
    // cout << A << endl;
  }
  REP(i, 0, N) {
    cin >> B;
    sb.update(i, B);
    // cout << B << endl;
  }
  cin >> Q;

  // REP(i, 0, N) cout << sa.dat[i] << " ";
  // cout << endl;
  // REP(i, 0, N) cout << sb.dat[i] << " ";
  // cout << endl;

  vector<ll> u;
  REP(i, 0, N) u.push_back(i);

  // REP(j, 0, N) cout << sa.query(j, j + 1) << " ";
  // cout << endl;
  // REP(j, 0, N) cout << sb.query(j, j + 1) << " ";
  // cout << endl;

  REP(q, 0, Q) {
    cin >> X >> Y >> Z;
    // cout << "  " << X << " " << Y << " " << Z << endl;
    if (X == 1) { // update A[Y] = Z
      sa.update(Y - 1, Z);
      u.push_back(Y - 1);
    }
    if (X == 2) { // update B[Y] = Z
      sb.update(Y - 1, Z);
      u.push_back(Y - 1);
    }
    if (X == 3) { // max(A[Y], ..., A[Z])
      cout << sa.query(Y - 1, Z) << endl;
      ;
    }
    if (X == 4) { // max(B[Y], ..., B[Z])
      cout << sb.query(Y - 1, Z) << endl;
    }
    if (X == 5) { // copy A <- B
      REP(i, 0, u.size()) sa.update(u[i], sb.dat[u[i] + sb.n - 1]);
      u.clear();
    }
    if (X == 6) { // copy B <- A
      REP(i, 0, u.size()) sb.update(u[i], sa.dat[u[i] + sa.n - 1]);
      u.clear();
    }
    // REP(j, 0, N) cout << sa.query(j, j + 1) << " ";
    // cout << endl;
    // REP(j, 0, N) cout << sb.query(j, j + 1) << " ";
    // cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
#define INF (1LL << 60)
using namespace std;
typedef long long ll;

ll N, A, B, Q, X, Y, Z;

template <typename T> class SegmentTree {
public:
  ll n;
  T e;
  T (*op)(T, T);
  vector<T> dat;

  SegmentTree(T _e, T (*_op)(T, T), ll _n) {
    e = _e;
    op = _op;
    n = 1;
    while (n < _n)
      n *= 2;
    REP(i, 0, 2 * n - 1) dat.push_back(e);
  }

  T query(ll a, ll b) { return rquery(a, b, 0, 0, n); };

  void update(ll k, T a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = op(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

private:
  T rquery(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l)
      return e;
    if (a <= l && r <= b)
      return dat[k];
    T vl = rquery(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = rquery(a, b, k * 2 + 2, (l + r) / 2, r);
    return op(vl, vr);
  }
};

ll opmin(ll a, ll b) { return min(a, b); };

int main(void) {
  SegmentTree<ll> sa(INF, opmin, 200000), sb(INF, opmin, 200000);

  cin >> N;
  REP(i, 0, N) {
    cin >> A;
    sa.update(i, A);
    // cout << A << endl;
  }
  REP(i, 0, N) {
    cin >> B;
    sb.update(i, B);
    // cout << B << endl;
  }
  cin >> Q;

  // REP(i, 0, N) cout << sa.dat[i] << " ";
  // cout << endl;
  // REP(i, 0, N) cout << sb.dat[i] << " ";
  // cout << endl;

  set<ll> ua, ub;
  REP(i, 0, N) ua.insert(i), ub.insert(i);

  // REP(j, 0, N) cout << sa.query(j, j + 1) << " ";
  // cout << endl;
  // REP(j, 0, N) cout << sb.query(j, j + 1) << " ";
  // cout << endl;

  REP(i, 0, Q) {
    cin >> X >> Y >> Z;
    // cout << "  " << X << " " << Y << " " << Z << endl;
    if (X == 1) { // update A[Y] = Z
      sa.update(Y - 1, Z);
      ua.insert(Y);
    }
    if (X == 2) { // update B[Y] = Z
      sb.update(Y - 1, Z);
      ub.insert(Y);
    }
    if (X == 3) { // max(A[Y], ..., A[Z])
      cout << sa.query(Y - 1, Z) << endl;
      ;
    }
    if (X == 4) { // max(B[Y], ..., B[Z])
      cout << sb.query(Y - 1, Z) << endl;
    }
    if (X == 5) { // copy A <- B
      for (ll j : ua)
        sa.update(j, sb.query(j, j + 1));
      for (ll j : ub)
        sa.update(j, sb.query(j, j + 1));
      ua.clear();
      ub.clear();
    }
    if (X == 6) { // copy B <- A
      for (ll j : ua)
        sb.update(j, sa.query(j, j + 1));
      for (ll j : ub)
        sb.update(j, sa.query(j, j + 1));
      ua.clear();
      ub.clear();
    }
    // REP(j, 0, N) cout << sa.query(j, j + 1) << " ";
    // cout << endl;
    // REP(j, 0, N) cout << sb.query(j, j + 1) << " ";
    // cout << endl;
  }
}