Problem: Company "Robots industries" produces robots for territory protection. Robots protect triangle territories — right isosceles triangles with catheti parallel to North-South and East-West directions.

Owner of some land buys and sets robots on his territory to protect it. From time to time, businessmen want to build offices on that land and want to know how many robots will guard it. You are to handle these queries. 

Input

The first line contains integer N — width and height of the land, and integer Q — number of queries to handle.

Next Q lines contain queries you need to process.

Two types of queries: 

  1. 1 dir x y len — add a robot to protect a triangle. Depending on the value of dir, the values of x, y and len represent a different triangle: 
    * dir = 1: Triangle is defined by the points (x, y), (x + len, y), (x, y + len)
    * dir = 2: Triangle is defined by the points (x, y), (x + len, y), (x, y - len)
    * dir = 3: Triangle is defined by the points (x, y), (x - len, y), (x, y + len)
    * dir = 4: Triangle is defined by the points (x, y), (x - len, y), (x, y - len)
  2. 2 x y — output how many robots guard this point (robot guards a point if the point is inside or on the border of its triangle) 


  * 1 ≤ N ≤ 5000
  * 1 ≤ Q ≤ 105
  * 1 ≤ dir ≤ 4
  * All points of triangles are within range [1, N]
  * All numbers are positive integers 

Output

For each second type query output how many robots guard this point. Each answer should be in a separate line.

Examples

Input

17 10
1 1 3 2 4
1 3 10 3 7
1 2 6 8 2
1 3 9 4 2
2 4 4
1 4 15 10 6
2 7 7
2 9 4
2 12 2
2 13 8


Output

