#include <bits/stdc++.h>
#define pb emplace_back
#define SZ(x) ((int)x.size())
#define fi first
#define se second
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)
template<typename T>
void __prine_one(const char *&s, const T &x)
{
while (*s == ' ') ++s;
const char *p = s;
int bal = 0;
while (*s)
{
if (*s == '(') ++bal;
else if (*s == ')') --bal;
else if (*s == ',' && bal == 0) break;
++s;
}
cerr.write(p, s - p) << " = " << x;
if (*s == ',')
{
cerr << " , ";
++s;
}
}
template<typename... Args>
void debug(const char *s, Args... args)
{
cerr << "[ ";
int dummy[] = {0, (__prine_one(s, args), 0)...};
(void)dummy;
cerr << " ]\n\n";
}
template<class X>
bool maximize(X &a, const X &b)
{
if (b > a)
{
a = b;
return true;
}
return false;
}
template<class X>
bool minimize(X &a, const X &b)
{
if (b < a)
{
a = b;
return true;
}
return false;
}
#define inline inline __attribute__ ((always_inline))
// --------------------------------------------------------------------------------------------
constexpr int maxn = 1e5 + 3;
int n, B, M;
ll D;
// --------------------------------------------------------------------------------------------
void readInput()
{
cin >> B >> n >> D >> M;
}
namespace sub1
{
int a[maxn];
ll res;
void solve()
{
FOR(i, 1, n)
cin >> a[i];
sort(a + 1, a + n + 1);
int j = 1;
FOR(i, 1, n)
{
while (j <= i && a[i] - a[j] > D)
++j;
res += i - j;
}
cout << res;
}
}
namespace sub2
{
ll res;
struct Point
{
ll x, y;
bool operator < (const Point &other) const
{
if (x != other.x) return x < other.x;
return y < other.y;
}
} a[maxn];
vector<int> idsy;
int lower(ll x) {return lower_bound(all(idsy), x) - idsy.begin() + 1;}
int upper(ll x) {return upper_bound(all(idsy), x) - idsy.begin() + 1;}
struct FenTree
{
vector<int> fw;
void init(int n)
{
fw.assign(n + 3, 0);
}
void upd(int pos, int val)
{
for (; pos <= SZ(fw); pos += pos & -pos)
fw[pos] += val;
}
int get(int pos)
{
int res = 0;
for (; pos; pos &= pos - 1)
res += fw[pos];
return res;
}
int get(int l, int r)
{
if (l > r) return 0;
return get(r) - get(l - 1);
}
} fw;
void solve()
{
FOR(i, 1, n)
{
ll curx, cury; cin >> curx >> cury;
a[i].x = curx + cury;
a[i].y = curx - cury;
idsy.push_back(a[i].y);
}
sort(a + 1, a + n + 1);
uni(idsy);
fw.init(SZ(idsy));
int j = 1;
FOR(i, 1, n)
{
while (a[i].x - a[j].x > D)
{
fw.upd(lower(a[j].y), -1);
++j;
}
res += fw.get(lower(a[i].y - D), upper(a[i].y + D) - 1);
fw.upd(lower(a[i].y), 1);
}
cout << res;
}
}
namespace sub3
{
ll res;
struct Point
{
ll x, y, z;
} a[maxn];
int pre[36 + 67][83 * 2][86 * 2];
int get(int z, int x, int y, int u, int v)
{
maximize(x, 1); maximize(y, 1);
minimize(u, 2 * M); minimize(v, 2 * M);
return pre[z][u][v] - pre[z][u][y - 1] - pre[z][x - 1][v] + pre[z][x - 1][y - 1];
}
void solve()
{
FOR(i, 1, n)
{
cin >> a[i].x >> a[i].y >> a[i].z;
pre[a[i].z][a[i].x + a[i].y][a[i].x - a[i].y + M]++;
a[i] = {a[i].x + a[i].y, a[i].x - a[i].y + M, a[i].z};
}
FOR(z, 1, M)
{
FOR(x, 1, 2 * M)
FOR(y, 1, 2 * M)
pre[z][x][y] += pre[z][x - 1][y] + pre[z][x][y - 1] - pre[z][x - 1][y - 1];
}
FOR(i, 1, n)
{
FOR(z, max(1ll, a[i].z - D), min(1ll * M, a[i].z + D))
{
int d = abs(z - a[i].z);
res += get(z, a[i].x - D + d, a[i].y - D + d, a[i].x + D - d, a[i].y + D - d);
}
}
res -= n;
res >>= 1;
cout << res;
}
}
void solve()
{
if (B == 1) return sub1 :: solve(), void();
if (B == 2) return sub2 :: solve(), void();
sub3 :: solve();
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define TASK "TEST"
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
readInput();
solve();
return 0;
}