PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000157. Các trạm phát sóng vô tuyến (Radio Stations)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Các trạm phát sóng vô tuyến (Radio Stations)

Radio Stations

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn radio stations at distinct integer positions xix_i, with broadcast ranges rir_i and frequencies fif_i. Stations i,ji,j can reach each other exactly when ∣xi−xj∣≤min⁡(ri,rj)|x_i-x_j|\le\min(r_i,r_j), so the distance must fit both ranges. An unordered pair is bad when the stations can reach each other and ∣fi−fj∣≤k|f_i-f_j|\le k. Count such unordered pairs once each; one-way reachability is not sufficient.

Input

The first line contains n,kn,k. The next nn lines contain xi,ri,fix_i,r_i,f_i.

Output

Print a 64-bit integer: the number of bad station pairs.

Subtasks

  • Subtask 1 (20 points): 1≤n≤151\le n\le 15; 0≤k≤100\le k\le10; 1≤xi,ri≤1091\le x_i,r_i\le10^9; 1≤fi≤1041\le f_i\le10^4; all station coordinates xix_i are distinct.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le 3000; 0≤k≤100\le k\le10; 1≤xi,ri≤1091\le x_i,r_i\le10^9; 1≤fi≤1041\le f_i\le10^4; all station coordinates xix_i are distinct.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 0≤k≤100\le k\le10; 1≤xi,ri≤1091\le x_i,r_i\le10^9; 1≤fi≤1041\le f_i\le10^4; all station coordinates xix_i are distinct.

Examples

Example 1

Input

3 2
1 3 10
3 2 5
4 10 8

Output

1

Explanation

Stations 1 and 3 are three units apart, within both ranges 3 and 10, and their frequency difference is ∣10−8∣=2≤k|10-8|=2\le k. The other pairs differ in frequency by 5 and 3, both above k=2k=2.

Example 2

Input

3 3
1 3 10
3 2 5
4 10 8

Output

2

Explanation

Raising kk to 3 adds pair 2–3: distance 1 is covered by both ranges and frequency difference is 3. Pair 1–3 still qualifies while 1–2 fails with frequency difference 5.