2
2
2
0
1

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int n, q, tot;
struct node {
  int x, l, r, t;
} nm[N];
int c[N], tp[N], x[N], y[N], z[N], out[N], t[N];
bool cmpt(node a, node b) { return abs(a.t) < abs(b.t); }
bool cmpx(node a, node b) {
  if (a.x == b.x) return a.t > b.t;
  return a.x < b.x;
}
bool cmpn(node a, node b) {
  if (a.x == b.x) return a.t > b.t;
  return a.x > b.x;
}
inline void get(int &a) {
  char c = getchar();
  a = 0;
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar()) a = a * 10 + c - '0';
}
inline int lowbit(int i) { return i & -i; }
inline void add(int a, int b) {
  for (int i = a; i <= n; i += lowbit(i)) t[i] += b;
}
inline int query(int a) {
  int ans = 0;
  for (int i = a; i > 0; i -= lowbit(i)) ans += t[i];
  return ans;
}
void workxx(int l, int r) {
  if (l >= r) return;
  int mid = (l + r) / 2;
  workxx(l, mid);
  workxx(mid + 1, r);
  sort(nm + l, nm + 1 + mid, cmpx);
  sort(nm + mid + 1, nm + 1 + r, cmpx);
  int i = 0, j = 0;
  for (i = l, j = mid + 1; j <= r; j++) {
    while (i <= mid && nm[i].x <= nm[j].x) {
      if (nm[i].t > 0) {
        add(nm[i].l, 1);
        add(nm[i].r + 1, -1);
      }
      i++;
    }
    if (nm[j].t < 0) out[-nm[j].t] += query(nm[j].l);
  }
  for (int j = l; j < i; j++)
    if (nm[j].t > 0) {
      add(nm[j].l, -1);
      add(nm[j].r + 1, 1);
    }
}
void workxy(int l, int r) {
  if (l >= r) return;
  int mid = (l + r) / 2;
  workxy(l, mid);
  workxy(mid + 1, r);
  sort(nm + l, nm + 1 + mid, cmpx);
  sort(nm + mid + 1, nm + 1 + r, cmpx);
  int i = 0, j = 0;
  for (i = l, j = mid + 1; j <= r; j++) {
    while (i <= mid && nm[i].x <= nm[j].x) {
      if (nm[i].t > 0) {
        add(nm[i].l, 1);
        add(nm[i].r + 1, -1);
      }
      i++;
    }
    if (nm[j].t < 0) out[-nm[j].t] -= query(nm[j].l);
  }
  for (int j = l; j < i; j++)
    if (nm[j].t > 0) {
      add(nm[j].l, -1);
      add(nm[j].r + 1, 1);
    }
}
void workyx(int l, int r) {
  if (l >= r) return;
  int mid = (l + r) / 2;
  workyx(l, mid);
  workyx(mid + 1, r);
  sort(nm + l, nm + 1 + mid, cmpn);
  sort(nm + mid + 1, nm + 1 + r, cmpn);
  int i = 0, j = 0;
  for (i = l, j = mid + 1; j <= r; j++) {
    while (i <= mid && nm[i].x >= nm[j].x) {
      if (nm[i].t > 0) {
        add(nm[i].l, 1);
        add(nm[i].r + 1, -1);
      }
      i++;
    }
    if (nm[j].t < 0) out[-nm[j].t] += query(nm[j].l);
  }
  for (int j = l; j < i; j++)
    if (nm[j].t > 0) {
      add(nm[j].l, -1);
      add(nm[j].r + 1, 1);
    }
}
void workyy(int l, int r) {
  if (l >= r) return;
  int mid = (l + r) / 2;
  workyy(l, mid);
  workyy(mid + 1, r);
  sort(nm + l, nm + 1 + mid, cmpn);
  sort(nm + mid + 1, nm + 1 + r, cmpn);
  int i = 0, j = 0;
  for (i = l, j = mid + 1; j <= r; j++) {
    while (i <= mid && nm[i].x >= nm[j].x) {
      if (nm[i].t > 0) {
        add(nm[i].l, 1);
        add(nm[i].r + 1, -1);
      }
      i++;
    }
    if (nm[j].t < 0) out[-nm[j].t] -= query(nm[j].l);
  }
  for (int j = l; j < i; j++)
    if (nm[j].t > 0) {
      add(nm[j].l, -1);
      add(nm[j].r + 1, 1);
    }
}
int main() {
  get(n);
  get(q);
  for (int i = 1; i <= q; i++) {
    get(c[i]);
    if (c[i] == 1) {
      get(tp[i]);
      get(x[i]);
      get(y[i]);
      get(z[i]);
    } else {
      get(x[i]);
      get(y[i]);
    }
  }
  tot = 0;
  for (int i = 1; i <= q; i++) {
    if (c[i] == 1 && tp[i] == 1)
      nm[++tot] = (node){y[i], x[i], x[i] + z[i], i};
    else if (c[i] == 2)
      nm[++tot] = (node){y[i], x[i], x[i], -i};
  }
  sort(nm + 1, nm + 1 + tot, cmpt);
  workxx(1, tot);
  tot = 0;
  for (int i = 1; i <= q; i++) {
    if (c[i] == 1 && tp[i] == 1)
      nm[++tot] = (node){x[i] + y[i] + z[i] + 1, x[i], x[i] + z[i], i};
    else if (c[i] == 2)
      nm[++tot] = (node){x[i] + y[i], x[i], x[i], -i};
  }
  sort(nm + 1, nm + 1 + tot, cmpt);
  workxy(1, tot);
  tot = 0;
  for (int i = 1; i <= q; i++) {
    if (c[i] == 1 && tp[i] == 2)
      nm[++tot] = (node){y[i], x[i], x[i] + z[i], i};
    else if (c[i] == 2)
      nm[++tot] = (node){y[i], x[i], x[i], -i};
  }
  sort(nm + 1, nm + 1 + tot, cmpt);
  workyx(1, tot);
  tot = 0;
  for (int i = 1; i <= q; i++) {
    if (c[i] == 1 && tp[i] == 2)
      nm[++tot] = (node){y[i] - x[i] - z[i] - 1, x[i], x[i] + z[i], i};
    else if (c[i] == 2)
      nm[++tot] = (node){y[i] - x[i], x[i], x[i], -i};
  }
  sort(nm + 1, nm + 1 + tot, cmpt);
  workyy(1, tot);
  tot = 0;
  for (int i = 1; i <= q; i++) {
    if (c[i] == 1 && tp[i] == 3)
      nm[++tot] = (node){y[i], x[i] - z[i], x[i], i};
    else if (c[i] == 2)
      nm[++tot] = (node){y[i], x[i], x[i], -i};
  }
  sort(nm + 1, nm + 1 + tot, cmpt);
  workxx(1, tot);
  tot = 0;
  for (int i = 1; i <= q; i++) {
    if (c[i] == 1 && tp[i] == 3)
      nm[++tot] = (node){y[i] - x[i] + z[i] + 1, x[i] - z[i], x[i], i};
    else if (c[i] == 2)
      nm[++tot] = (node){y[i] - x[i], x[i], x[i], -i};
  }
  sort(nm + 1, nm + 1 + tot, cmpt);
  workxy(1, tot);
  tot = 0;
  for (int i = 1; i <= q; i++) {
    if (c[i] == 1 && tp[i] == 4)
      nm[++tot] = (node){y[i], x[i] - z[i], x[i], i};
    else if (c[i] == 2)
      nm[++tot] = (node){y[i], x[i], x[i], -i};
  }
  sort(nm + 1, nm + 1 + tot, cmpt);
  workyx(1, tot);
  tot = 0;
  for (int i = 1; i <= q; i++) {
    if (c[i] == 1 && tp[i] == 4)
      nm[++tot] = (node){x[i] + y[i] - z[i] - 1, x[i] - z[i], x[i], i};
    else if (c[i] == 2)
      nm[++tot] = (node){x[i] + y[i], x[i], x[i], -i};
  }
  sort(nm + 1, nm + 1 + tot, cmpt);
  workyy(1, tot);
  for (int i = 1; i <= q; i++)
    if (c[i] == 2) printf("%d\n", out[i]);
  return 0;
}