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

#include <bits/stdc++.h>

using namespace std;

template <typename T> struct SegmentTree {
  const T INF;

  vector<T> seg;
  int sz;

  SegmentTree(int n) : INF(numeric_limits<T>::max()) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz - 1, INF);
  }

  T merge(T a, T b) { return (min(a, b)); }

  void set(int k, int x) { seg[k + sz - 1] = x; }

  void build() {
    for (int k = sz - 2; k >= 0; k--) {
      seg[k] = merge(seg[2 * k + 1], seg[2 * k + 2]);
    }
  }

  T rmq(int a, int b, int k, int l, int r) {
    if (a >= r || b <= l)
      return (INF);
    if (a <= l && r <= b)
      return (seg[k]);
    return (merge(rmq(a, b, 2 * k + 1, l, (l + r) >> 1),
                  rmq(a, b, 2 * k + 2, (l + r) >> 1, r)));
  }

  T rmq(int a, int b) { return (rmq(a, b, 0, 0, sz)); }

  void update(int k, T x) {
    k += sz - 1;
    seg[k] = x;
    while (k > 0) {
      k = (k - 1) >> 1;
      seg[k] = merge(seg[2 * k + 1], seg[2 * k + 2]);
    }
  }
};

int main() {
  int N, A[200000], B[200000];

  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &A[i]);
  for (int i = 0; i < N; i++)
    scanf("%d", &B[i]);
  SegmentTree<int> seg1(N), seg2(N);
  for (int i = 0; i < N; i++)
    seg1.set(i, A[i]);
  for (int i = 0; i < N; i++)
    seg2.set(i, B[i]);
  seg1.build();
  seg2.build();

  int Q;
  scanf("%d", &Q);
  bool first = false;
  vector<pair<int, int>> aa, bb, cc, dd;
  while (Q--) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    --b;
    if (a == 1)
      cc.emplace_back(b, seg1.rmq(b, b + 1)), seg1.update(b, c),
          aa.emplace_back(b, c);
    if (a == 2)
      dd.emplace_back(b, seg2.rmq(b, b + 1)), seg2.update(b, c),
          bb.emplace_back(b, c);
    if (a == 3)
      printf("%d\\n", seg1.rmq(b, c));
    if (a == 4)
      printf("%d\\n", seg2.rmq(b, c));
    if (a == 5) {
      if (!first) {
        for (int i = 0; i < N; i++)
          seg1.update(i, seg2.rmq(i, i + 1));
      } else {
        reverse(begin(cc), end(cc));
        for (auto &p : cc)
          seg1.update(p.first, p.second);
        for (auto &p : bb)
          seg1.update(p.first, p.second);
      }
      first = true;
      aa.clear();
      bb.clear();
      cc.clear();
      dd.clear();
    }
    if (a == 6) {
      if (!first) {
        for (int i = 0; i < N; i++)
          seg2.update(i, seg1.rmq(i, i + 1));
      } else {
        reverse(begin(dd), end(dd));
        for (auto &p : dd)
          seg2.update(p.first, p.second);
        for (auto &p : aa)
          seg2.update(p.first, p.second);
      }
      first = true;
      aa.clear();
      bb.clear();
      cc.clear();
      dd.clear();
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

template <typename T> struct SegmentTree {
  const T INF;

  vector<T> seg;
  int sz;

  SegmentTree(int n) : INF(numeric_limits<T>::max()) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz - 1, INF);
  }

  T merge(T a, T b) { return (min(a, b)); }

  void set(int k, int x) { seg[k + sz - 1] = x; }

  void build() {
    for (int k = sz - 2; k >= 0; k--) {
      seg[k] = merge(seg[2 * k + 1], seg[2 * k + 2]);
    }
  }

  T rmq(int a, int b, int k, int l, int r) {
    if (a >= r || b <= l)
      return (INF);
    if (a <= l && r <= b)
      return (seg[k]);
    return (merge(rmq(a, b, 2 * k + 1, l, (l + r) >> 1),
                  rmq(a, b, 2 * k + 2, (l + r) >> 1, r)));
  }

  T rmq(int a, int b) { return (rmq(a, b, 0, 0, sz)); }

  void update(int k, T x) {
    k += sz - 1;
    seg[k] = x;
    while (k > 0) {
      k = (k - 1) >> 1;
      seg[k] = merge(seg[2 * k + 1], seg[2 * k + 2]);
    }
  }
};

int main() {
  int N, A[200000], B[200000];

  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &A[i]);
  for (int i = 0; i < N; i++)
    scanf("%d", &B[i]);
  SegmentTree<int> seg1(N), seg2(N);
  for (int i = 0; i < N; i++)
    seg1.set(i, A[i]);
  for (int i = 0; i < N; i++)
    seg2.set(i, B[i]);
  seg1.build();
  seg2.build();

  int Q;
  scanf("%d", &Q);
  bool first = false;
  vector<pair<int, int>> aa, bb;
  while (Q--) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    --b;
    if (a == 1)
      seg1.update(b, c), aa.emplace_back(b, c);
    if (a == 2)
      seg2.update(b, c), bb.emplace_back(b, c);
    if (a == 3)
      printf("%d\\n", seg1.rmq(b, c));
    if (a == 4)
      printf("%d\\n", seg2.rmq(b, c));
    if (a == 5) {
      if (!first) {
        for (int i = 0; i < N; i++)
          seg1.update(i, seg2.rmq(i, i + 1));
      } else {
        for (auto &p : bb)
          seg1.update(p.first, p.second);
      }
      // first = true;
      aa.clear();
      bb.clear();
    }
    if (a == 6) {
      if (!first) {
        for (int i = 0; i < N; i++)
          seg2.update(i, seg1.rmq(i, i + 1));
      } else {
        for (auto &p : aa)
          seg2.update(p.first, p.second);
      }
      // first = true;
      aa.clear();
      bb.clear();
    }
  }
}