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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <bits/stdc++.h>
using namespace std;

const int p = 1000000000 + 7;

int nextInt() { int n; scanf("%d", &n); return n; }
int nextLong() { long long n; scanf("%lld", &n); return n; }

int myMin(int a, int b) { return a <= b ? a : b; }
int myMax(int a, int b) { return a >= b ? a : b; }
int mySum(int a, int b) { a += b; if (a >= p) a -= p; return a; }

class RangeTreeMin {
	int s;
	vector<int> data;
public:
	RangeTreeMin(int n) {
		s = 1;
		while (s < n) s *= 2;
		data.resize(2 * s, 0);
	}
	void setValue(int i, int val) {
		i += s;
		data[i] = val;
		for (i/=2; i>0; i/=2) {
			data[i] = myMin(data[2 * i], data[2 * i + 1]);
		}
	}
	int getMin(int i, int j) {
		int res = 999999999;
		i += s;
		j += s;

		while (i < j) {
			if (i % 2 == 1) res = myMin(res, data[i++]);
			if (j % 2 == 0) res = myMin(res, data[j--]);
			i /= 2;
			j /= 2;
		}

		if (i == j) res = myMin(res, data[i]);

		return res;
	}
};

class RangeTreeMax {
	int s;
	vector<int> data;
public:
	RangeTreeMax(int n) {
		s = 1;
		while (s < n) s *= 2;
		data.resize(2 * s, 0);
	}
	void setValue(int i, int val) {
		i += s;
		data[i] = val;
		for (i/=2; i>0; i/=2) {
			data[i] = myMax(data[2 * i], data[2 * i + 1]);
		}
	}
	int getMax(int i, int j) {
		int res = -999999999;
		i += s;
		j += s;

		while (i < j) {
			if (i % 2 == 1) res = myMax(res, data[i++]);
			if (j % 2 == 0) res = myMax(res, data[j--]);
			i /= 2;
			j /= 2;
		}

		if (i == j) res = myMax(res, data[i]);

		return res;
	}
};

class RangeTreeSum {
	int s;
	vector<int> data;
public:
	RangeTreeSum(int n) {
		s = 1;
		while (s < n) s *= 2;
		data.resize(2 * s, 0);
	}
	void add(int i, int val) {
		i += s;
		for (; i>0; i/=2) data[i] = mySum(data[i], val);
	}
	int getSum(int i, int j) {
		int res = 0;
		i += s;
		j += s;

		while (i < j) {
			if (i % 2 == 1) res = mySum(res, data[i++]);
			if (j % 2 == 0) res = mySum(res, data[j--]);
			i /= 2;
			j /= 2;
		}

		if (i == j) res = mySum(res, data[i]);

		return res;
	}
};

struct Item {
	int a;
	int b;
};

bool longestFirst(const Item &x, const Item &y) {
	if (x.b - x.a != y.b - y.a) return x.b - x.a > y.b - y.a;
	return x.a < y.a;
}

bool leftFirst(const Item &x, const Item &y) {
	if (x.a != y.a) return x.a < y.a;
	return x.b < y.b;
}

map<pair<int, int>, set<int> > trigSet;

vector<int> worker;

void show(vector<Item> &v) {
//	printf("size %d => ", v.size());
//	for (int i=0; i<v.size(); ++i) printf("%d-%d ", v[i].a, v[i].b);
}

