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
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

#define f first
#define s second

const int MAXN = 3e5 + 13;
const int MOD = 1e9 + 7;

struct spojna {
    int l, r;
    long long zl, zr;
    long long kl, kr;
};

int repr[MAXN];
int roz[MAXN];
spojna t[MAXN];
long long tab[MAXN];
int dp[MAXN][2];

int fnd(int x) {
    if (x == repr[x])
        return x;
    return repr[x] = fnd(repr[x]);
}

void onion(int x, int y) {
    if (roz[x] < roz[y])
        swap(x, y);
    repr[y] = x;
    roz[x] += roz[y];
    t[x].l = min(t[x].l, t[y].l);
    t[x].r = max(t[x].r, t[y].r);
    t[x].zl = min(t[x].zl, t[y].zl);
    t[x].zr = max(t[x].zr, t[y].zr);
    t[x].kl = min(t[x].kl, t[y].kl);
    t[x].kr = max(t[x].kr, t[y].kr);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, a, b, c;
    long long x;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> t[i].kl >> x;
        tab[i] = t[i].kl;
        t[i].kr = t[i].kl;
        t[i].l = t[i].r = i;
        t[i].zl = t[i].kl - x;
        t[i].zr = t[i].kl + x;
        repr[i] = i;
        roz[i] = 1;
    }
    vector<int> vec;
    bool change = true;
    while (change) {
        change = false;
        for (int i = 1; i <= n; i++) {
            while (!vec.empty()) {
                b = fnd(vec.back());
                if (t[b].zr < tab[i])
                    vec.pop_back();
                else
                    break;
            }
            if (!vec.empty()) {
                a = fnd(i);
                c = vec.back();
                b = fnd(c);
                if (a != b && t[a].zl <= tab[c] && t[b].zr >= tab[i]) {
                    change = true;
                    onion(a, b);
                }
            }
            vec.push_back(i);
        }
        vec.clear();
        for (int i = n; i >= 1; i--) {
            while (!vec.empty()) {
                b = fnd(vec.back());
                if (t[b].zl > tab[i])
                    vec.pop_back();
                else
                    break;
            }
            if (!vec.empty()) {
                a = fnd(i);
                c = vec.back();
                b = fnd(c);
                if (a != b && t[a].zr >= tab[c] && t[b].zl <= tab[i]) {
                    change = true;
                    onion(a, b);
                }
            }
            vec.push_back(i);
        }
        vec.clear();
    }
    vector<int> v;
    for (int i = 1; i <= n; i++) {
        a = fnd(i);
        while (!v.empty()) {
            b = v.back();
            if (t[a].zl <= tab[b]) {
                b = fnd(b);
                t[a].zl = min(t[a].zl, t[b].zl);
                t[a].l = min(t[a].l, t[b].l);
                v.pop_back();
            }
            else
                break;
        }
        v.push_back(i);
    }
    v.clear();
    for (int i = n; i >= 1; i--) {
        a = fnd(i);
        while (!v.empty()) {
            b = v.back();
            if (t[a].zr >= tab[b]) {
                b = fnd(b);
                t[a].zr = max(t[a].zr, t[b].zr);
                t[a].r = max(t[a].r, t[b].r);
                v.pop_back();
            }
            else
                break;
        }
        v.push_back(i);
    }
    vector<pair<int, int> > v2;
    dp[0][0] = 1;
    pair<int, int> p2;
    for (int i = 1; i <= n; i++) {
        a = fnd(i);
        while (!v2.empty()) {
            p2 = v2.back();
            if (p2.f < i)
                v2.pop_back();
            else
                break;
        }
        dp[i][0] = dp[i - 1][0] + dp[i - 1][1];
        if (!v2.empty()) {
            p2 = v2.back();
            dp[i][0] -= p2.s;
            if (dp[i][0] < 0)
                dp[i][0] += MOD;
        }
        if (dp[i][0] >= MOD)
            dp[i][0] -= MOD;
        while (!v2.empty()) {
            p2 = v2.back();
            if (p2.f <= t[a].r)
                v2.pop_back();
            else
                break;
        }
        dp[i][1] = dp[t[a].l - 1][0] + dp[t[a].l - 1][1];
        if (dp[i][1] >= MOD)
            dp[i][1] -= MOD;
        v2.emplace_back(t[a].r, dp[i][1]);
    }
    cout << (dp[n][0] + dp[n][1]) % MOD;
    return 0;
}