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
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
typedef vector<int> vi;
#define pb push_back
#define sz size()
typedef pair<int,int> pii;
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
#define INF 1000000001
const ll M = 1000000007;
#define ALL(t) t.begin(),t.end()
#define SC(a) scanf("%d", &a)
#define GET(a) int a; SC(a)

#define ISDEBUG 0
#define dprintf(...) if(ISDEBUG) \
{printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");}
template <class It> void dptab(It b, It e, const char* f="%d ") {
	if(ISDEBUG) {
		for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n");
}}

int find_parent(int id);

struct mine {
	int id;
	int x;
	int r;
	int parent;
	int left;
	int right;
	vi triggers;
	bool triggered;
	bool visited;
	ll calculated;
};

bool operator<(const mine& lhs, const mine& rhs) {
	if(lhs.x != rhs.x) return lhs.x < rhs.x;
	if(lhs.r != rhs.r) return lhs.r > rhs.r;
	return lhs.id < rhs.id;
}

struct event {
	enum event_type { RANGE_BEGIN, BOMB, RANGE_END };
	int x;
	int id;
	event_type type;

	bool operator<(const event& rhs) const {
		if(x != rhs.x) return x < rhs.x;
		if(type != rhs.type) return type < rhs.type;
		return id < rhs.id;
	}
};

vector<mine> mines;
set<mine> tree;
set<event> events;
set<mine> sweep;
vi topological;

int find_parent(int id) {
	if(mines[id].parent == id) return id;
	int parent = find_parent(mines[id].parent);
	mines[id].parent = parent;
	return parent;
}

void set_parent(int id, int parent) {
	mines[id].parent = parent;
	mines[id].left = min(mines[id].left, mines[parent].left);
	mines[id].right = max(mines[id].right, mines[parent].right);
	mines[parent].left = mines[id].left;
	mines[parent].right = mines[id].right;
}

void join_into_union(int id1, int id2) {
	int parent = find_parent(id2);
	set_parent(id1, parent);
}

void visit(int i) {
	if(mines[i].visited) return;

	mines[i].visited = true;
	for(auto const& j : mines[i].triggers) {
		visit(j);
	}
	topological.pb(i);
}

ll calculate(int i) {
	ll result = 1;
	if (mines[i].triggered) {
		return 1;
	}
	for(auto const& j : mines[i].triggers) {
		if (!mines[j].triggered) {
			result *= mines[j].calculated;
			result %= M;
			mines[j].triggered = true;
		}
	}
	result += 1;
	mines[i].calculated = result;
	dprintf("for #%d calculated %d\n", i, (int)result);
	return result + 1;
}

int main() {
	GET(n);
	FOR(i,0,n) {
		GET(x);
		GET(r);
		mines.pb({i,x,r,i,x-r,x+r});
		tree.insert(mines.back());
		events.insert(event{x-r, i, event::RANGE_BEGIN});
		events.insert(event{x, i, event::BOMB});
		events.insert(event{x+r, i, event::RANGE_END});
	}

	for(auto const& e : events) {
		if(e.type == event::RANGE_BEGIN) {
			//sweep.insert(mines[e.id]);
			//dprintf("Begin of bomb #%d range at %d\n", e.id, e.x);
		} else if(e.type == event::BOMB) {
			dprintf("Reached bomb #%d at %d\n", e.id, e.x);
			auto left = mines[e.id].left;
			dprintf("left: %d\n", left);
			mine dummy{-1, left, INF};
			auto bound = sweep.lower_bound(dummy);
			if(bound == sweep.end()) {
				dprintf("No equivalent bombs found\n");
				sweep.insert(mines[e.id]);
				goto debug;
			}
			dprintf("bound id = %d\n", bound->id);
			int new_parent = e.id;
			if(find_parent(new_parent) != new_parent) {
				dprintf("Assert failed");
			}
			for(auto it = bound; it != sweep.end(); ++it) {
				const mine& m = *it;
				dprintf("%d is equivalent\n", m.id);
				set_parent(m.id, new_parent);
				events.erase({m.x, m.id, event::BOMB});
				events.erase({m.x+m.r, m.id, event::RANGE_END});
				//dodaj do unii
				//wylicz wspólny koniec zasiegu
				//pousuwaj z eventow bomby, końce
			}
			events.erase({mines[e.id].x, e.id, event::RANGE_END});
			events.insert({mines[e.id].right, e.id, event::RANGE_END});
			sweep.erase(bound, sweep.end());
			sweep.insert(mines[e.id]);
			//dodaj wspólny koniec zasięgu 
		} else if(e.type == event::RANGE_END) {
			dprintf("End of bomb #%d range at %d\n", e.id, e.x);
			sweep.erase(mines[e.id]);
		}
debug:
		dprintf("================= sweep state: ");
		for(auto const& mine : sweep) {
			dprintf("%d ", mine.id);
		}
		dprintf("\n");
	}

	dprintf("ids    : ");
	for(auto const& mine : mines) {
		dprintf("%d ", mine.id);
	}
	dprintf("\n");
	dprintf("parent : ");
	for(auto const& mine : mines) {
		dprintf("%d ", mine.parent);
	}
	dprintf("\n");

	//generate events
	events.clear();
	FOR(i,0,n) {
		mine& m = mines[i];
		if(m.parent == i) {
			events.insert(event{m.x, i, event::BOMB});
		}
	}
	//go left-right
	sweep.clear();
	dprintf("++++++++++++++++++ LEFT-RIGHT +++++++++++++++++++++ \n");
	for(auto const& e : events) {
		if(e.type == event::BOMB) {
			dprintf("Reached bomb #%d at %d\n", e.id, e.x);
			auto left = mines[e.id].left;
			dprintf("left: %d\n", left);
			mine dummy{-1, left, INF};
			auto bound = sweep.lower_bound(dummy);
			if(bound == sweep.end()) {
				dprintf("No triggered bombs found\n");
				sweep.insert(mines[e.id]);
				goto debug2;
			}
			for(auto it = bound; it != sweep.end(); ++it) {
				const mine& m = *it;
				dprintf("%d is triggered\n", m.id);
				mines[e.id].triggers.pb(m.id);
			}
			sweep.insert(mines[e.id]);
		}
debug2:
		dprintf("================= sweep state: ");
		for(auto const& mine : sweep) {
			dprintf("%d ", mine.id);
		}
		dprintf("\n");
	}

	//go right-left
	dprintf("\n\n++++++++++++++++++ RIGHT-LEFT +++++++++++++++++++++ \n");
	sweep.clear();
	for(auto it = events.rbegin(); it != events.rend(); ++it) {
		const event& e = *it;
		if(e.type == event::BOMB) {
			dprintf("Reached bomb #%d at %d\n", e.id, e.x);
			auto right = mines[e.id].right;
			dprintf("right: %d\n", right);
			mine dummy{INF, right, -1};
			auto bound = sweep.upper_bound(dummy);
			if(bound == sweep.end()) {
				dprintf("No triggered bombs found\n");
				sweep.insert(mines[e.id]);
				goto debug3;
			}
			for(auto it2 = sweep.begin(); it2 != bound; ++it2) {
				const mine& m = *it2;
				dprintf("%d is triggered\n", m.id);
				mines[e.id].triggers.pb(m.id);
			}
			sweep.insert(mines[e.id]);
		}
debug3:
		dprintf("================= sweep state: ");
		for(auto const& mine : sweep) {
			dprintf("%d ", mine.id);
		}
		dprintf("\n");
	}

	FOR(i,0,n) {
		if(mines[i].parent == i && !mines[i].visited) {
			visit(i);
		}
	}
	FOR(i,0,topological.sz) {
		calculate(topological[i]);
	}
	dprintf("topo: ");
	dptab(ALL(topological));


	ll result = 1;
	FOR(i,0,topological.sz) {
		int id = topological[i];
		if (!mines[id].triggered) {
			dprintf("calc for #%d = %d\n", id, (int) mines[id].calculated);
			result *= mines[id].calculated;
			result %= M;
		}
	}

	printf("%d\n", (int) result);;
	return 0;
}