int solve(vector<Item> &v) {
	vector<Item> vcp(v);

	if (v.empty()) { /*show(vcp); printf("=> 1\n");*/ return 1; }
	if (v.size() == 1) { /*show(vcp); printf("=> 2\n");*/ return 2; }

	worker.clear();
	worker.push_back(0);
	int bestDist = v[0].b - v[0].a;
	for (int i=1; i<v.size(); ++i) {
		if (v[i].b - v[i].a > bestDist) {
			bestDist = v[i].b - v[i].a;
			worker.clear();
		}
		if (v[i].b - v[i].a == bestDist) worker.push_back(i);
	}

	int x = worker[rand() % worker.size()];

	int aa = v[x].a;
	int bb = v[x].b;
	v[x] = v.back();
	v.pop_back();

	sort(v.begin(), v.end(), leftFirst);

	set<int>::iterator trigBegin = trigSet[make_pair(aa, bb)].begin();
	set<int>::iterator trigEnd = trigSet[make_pair(aa, bb)].end();
	int nextTrig = *trigBegin;

	vector<Item> mjm;

	int res = 1;
	for (int i=0; i<v.size(); ++i) {
		while (v[i].a > nextTrig) {
//printf("next %d-%d\n", aa, bb);
			res = (1LL * res * solve(mjm)) % p;
			mjm.clear();
			++trigBegin;
			if (trigBegin != trigEnd) nextTrig = *trigBegin;
			else nextTrig = 999999999;
		}
		mjm.push_back(v[i]);
	}
	if (!mjm.empty()) {
//printf("next %d-%d\n", aa, bb);
		res = (1LL * res * solve(mjm)) % p;
		mjm.clear();
	}

	vector<Item> mjm2;

	for (int i=0; i<v.size(); ++i) {
		if (v[i].a < aa) mjm.push_back(v[i]);
		else if (v[i].b > bb) mjm2.push_back(v[i]);
	}

	int add = (1LL * solve(mjm) * solve(mjm2)) % p;
	res = mySum(res, add);

//	show(vcp);
//	printf("=> %d\n", res);

	return res;
}

int main() {
	int n = nextInt();

	vector<long long> pos(n);
	vector<long long> radius(n);

	for (int i=0; i<n; ++i) {
		pos[i] = nextLong();
		radius[i] = nextLong();
	}

	// first phase - fill init range
	// a[i] - id of the most left bomb, that can be reached by triggering bomb with id=i
	// b[i] - id of the most right bomb, that can be reached by triggering bomg with id=i

	vector<int> a(n);
	vector<int> b(n);

	RangeTreeMin rta(n);
	RangeTreeMax rtb(n);

	for (int i=0; i<n; ++i) {
		// looking init range for bomb i

		// left range
		{
			long long posA = pos[i] - radius[i];
			int x = -1;
			int y = i;
			while (x + 1 < y) {
				int z = (x + y) / 2;
				if (pos[z] >= posA)
					y = z;
				else
					x = z;
			}
			a[i] = y;
			rta.setValue(i, y);
		}

		// right range
		{
			long long posB = pos[i] + radius[i];
			int x = i;
			int y = n;
			while (x + 1 < y) {
				int z = (x + y) / 2;
				if (pos[z] <= posB)
					x = z;
				else
					y = z;
			}
			b[i] = x;
			rtb.setValue(i, x);
		}
	}

//	printf("Initial:\n");
//	for (int i=0; i<n; ++i) printf("%d: [%d, %d]\n", i, a[i], b[i]);

	bool anyChange = true;
	while (anyChange) {
		anyChange = false;
		for (int i=0; i<n; ++i) {
			int aa = rta.getMin(a[i], b[i]);
			int bb = rtb.getMax(a[i], b[i]);
			if (aa != a[i]) {
				anyChange = true;
				a[i] = aa;
				rta.setValue(i, aa);
			}
			if (bb != b[i]) {
				anyChange = true;
				b[i] = bb;
				rtb.setValue(i, bb);
			}
		}
	}

//	printf("After 1st:\n");
//	for (int i=0; i<n; ++i) printf("%d: [%d, %d]\n", i, a[i], b[i]);

	// final computation

	for (int i=0; i<n; ++i) {
		pair<int, int> pp = make_pair(a[i], b[i]);
		trigSet[pp].insert(i);
	}

	vector<Item> todo;
	for( map<pair<int, int>, set<int> >::iterator it = trigSet.begin(); it != trigSet.end(); ++it)
		todo.push_back( {it->first.first, it->first.second} );

	int res = solve(todo);
	printf("%d\n", res);

	return 0;
}