-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCF, Radio Stations, E
200 lines (168 loc) · 4.2 KB
/
CF, Radio Stations, E
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// http://codeforces.com/contest/762/problem/E
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define DO long double
#define pb push_back
#define pfr push_front
#define eb emplace_back
#define ef emplace_front
#define gc getchar_unlocked
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define fr(i,j,k) for(i = j; i < k; i++)
#define bck(i,j,k) for(i = j; i > k; i--)
//////////////////////////////////////////////////////////////////////////////////////////
#define error(args...) { vector<string> _v = split(#args, ','); err(_v.begin(), args); cerr << '\n'; }
vector<string> split(const string& s, char c) {
vector<string> v;
stringstream ss(s);
string x;
while (getline(ss, x, c))
v.emplace_back(x);
return move(v);
}
void err(vector<string>::iterator it) { }
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << '\t';
err(++it, args...);
}
////////////////////////////////////////////////////////////////////////////////////////////
const int NM = 1e5 + 10;
pair<int, pii> radio[NM];
int xx[NM];
vi freq[NM], AR[NM];
int sea_(int lo, int hi, int key)
{
int cnt = 21, m;
while(lo <= hi && cnt--){
m = (lo + hi)/2;
if(xx[m] >= key)
hi = m;
else{
if(xx[m + 1] >= key)
return m + 1;
lo = m + 1;
}
}
return lo;
}
int _sea(int lo, int hi, int key){
if(xx[lo] > key)
return lo - 1;
if(xx[hi] <= key)
return hi;
int cnt = 21, m;
while(lo <= hi && cnt--){
m = (lo + hi)/2;
if(xx[m] <= key)
lo = m;
else{
if(xx[m - 1] <= key)
return m - 1;
hi = m - 1;
}
}
return lo;
}
struct query{
int st, en, key;
};
vector<query> Q[NM]; // query in form of L..R and k
bool comp(query A, query B)
{
return A.key < B.key;
}
void update(int b[], int x, int n)
{
for(; x <= n; x += x&-x)
b[x] += 1;
}
int query(int b[], int x)
{
int sum = 0;
for(; x > 0; x -= x&-x)
sum += b[x];
return sum;
}
ll process(int b){
sort(Q[b].begin(), Q[b].end(), comp);
int i, j, n = freq[b].size();
int bit[n+1];
fill(bit, bit+n+1, 0);
pii a[n];
fr(i, 0, n)
a[i] = {AR[b][i], i};
sort(a, a + n);
j = 0;
ll res = 0;
for(auto q : Q[b])
{
if(q.st == -1)
continue;
int K = q.key;
while(j < n && a[j].first <= K){
int ind = a[j].second;
update(bit, ind+1, n);
j++;
}
res += (query(bit, q.en + 1) - query(bit, q.st));
}
return res;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n,i,j,k,u,v,t,p,q;
cin >> n >> k;
fr(i, 0, n){
int x, r, f;
cin >> x >> r >> f;
radio[i] = {x, {r, f}};
}
sort(radio, radio + n);
fr(i, 0, n){
xx[i] = radio[i].fi;
// error(i, xx[i]);
}
fr(i, 0, n){
p = radio[i].se.se;
freq[p].pb(i);
q = radio[i].fi - radio[i].se.fi;
q = sea_(0, n-1, q);
AR[p].pb(q);
}
fr(i, 0, n-1){
v = radio[i].fi + radio[i].se.fi;
t = _sea(i + 1, n - 1, v);
p = max(radio[i].se.se - k, 1);
q = min(radio[i].se.se + k, (int)1e4);
if(t == i) continue;
fr(j, p, q+1){
if(freq[j].size() == 0)
continue;
// find indices of start and end with i + 1, t respectively in freq[j] vector
auto low = upper_bound(freq[j].begin(), freq[j].end(), i);
if(low != freq[j].end()){
auto up = upper_bound(freq[j].begin(), freq[j].end(), t);
up--;
Q[j].pb({(int)(low - freq[j].begin()), (int)(up - freq[j].begin()), i});
}
}
}
ll ans = 0;
int m = 1e4;
for(i = 1; i <= m; i++)
{
if(Q[i].size() == 0)
continue;
ans += process(i);
}
cout << ans;
return 0;
}