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
#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 300005;

const ll mod = 1000000007LL;

int n;
ll dp[2][maxn];

ll pos[maxn];
ll rad[maxn];

ll zasiegi[maxn];

pair<ll, int> segtree[2 * maxn];
int segtree_min[2 * maxn];

void construct_segment_tree() 
{ 
    for (int i = 0; i < n; i++) {
        segtree[n + i] = {pos[i] + rad[i], i}; 
        segtree_min[n + i] = (int) 1e8;
    }
  
    for (int i = n - 1; i >= 1; i--) {
        segtree[i] = max(segtree[2 * i], segtree[2 * i + 1]); 
        segtree_min[i] = min(segtree_min[2 * i], segtree_min[2 * i + 1]);
    }
}

void update(int p, ll val) 
{ 
    p += n; 
  
    segtree[p].fi = val; 
  
    while (p > 1) { 
        p >>= 1; 
  
        segtree[p] = max(segtree[2 * p], segtree[2 * p + 1]); 
    } 
} 

int query(int l, int r)
{
    l += n;
    r += n;

    pair<ll, int> res = {0, -1};

    while (l < r)
    {
        if (l & 1) {
            res = max(res, segtree[l]);
            l++;
        }

        if (r & 1) {
            r--;
            res = max(res, segtree[r]);
        }

        l /= 2;
        r /= 2;
    }

    return res.se;
}

void update_min(int p, int val) 
{ 
    p += n; 
  
    segtree_min[p] = val; 
  
    while (p > 1) { 
        p >>= 1; 
  
        segtree_min[p] = min(segtree_min[2 * p], segtree_min[2 * p + 1]); 
    } 
} 

int query_min(int l, int r)
{
    l += n;
    r += n;

    int res = (int) 1e8;

    while (l < r)
    {
        if (l & 1) {
            res = min(res, segtree_min[l]);
            l++;
        }

        if (r & 1) {
            r--;
            res = min(res, segtree_min[r]);
        }

        l /= 2;
        r /= 2;
    }

    return res;
}

int main()
{
    ios_base::sync_with_stdio(0);

    cin >> n;

    for (int i = 0; i < n; i++) {
        cin >> pos[i] >> rad[i];
    }

    construct_segment_tree();

    dp[0][0] = 1;

    stack<int> Q;

    for (int i = 0; i < n; i++) {
        int razenie = lower_bound(pos, &pos[i + 1], pos[i] - rad[i]) - pos;
        if (razenie != i)
            razenie = query_min(razenie, i);

        update_min(i, razenie);

        dp[1][i + 1] = (dp[0][razenie] + dp[1][razenie]) % mod;

        while(!Q.empty() && zasiegi[Q.top()] < pos[i])
            Q.pop();

        dp[0][i + 1] = (dp[0][i] + dp[1][i]) % mod;

        if (!Q.empty()) {
            dp[0][i + 1] = (dp[0][i+1] - dp[1][Q.top() + 1] + mod) % mod;
        }

        zasiegi[i] = pos[i] + rad[i];
        if (razenie != i)
            zasiegi[i] = max(zasiegi[i], zasiegi[query(razenie, i)]);
        
        update(i, zasiegi[i]);

        //cout << i << ": " << pos[i] << " - " << rad[i] << "; " << (dp[0][i + 1]) << ", "<< (dp[1][i+1]) << " " << zasiegi[i] <<  " " << razenie << "\n";

        Q.push(i);
    }

    cout << ((dp[0][n] + dp[1][n]) % mod) << "\n";

    return 0;
}