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
#include <iostream>
    #include <stdio.h>
    using namespace std;
   

  long long blast[300009][3];
  int mines[300009][2];
  int n;

  int smaller(int a, int b) {
    return a < b ? a : b;
  }

  int smallest(int a, int b, int c) {
    return smaller(smaller(a, b), c);
  }

  int bigger(int a, int b) {
    return a > b ? a : b;
  }

  int biggest(int a, int b, int c) {
    return bigger(bigger(a, b), c);
  }

  void swapBlast(int indexA, int indexB) {
    long long a = blast[indexA][0];
    long long b = blast[indexA][1];
    long long c = blast[indexA][2];

    blast[indexA][0] = blast[indexB][0];
    blast[indexA][1] = blast[indexB][1];
    blast[indexA][2] = blast[indexB][2];

    blast[indexB][0] = a;
    blast[indexB][1] = b;
    blast[indexB][2] = c;

  }

  bool compareBlast(int a, int b) {
    return blast[a][0] < blast[b][0];
  }

  void sortBlast(int leftBound, int rightBound) {
    if (leftBound >= rightBound) {
      return;
    }
    int left = leftBound;
    int right = rightBound;

    while (left < right) {
      if (compareBlast(left, left + 1)) {
        swapBlast(left + 1, right);
        right--;
      } else {
        swapBlast(left, left + 1);
        left++;
      }
    }
    sortBlast(leftBound, left - 1);
    sortBlast(right + 1, rightBound);
  }

  void calculateMines() {
    for (int i = 0; i < n; i++) {
      int bad = -1;
      int good = i;
      long long leftBlast = blast[i][1];
      while (bad + 1 != good) {
        int candidate = (bad + good) / 2;
        long long candidatePosition = blast[candidate][0];
        if (candidatePosition < leftBlast) {
          bad = candidate;
        } else {
          good = candidate;
        }
      }
      mines[i][0] = good;
    }
    for (int i = n - 1; i >= 0; i--) {
      int bad = n;
      int good = i;
      long long blastRight = blast[i][2];
      while (good + 1 != bad) {
        int candidate = (bad + good) / 2;
        long long candidatePosition = blast[candidate][0];
        if (blastRight < candidatePosition) {
          bad = candidate;
        } else {
          good = candidate;
        }
      }
      mines[i][1] = good;
    }
  }

  bool compareMines(int a, int b) {
    return mines[a][0] < mines[b][0]
        || mines[a][0] == mines[b][0]
            && mines[a][1] < mines[b][1];
  }

  void swapMines(int indexA, int indexB) {
    int a = mines[indexA][0];
    int b = mines[indexA][1];
    mines[indexA][0] = mines[indexB][0];
    mines[indexA][1] = mines[indexB][1];
    mines[indexB][0] = a;
    mines[indexB][1] = b;
  }

  void sortMines(int leftBound, int rightBound) {
    if (leftBound >= rightBound) {
      return;
    }
    int left = leftBound;
    int right = rightBound;

    while (left < right) {
      if (compareMines(left, left + 1)) {
        swapMines(left + 1, right);
        right--;
      } else {
        swapMines(left, left + 1);
        left++;
      }
    }
    sortMines(leftBound, left - 1);
    sortMines(right + 1, rightBound);
  }

  void detonate(int index) {
    int myLeft = mines[index][0];
    int myRight = mines[index][1];

    int leftLeft = mines[myLeft][0];
    int leftRight = mines[myLeft][1];
    int rightLeft = mines[myRight][0];
    int rightRight = mines[myRight][1];

    mines[index][0] = smallest(myLeft, leftLeft, rightLeft);
    mines[index][1] = biggest(myRight, leftRight, rightRight);
  }

  void chainReaction() {
    for (int i = 0; i < n; i++) {
      int maxLeft = mines[i][0];
      int maxRight = mines[i][1];

      int left = i;
      int right = i;
      while (true) {
        if (maxLeft <= left) {
          maxLeft = smaller(maxLeft, mines[left][0]);
          maxRight = bigger(maxRight, mines[left][1]);
          left--;
          continue;
        }
        if (right <= maxRight) {
          maxLeft = smaller(maxLeft, mines[right][0]);
          maxRight = bigger(maxRight, mines[right][1]);
          right++;
          continue;
        }
        break;
      }
      mines[i][0] = maxLeft;
      mines[i][1] = maxRight;
    }
  }

  int main() {
    cin >> n;

    for (int i = 0; i < n; i++) {
      long long position;
      cin >> position;
      long long radius;
      cin >> radius;

      blast[i][0] = position;
      blast[i][1] = position - radius;
      blast[i][2] = position + radius;
    }

    sortBlast(0, n - 1);
    calculateMines();

    chainReaction();

    sortMines(0, n - 1);

    long long solution[300009];

    for (int i = 0; i < n; i++) {
      solution[i] = 0;
    }
    solution[n] = 1;
    int mineIndex;
    int lastRight = n;
    int lastLeft = mines[n - 1][0];
    for (mineIndex = n - 1; mineIndex >= 0; mineIndex--) {
      int left = mines[mineIndex][0];
      int right = mines[mineIndex][1];

      if (left == lastLeft) {
        for (int i = lastRight - 1; i > right; i--) {
          solution[i] = solution[i + 1];
        }
        solution[right] = solution[right + 1] + 1;
      } else {
        break;
      }
      lastLeft = left;
      lastRight = right;
    }
    for (int i = lastRight - 1; i >= lastLeft; i--) {
      solution[i] = solution[i + 1];
    }

    for (; mineIndex >= 0; mineIndex--) {
      int left = mines[mineIndex][0];
      int right = mines[mineIndex][1];
      int nextRight = -1;
      int nextLeft = -1;
      if (mineIndex > 0) {
        nextLeft = mines[mineIndex - 1][0];
        nextRight = mines[mineIndex - 1][1];
      }
      if (left == nextLeft && right == nextRight) {
        continue;
      }
      for (int i = lastLeft - 1; i > left; i--) {
        solution[i] = solution[i + 1];
      }

      solution[left] += solution[right + 1];
      solution[left] = solution[left] % 1000000009L;

      if (nextLeft != left) {
        solution[left] += solution[left + 1];
        solution[left] = solution[left] % 1000000009L;
      }
      lastLeft = left;
      lastRight = right;
    }

    printf("%d", (int) solution[0]);
    return 0;
  }