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

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

#define ll long long
#define inf 1000000007
#define mod 1000000007
#define pa pair<int, int>
#define pal pair<ll, ll>
#define ppa pair<int, pa>
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double a) { return Point(x * a, y * a); }
  Point operator/(double a) { return Point(x / a, y / a); }
  double absv() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};
typedef Point Vector;

struct Segment {
  Point p1, p2;
};

double hen(Vector a) {
  if (fabs(a.x) < EPS && a.y > 0)
    return acos(0);
  else if (fabs(a.x) < EPS && a.y < 0)
    return 3 * acos(0);
  else if (fabs(a.y) < EPS && a.x < 0)
    return 2 * acos(0);
  else if (fabs(a.y) < EPS && a.x > 0)
    return 0.0;
  else if (a.y > 0)
    return acos(a.x / a.absv());
  else
    return 2 * acos(0) + acos(-a.x / a.absv());
}

double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ru[11] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};

ll frac[100010];
ll inv[100010];

ll saikif(ll a, int n) {
  if (n == 1)
    return a % mod;
  else if (n % 2 == 0) {
    ll q = saikif(a, n / 2);
    return (q * q) % mod;
  } else
    return (a * saikif(a, n - 1)) % mod;
}

void gyakugen() {
  frac[0] = 1;
  for (int i = 1; i <= 100000; i++)
    frac[i] = (frac[i - 1] * i) % mod;
  for (int i = 0; i <= 100000; i++)
    inv[i] = saikif(frac[i], mod - 2);
}

//----------------kokomade temple------------

int a[10010][1270] = {0};
// int b[10010][1270]={0};
int main() {
  int n, m;

  while (cin >> n >> m) {
    int t, nn, mm, s;
    fill(a[0], a[10010], 0);
    // fill(b[0],b[10010],0);
    if (n == 0 && m == 0)
      break;
    int r, q;
    cin >> r;
    for (int i = 0; i < r; i++) {
      cin >> t >> nn >> mm >> s;
      if (s == 1) {
        a[mm][t]++;
        //  cout<<mm<<" in "<<t<<endl;
      } else {
        a[mm][t]--;
        //  cout<<mm<<" out "<<t<<endl;
      }
    }

    for (int i = 1; i <= m; i++) {
      for (int j = 1; j < 1265; j++) {
        a[i][j] += a[i][j - 1];
      }

      for (int j = 1; j < 1265; j++) {
        if (a[i][j] > 0)
          a[i][j] = 1;
      }
      for (int j = 1; j < 1265; j++) {
        a[i][j] = a[i][j - 1] + a[i][j];
      }
    }

    cin >> q;
    int ts, te, mm1;
    for (int i = 0; i < q; i++) {
      cin >> ts >> te >> mm1;
      cout << a[mm1][te - 1] - a[mm1][ts - 1] << endl;
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

#define ll long long
#define inf 1000000007
#define mod 1000000007
#define pa pair<int, int>
#define pal pair<ll, ll>
#define ppa pair<int, pa>
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double a) { return Point(x * a, y * a); }
  Point operator/(double a) { return Point(x / a, y / a); }
  double absv() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};
typedef Point Vector;

struct Segment {
  Point p1, p2;
};

double hen(Vector a) {
  if (fabs(a.x) < EPS && a.y > 0)
    return acos(0);
  else if (fabs(a.x) < EPS && a.y < 0)
    return 3 * acos(0);
  else if (fabs(a.y) < EPS && a.x < 0)
    return 2 * acos(0);
  else if (fabs(a.y) < EPS && a.x > 0)
    return 0.0;
  else if (a.y > 0)
    return acos(a.x / a.absv());
  else
    return 2 * acos(0) + acos(-a.x / a.absv());
}

double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ru[11] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};

ll frac[100010];
ll inv[100010];

ll saikif(ll a, int n) {
  if (n == 1)
    return a % mod;
  else if (n % 2 == 0) {
    ll q = saikif(a, n / 2);
    return (q * q) % mod;
  } else
    return (a * saikif(a, n - 1)) % mod;
}

void gyakugen() {
  frac[0] = 1;
  for (int i = 1; i <= 100000; i++)
    frac[i] = (frac[i - 1] * i) % mod;
  for (int i = 0; i <= 100000; i++)
    inv[i] = saikif(frac[i], mod - 2);
}

//----------------kokomade temple------------

int a[10010][1270] = {0};
int b[10010][1270] = {0};
int main() {
  int n, m;

  while (cin >> n >> m) {
    int t, nn, mm, s;
    fill(a[0], a[10010], 0);
    fill(b[0], b[10010], 0);
    if (n == 0 && m == 0)
      break;
    int r, q;
    cin >> r;
    for (int i = 0; i < r; i++) {
      cin >> t >> nn >> mm >> s;
      if (s == 1) {
        a[mm][t]++;
        //  cout<<mm<<" in "<<t<<endl;
      } else {
        a[mm][t]--;
        //  cout<<mm<<" out "<<t<<endl;
      }
    }

    for (int i = 1; i <= m; i++) {
      for (int j = 1; j < 1265; j++) {
        a[i][j] += a[i][j - 1];
      }

      for (int j = 1; j < 1265; j++) {
        if (a[i][j] > 0)
          a[i][j] = 1;
      }
      for (int j = 1; j < 1265; j++) {
        b[i][j] = b[i][j - 1] + a[i][j];
      }
    }

    cin >> q;
    int ts, te, mm1;
    for (int i = 0; i < q; i++) {
      cin >> ts >> te >> mm1;
      cout << b[mm1][te - 1] - b[mm1][ts - 1] << endl;
    }
  }

  return 0;
}