Problem p02122 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
// const int B = 450;
const int B = 1;
const int MAX_S = (200000 / B) + 10;

int N, Q, S;
bool lazy[2][MAX_S];
int A[2][200000 + B];
int M[2][MAX_S];

void push(int k, int b) {
  if (!lazy[k][b])
    return;
  rep(i, B) A[k][b * B + i] = A[k ^ 1][b * B + i];
  lazy[k][b] = false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  S = (N - 1) / B + 1;
  rep(k, 2) {
    rep(i, S) M[k][i] = INF;
    rep(i, N + B) A[k][i] = INF;
    rep(i, N) cin >> A[k][i], M[k][i / B] = min(M[k][i / B], A[k][i]);
  }
  cin >> Q;
  rep(i, Q) {
    int type, y, z;
    cin >> type >> y >> z;
    type--;
    int k = type % 2;
    if (type / 2 == 0) {
      y--;
      int b = y / B;
      push(k, b);
      push(k ^ 1, b);
      A[k][y] = z;
      M[k][b] = INF;
      rep(i, B) M[k][b] = min(M[k][b], A[k][b * B + i]);
    } else if (type / 2 == 1) {
      y--, z--;
      // min [y, z]
      int lb = y / B, rb = z / B, m = INF;
      push(k, lb);
      push(k, rb);
      if (lb == rb) {
        for (int i = y; i <= z; i++)
          m = min(m, A[k][i]);
      } else {
        for (int i = y; i < (lb + 1) * B; i++)
          m = min(m, A[k][i]);
        for (int i = rb * B; i <= z; i++)
          m = min(m, A[k][i]);
        for (int b = lb + 1; b <= rb - 1; b++)
          m = min(m, M[k][b]);
      }
      cout << m << "\n";
    } else {
      rep(b, S) {
        if (!lazy[k ^ 1][b]) {
          lazy[k][b] = true;
          M[k][b] = M[k ^ 1][b];
        }
      }
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
const int B = 450;
const int MAX_S = (200000 / B) + 10;

int N, Q, S;
bool lazy[2][MAX_S];
int A[2][200000 + B];
int M[2][MAX_S];

void push(int k, int b) {
  if (!lazy[k][b])
    return;
  rep(i, B) A[k][b * B + i] = A[k ^ 1][b * B + i];
  lazy[k][b] = false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  S = (N - 1) / B + 1;
  rep(k, 2) {
    rep(i, S) M[k][i] = INF;
    rep(i, N + B) A[k][i] = INF;
    rep(i, N) cin >> A[k][i], M[k][i / B] = min(M[k][i / B], A[k][i]);
  }
  cin >> Q;
  rep(i, Q) {
    int type, y, z;
    cin >> type >> y >> z;
    type--;
    int k = type % 2;
    if (type / 2 == 0) {
      y--;
      int b = y / B;
      push(k, b);
      push(k ^ 1, b);
      A[k][y] = z;
      M[k][b] = INF;
      rep(i, B) M[k][b] = min(M[k][b], A[k][b * B + i]);
    } else if (type / 2 == 1) {
      y--, z--;
      // min [y, z]
      int lb = y / B, rb = z / B, m = INF;
      push(k, lb);
      push(k, rb);
      if (lb == rb) {
        for (int i = y; i <= z; i++)
          m = min(m, A[k][i]);
      } else {
        for (int i = y; i < (lb + 1) * B; i++)
          m = min(m, A[k][i]);
        for (int i = rb * B; i <= z; i++)
          m = min(m, A[k][i]);
        for (int b = lb + 1; b <= rb - 1; b++)
          m = min(m, M[k][b]);
      }
      cout << m << "\n";
    } else {
      rep(b, S) {
        if (!lazy[k ^ 1][b]) {
          lazy[k][b] = true;
          M[k][b] = M[k ^ 1][b];
        }
      }
    }
  }
  return 0;
}