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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#define debug if(0)
#define BRT_
// Grzegorz Guspiel
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i,n) for(int i=0;i<int(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second

typedef pair<int, int> PII;

template<typename T> void maxE(T& a, const T& b) { a = max(a, b); }
template<typename T> void minE(T& a, const T& b) { a = min(a, b); }
template<typename S, typename T>
ostream& operator<<(ostream& out, const pair<S, T>& rhs) {
    out << "(" << rhs.st << "," << rhs.nd << ")";
    return out;
}
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& t) {
    out << "[";
    FOREACH (i, t) out << *i << ", ";
    out << "]";
    return out;
}

const int MAXN = 1000 * 1000 + 1000;
const int INF = 1000 * 1000 * 1000 + 1000;
const int P = 1000 * 1000 * 1000 + 7;

int n;
vector<PII> data;
int gi;

#ifdef BRT
namespace brt {
void improve(PII& a, PII b) {
    debug cout << "improve " << a << " " << b;
    if (b.st + 1 == a.st) {
        a.nd = (a.nd + b.nd) % P;
    } else if (b.st + 1 > a.st) {
        a.st = b.st + 1;
        a.nd = b.nd;
    }
    debug cout << " to " << a << endl;
}

PII empty() { return PII(INF, -INF); }
PII full() { return PII(-INF, INF); }
PII intersect(PII a, PII b) { return PII(max(a.st, b.st), min(a.nd, b.nd)); }
bool contains(PII a, int b) { return b >= a.st && b <= a.nd; }

PII t[MAXN];

void solve() {
    ios_base::sync_with_stdio(0);

    t[0] = mp(0, 1);
    for (int i = 1; i <= n; i++) {
        t[i] = mp(-INF, 0);
        for (int j = 0; j < i; j++) {
            PII restriction = full();
            for (int k = j; k < i; k++) restriction = intersect(restriction, data[k]);
            if (contains(restriction, i - j)) {
                improve(t[i], t[j]);
            }
        }
    }
}
} //ns
#endif

int zeroesAt[MAXN];
int leftLimit;

struct Item {
    int i, l;
    Item() {}
    Item(int i_, int l_): i(i_), l(l_) {}
};
ostream& operator<<(ostream& out, const Item& i) {
    out << "(i=" << i.i << ",l=" << i.l << ")";
    return out;
}

static struct {
    deque<Item> q;
    // st i nd l

    void addLimit(int i, int l) {
        while (!q.empty() && q.back().l >= l) q.pop_back();
        q.push_back(Item(i, l));
    }

    int current() { return q.empty() ? INF : q.front().l; }

    void update(int i) {
        bool changed;
        do {
            changed = 0;
            maxE(leftLimit, i - current() + 1);
            while (!q.empty() && q.front().i < leftLimit) {
                q.pop_front();
                changed = 1;
            }
        } while (changed);
    }
} leftLimiter;

struct ResultSet {
    PII t[MAXN];
    map<int, PII> s;
    bool inSet[MAXN];
    int myLeft;

    void init() {
        myLeft = 0;
        REP (i, n  +5) inSet[i] = 0;
        s.clear();
        REP (i, n + 5) t[i] = mp(0, 0);
    }

    void assign(int i, PII v) { 
        t[i] = v;
    }
    PII get(int i) { return t[i]; }
    void activate(int i, int from) {
        if (inSet[i]) return;
        if (i < myLeft) return;
        map<int, PII>::iterator found = s.find(t[i].st);
        if (found == s.end()) s[t[i].st] = mp(1, t[i].nd);
        else {
            found->nd.st++;
            found->nd.nd = (found->nd.nd + t[i].nd) % P;
        }
        inSet[i] = 1;
    }
    void deactivate(int i) {
        if (!inSet[i]) return;
        if (i < 0) return;
        map<int, PII>::iterator found = s.find(t[i].st);
        found->nd.st--;
        found->nd.nd = (P + found->nd.nd - t[i].nd) % P;
        if (!found->nd.st) {
            assert(!found->nd.nd);
            s.erase(found);
        }
        inSet[i] = 0;
    }
    PII querySet() {
        if (s.begin() == s.end()) return mp(-INF, 0);
        map<int, PII>::iterator found = --s.end();
        int a = found->st;
        int b = found->nd.nd;
        assert(found->nd.st > 0);
        return mp(a, b);
    }
    void updateLeft() {
        while (myLeft < leftLimit) {
            deactivate(myLeft++);
        }
    }
};
ResultSet resultSet;

struct ForbiddenStack {
    vector<int> s;
    void cleanTop() {
        while (!s.empty() && gi >= zeroesAt[s.back()]) s.pop_back();
    }
    int top() { cleanTop(); return s.back(); }
    void pop() { cleanTop(); s.pop_back(); }
    bool empty() { cleanTop(); return s.empty(); }
    void push(int a) {       s.pb(a);    }
};
ostream& operator<<(ostream& out, const ForbiddenStack& s) {
    out << "S[";
    FOREACH (i, s.s) out << *i  << "(z" << zeroesAt[*i] << "), ";
    out << "]";
    return out;
}
ForbiddenStack forbiddenStack;

static struct {
    vector<int> todo;
    vector<int> starters[MAXN];
    bool disabled[MAXN];

    void init() {
        todo.clear();
        REP (i, n + 5) {
            starters[i].clear();
            disabled[i] = 0;
        }
    }

    void addEvent(int i, int firstMove) {
        starters[firstMove].pb(i);
    }
    void eraseEvent(int i) {
        disabled[i] = 1;
    }
    void processEvents(int moveTo) {
        FOREACH (j, starters[moveTo]) {
            todo.pb(*j);
        }
        starters[moveTo].clear();
        vector<int> now = todo;
        todo.clear();
        FOREACH (j, now) {
            if (disabled[*j]) continue;
            if (zeroesAt[*j] < moveTo) continue;
            resultSet.activate(*j - (zeroesAt[*j] - moveTo), *j);
            todo.pb(*j);
        }
    }
} eventQueue;

PII empty() { return PII(INF, -INF); }
PII full() { return PII(-INF, INF); }
PII intersect(PII a, PII b) { return PII(max(a.st, b.st), min(a.nd, b.nd)); }
int rangeLength(PII a) { return a.nd - a.st; }
bool intersects(PII a, PII b) { return rangeLength(intersect(a, b)) > 0; }
bool contains(PII a, PII b) { return a.st <= b.st && b.nd <= a.nd; }
bool contains(PII a, int b) { return b >= a.st && b <= a.nd; }

PII forbiddenRange(int i) { return mp(i - (zeroesAt[i] - gi) + 1, i); }

void makeForbidden(int i, int length, int firstMove) {
    zeroesAt[i] = i + length + 1;
    int clearedTo = i + 1;
    while (!forbiddenStack.empty() &&
            contains(forbiddenRange(i), forbiddenRange(forbiddenStack.top()))) {
        while (clearedTo > forbiddenStack.top() + 1) resultSet.deactivate(--clearedTo);
        clearedTo = forbiddenRange(forbiddenStack.top()).st;
        eventQueue.eraseEvent(forbiddenStack.top());
        forbiddenStack.pop();
    }
    int delay = 0;
    if (!forbiddenStack.empty() && forbiddenStack.top() >= forbiddenRange(i).st) {
        delay = 1 + rangeLength(
                    intersect(forbiddenRange(i), forbiddenRange(forbiddenStack.top())));
        firstMove += delay;
    }
    while (clearedTo > forbiddenRange(i).st) resultSet.deactivate(--clearedTo);
    forbiddenStack.push(i);
    eventQueue.addEvent(i, firstMove);
}

PII solve() {
    REP (i, n + 5) zeroesAt[i] = 0;
    resultSet.init();
    forbiddenStack.s.clear();
    leftLimit = 0;
    leftLimiter.q.clear();
    eventQueue.init();
    resultSet.assign(0, mp(0, 1));
    resultSet.activate(0, -1);
    gi = 0;
    for (int i = 1; i <= n; i++) {
        gi = i;
        eventQueue.processEvents(i);
        if (data[i - 1].st > 1)
            makeForbidden(i - 1, data[i - 1].st - 1, i + 1);

        leftLimiter.addLimit(i - 1, data[i - 1].nd);
        leftLimiter.update(i - 1);
        resultSet.updateLeft();

        PII result = resultSet.querySet();
        result.st++;
        resultSet.assign(i, result);
        resultSet.activate(i, INF);
    }
#ifdef BRT
    brt::solve();
    REP (i, n + 1) {
        PII expected = brt::t[i]; maxE(expected.st, 0);
        PII received = resultSet.get(i); maxE(received.st, 0);
        if (expected != received) {
            cout << "expected " << expected << " received " << received << endl;
            cout << n << endl;
            FOREACH (i, data) cout << i->st << " " << i->nd << endl;
            assert(0);
        }
    }
#endif
    return resultSet.get(n);
}

#ifdef BRT
void genAll() {
    if (SIZE(data) == n) {
        solve();
    } else {
        for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) {
            data.pb(mp(i, j));
            genAll();
            data.pop_back();
        }
    }
}
void genRand() {
    while (true) {
        data.clear();
        REP (i, n) {
            int a = 1 + rand() % n;
            int b = 1 + rand() % n;
            if (a > b) swap(a, b);
            data.pb(mp(a, b));
        }
        solve();
    }
}
#endif

int main() {
#ifdef BRT
    n = 10;
    genRand();
    for (n = 1; ; n++) {
        cout << "n " << n << endl;
        genAll();
    }
#endif
    ios_base::sync_with_stdio(0);

    scanf("%d", &n);
    data.assign(n, mp(0, 0));
    REP (i, n) scanf("%d%d", &data[i].st, &data[i].nd);

    PII result = solve();
    if (result.st > 0) printf("%d %d\n", result.st, result.nd);
    else printf("NIE\n");
	return 0;
}