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

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int segtree[12000000];
int n, m;
int H;
/*int query(int a,int b,int c,int d,int e,int f){
        if(d<a||b<c)return 0;
        if(c<=a&&b<=d)return segtree[f][e-1];
        return max(query(a,(a+b)/2,c,d,e*2,f),query((a+b)/2+1,b,c,d,e*2+1,f));
}*/
int query2(int a, int b, int c, int d, int e, int f, int g, int h, int i,
           int j) {
  if (d < a || b < c || h < e || f < g)
    return 0;
  if (c <= a && b <= d) {
    if (g <= e && f <= h)
      return segtree[(i - 1) * H + j - 1];
    return max(query2(a, b, c, d, e, (e + f) / 2, g, h, i, j * 2),
               query2(a, b, c, d, (e + f) / 2 + 1, f, g, h, i, j * 2 + 1));
  }
  return max(query2(a, (a + b) / 2, c, d, e, f, g, h, i * 2, j),
             query2((a + b) / 2 + 1, b, c, d, e, f, g, h, i * 2 + 1, j));
}
/*void update(int a,int b,int c){
        b+=m;
        //while(a){
                int d=b;
                while(d){
                        segtree[a][d-1]=max(segtree[a][d-1],c);
                        d/=2;
                }
        //	a/=2;
        //}
}*/
void update2(int a, int b, int c) {
  a += n;
  b += m;
  while (a) {
    int d = b;
    while (d) {
      segtree[(a - 1) * H + d - 1] = max(segtree[(a - 1) * H + d - 1], c);
      d /= 2;
    }
    a /= 2;
  }
}
int main() {
  int a, b, c;
  while (scanf("%d%d%d", &a, &b, &c), a) {
    n = 1;
    while (n < a)
      n *= 2;
    m = 1;
    while (m < b)
      m *= 2;
    H = m + b - 1;
    /*	if((n+a-1)*(b+m-1)>1200000){
                    //segtree=new int*[a];
                    //for(int i=0;i<a;i++){
                            segtree[i]=new int[m+b-1];
                            for(int j=0;j<m+b-1;j++)segtree[i][j]=0;
                    }
            //	segtree=vector<vector<int> >(n+a,vector<int>(m+b,2147483647));
                    for(int i=0;i<a;i++)for(int j=0;j<b;j++){
                            int d;scanf("%d",&d);
                            update(i,j,2147483647-d);
                    }
                    for(int i=0;i<c;i++){
                            int p,q,r,s;
                            scanf("%d%d%d%d",&p,&q,&r,&s);
                            int ret=0;
                            for(int
       j=p;j<=r;j++)ret=max(ret,query(0,m-1,q,s,1,j));
                            printf("%d\\n",2147483647-ret);
                    }
                    for(int i=0;i<a;i++)delete[] segtree[i];
                    delete[] segtree;
            }else{*/
    //	segtree=new int*[n+a-1];
    //	for(int i=0;i<n+a-1;i++){
    //		segtree[i]=new int[m+b-1];
    //		for(int j=0;j<m+b-1;j++)segtree[i][j]=0;
    //	}
    //  segtree=vector<vector<int> >(n+a,vector<int>(m+b,2147483647));
    for (int i = 0; i < (n + a - 1) * (m + b - 1); i++)
      segtree[i] = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++) {
        int d;
        scanf("%d", &d);
        update2(i, j, 2147483647 - d);
      }
    for (int i = 0; i < c; i++) {
      int p, q, r, s;
      scanf("%d%d%d%d", &p, &q, &r, &s);
      printf("%d\\n", 2147483647 - query2(0, n - 1, p, r, 0, m - 1, q, s, 1, 1));
    }
    //	for(int i=0;i<n+a-1;i++)delete[] segtree[i];
    //	delete[] segtree;
    //}
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int **segtree;
int n, m;
int query(int a, int b, int c, int d, int e, int f) {
  if (d < a || b < c)
    return 0;
  if (c <= a && b <= d)
    return segtree[f][e - 1];
  return max(query(a, (a + b) / 2, c, d, e * 2, f),
             query((a + b) / 2 + 1, b, c, d, e * 2 + 1, f));
}
int query2(int a, int b, int c, int d, int e, int f, int g, int h, int i,
           int j) {
  if (d < a || b < c || h < e || f < g)
    return 0;
  if (c <= a && b <= d) {
    if (g <= e && f <= h)
      return segtree[i - 1][j - 1];
    return max(query2(a, b, c, d, e, (e + f) / 2, g, h, i, j * 2),
               query2(a, b, c, d, (e + f) / 2 + 1, f, g, h, i, j * 2 + 1));
  }
  return max(query2(a, (a + b) / 2, c, d, e, f, g, h, i * 2, j),
             query2((a + b) / 2 + 1, b, c, d, e, f, g, h, i * 2 + 1, j));
}
void update(int a, int b, int c) {
  b += m;
  // while(a){
  int d = b;
  while (d) {
    segtree[a][d - 1] = max(segtree[a][d - 1], c);
    d /= 2;
  }
  //	a/=2;
  //}
}
void update2(int a, int b, int c) {
  a += n;
  b += m;
  while (a) {
    int d = b;
    while (d) {
      segtree[a - 1][d - 1] = max(segtree[a - 1][d - 1], c);
      d /= 2;
    }
    a /= 2;
  }
}
int main() {
  int a, b, c;
  while (scanf("%d%d%d", &a, &b, &c), a) {
    n = 1;
    while (n < a)
      n *= 2;
    m = 1;
    while (m < b)
      m *= 2;
    if ((n + a - 1) * (b + m - 1) > 6300000) {
      segtree = new int *[a];
      for (int i = 0; i < a; i++) {
        segtree[i] = new int[m + b - 1];
        for (int j = 0; j < m + b - 1; j++)
          segtree[i][j] = 0;
      }
      //	segtree=vector<vector<int> >(n+a,vector<int>(m+b,2147483647));
      for (int i = 0; i < a; i++)
        for (int j = 0; j < b; j++) {
          int d;
          scanf("%d", &d);
          update(i, j, 2147483647 - d);
        }
      for (int i = 0; i < c; i++) {
        int p, q, r, s;
        scanf("%d%d%d%d", &p, &q, &r, &s);
        int ret = 0;
        for (int j = p; j <= r; j++)
          ret = max(ret, query(0, m - 1, q, s, 1, j));
        printf("%d\\n", 2147483647 - ret);
      }
      for (int i = 0; i < a; i++)
        delete[] segtree[i];
      delete[] segtree;
    } else {
      segtree = new int *[n + a - 1];
      for (int i = 0; i < n + a - 1; i++) {
        segtree[i] = new int[m + b - 1];
        for (int j = 0; j < m + b - 1; j++)
          segtree[i][j] = 0;
      }
      //  segtree=vector<vector<int> >(n+a,vector<int>(m+b,2147483647));
      for (int i = 0; i < a; i++)
        for (int j = 0; j < b; j++) {
          int d;
          scanf("%d", &d);
          update2(i, j, 2147483647 - d);
        }
      for (int i = 0; i < c; i++) {
        int p, q, r, s;
        scanf("%d%d%d%d", &p, &q, &r, &s);
        printf("%d\\n",
               2147483647 - query2(0, n - 1, p, r, 0, m - 1, q, s, 1, 1));
      }
      for (int i = 0; i < n + a - 1; i++)
        delete[] segtree[i];
      delete[] segtree;
    }
  }
}