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

#include <algorithm>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<int, int> Pi;

const int INF = 100000000;
const long long int INF_ = 100000000000000000;

lli Cmin(const lli a, const lli b) {
  if (a < b)
    return a;
  else
    return b;
}

class SegmentTree {
private:
  static const int MAX_N = 1 << 25;
  static lli dat[2 * MAX_N - 1];

public:
  int n;
  void Init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; ++i)
      dat[i] = INF_;
  }
  void Update(int k, lli a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = Cmin(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  lli Query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF_;
    if (a <= l && r <= b)
      return dat[k];
    else {
      lli vl = Query(a, b, k * 2 + 1, l, (l + r) / 2);
      lli vr = Query(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
  int GetData(const int k) { return dat[k]; }
};

lli SegmentTree::dat[2 * MAX_N - 1];

class SegmentTree_ {
private:
  static const int MAX_N = 1 << 25;
  static lli dat[2 * MAX_N - 1];

public:
  int n;
  void Init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; ++i)
      dat[i] = INF_;
  }
  void Update(int k, lli a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = Cmin(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  lli Query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF_;
    if (a <= l && r <= b)
      return dat[k];
    else {
      lli vl = Query(a, b, k * 2 + 1, l, (l + r) / 2);
      lli vr = Query(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
  int GetData(const int k) { return dat[k]; }
};

lli SegmentTree_::dat[2 * MAX_N - 1];

int Serialize(const int y, const int x, const int w) { return x + y * w; }

int Serialize_(const int y, const int x, const int h) { return y + x * h; }

int solve() {
  int R, C, Q;
  cin >> R >> C >> Q;
  if (R == 0 && C == 0 && Q == 0)
    return 0;
  SegmentTree st;
  SegmentTree_ st_;
  st.Init(R * C);
  st_.Init(R * C);
  REP(y, 0, R) REP(x, 0, C) {
    lli tmp;
    cin >> tmp;
    st.Update(Serialize(y, x, C), tmp);
    st_.Update(Serialize_(y, x, R), tmp);
  }
  /*
  REP(i,0,R*C+st.n-1) cout << st.GetData(i) << endl;
  cout << endl;
  */
  REP(i, 0, Q) {
    int r[2], c[2];
    REP(j, 0, 2) cin >> r[j] >> c[j];
    lli ans = INF_;
    if (r[1] - r[0] < c[1] - c[0])
      REP(y, r[0], r[1] + 1)
      ans = Cmin(ans, st.Query(Serialize(y, c[0], C), Serialize(y, c[1] + 1, C),
                               0, 0, st.n));
    else
      REP(x, c[0], c[1] + 1)
      ans = Cmin(ans, st_.Query(Serialize_(r[0], x, R),
                                Serialize_(r[1] + 1, x, R), 0, 0, st_.n));
    cout << ans << endl;
  }
  // REP(i,0,R*C) cout << st.GetData(i) << endl;
  // REP(y,0,R) REP(x,0,C) cout << st.GetData(Serialize(y,x,C)) << endl;
  return 1;
}

int main() {
  while (solve())
    ;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<int, int> Pi;

const int INF = 100000000;
const long long int INF_ = 100000000000000000;

lli Cmin(const lli a, const lli b) {
  if (a < b)
    return a;
  else
    return b;
}

class SegmentTree {
private:
  static const int MAX_N = 1 << 25;
  static lli dat[2 * MAX_N - 1];

public:
  int n;
  void Init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; ++i)
      dat[i] = INF_;
  }
  void Update(int k, lli a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = Cmin(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  lli Query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF_;
    if (a <= l && r <= b)
      return dat[k];
    else {
      lli vl = Query(a, b, k * 2 + 1, l, (l + r) / 2);
      lli vr = Query(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
  int GetData(const int k) { return dat[k]; }
};

lli SegmentTree::dat[2 * MAX_N - 1];

int Serialize(const int y, const int x, const int w) { return x + y * w; }

int solve() {
  int R, C, Q;
  cin >> R >> C >> Q;
  if (R == 0 && C == 0 && Q == 0)
    return 0;
  SegmentTree st;
  st.Init(R * C);
  REP(y, 0, R) REP(x, 0, C) {
    lli tmp;
    cin >> tmp;
    st.Update(Serialize(y, x, C), tmp);
  }
  /*
  REP(i,0,R*C+st.n-1) cout << st.GetData(i) << endl;
  cout << endl;
  */
  REP(i, 0, Q) {
    int r[2], c[2];
    REP(j, 0, 2) cin >> r[j] >> c[j];
    lli ans = INF_;
    REP(y, r[0], r[1] + 1)
    ans = Cmin(ans, st.Query(Serialize(y, c[0], C), Serialize(y, c[1] + 1, C),
                             0, 0, st.n));
    cout << ans << endl;
  }
  // REP(i,0,R*C) cout << st.GetData(i) << endl;
  // REP(y,0,R) REP(x,0,C) cout << st.GetData(Serialize(y,x,C)) << endl;
  return 1;
}

int main() {
  while (solve())
    ;
  return 0;
}