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
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <set>
#include <cstring>
#include <list>
#include <iostream>
#include <cassert>
using namespace std;
#define FOR(i,n) for(int i = 0; i < n; i++)
#define REP(i,n) for(int i = 1; i <= n; i++)
#define FORI(it,n)for(typeof(n.begin()) it = n.begin(); it != n.end(); it++)
#define frs first
#define sec second
#define psh push_back
#define mkp make_pair
typedef long long LL;
typedef long double LD;

const int INF = 2147483647;
const int MAX = 1000100;
const int MOD = 1000000007;

int n, blk;
pair<int,int> DP[MAX];
int C[MAX], D[MAX], N[MAX];

struct str_cmp_D {
	bool operator()(int a, int b) {
		if(D[a] == D[b]) return a < b;
		return D[a] < D[b];
	}
};

struct str_cmp_C {
	bool operator()(int a, int b) {
		if(C[a] - a == C[b] - b) return a < b;
		return C[a] - a > C[b] - b;
	}
};

struct str_cmp_CC {
	bool operator()(int a, int b) {
		if(C[a] == C[b]) return a < b;
		return C[a] < C[b];
	}
};

list<pair<int,int>> W;
set<int, str_cmp_D> MD;
set<int, str_cmp_C> SC;
set<int, str_cmp_CC> SCC;

struct {
	pair<int,int> T[MAX << 2];
	pair<int,int> z;
	int x, y;

	void __put(int a, int b, int k) {
		if(a >= x && b <= y) {
			if(T[k].frs > z.frs) return;
			if(T[k].frs == z.frs) {
				T[k].sec = T[k].sec + z.sec;
				T[k].sec = T[k].sec >= MOD ? T[k].sec - MOD : T[k].sec;
			}
			else T[k] = z;
			return ;
		}

		int sr = (a + b) / 2;
		if(x <= sr) __put(a, sr, k << 1);
		if(y > sr) __put(sr + 1, b, (k << 1) + 1);
	}
	int __find(int a, int b, int k) {
		if(a == b && a == x) return k;
		int sr = (a + b) / 2;
		if(x <= sr) return __find(a, sr, (k << 1));
		else return __find(sr + 1, b, (k << 1) + 1);
	}
	pair<int,int> __get(int k) {
		if(k == 0) return mkp(0, 0);
		pair<int,int> v = __get(k >> 1);
		if(T[k].frs > v.frs) return T[k];
		if(T[k].frs == v.frs) {
			v.sec = v.sec + T[k].sec;
			v.sec = v.sec >= MOD ? v.sec - MOD : v.sec;
		}
		return v;
	}

	pair<int,int> get(int a) {
		x = a;
		int b = __find(0, n, 1);
		return __get(b);
	}
	void put(int a, int b, pair<int,int> v) {
		x = a;
		y = b;
		z = v;
		__put(0, n, 1);
	}
} T;

void split(list<pair<int,int>>::iterator ptr, int a, int b) {
	W.insert(ptr, mkp(ptr->frs, a - 1));
	ptr->frs = b + 1;
}
int next(int a) { return N[a]; }

int main() {
	pair<int,int> dp;
	auto ptr = W.begin(), prv = W.begin();
	scanf("%d", &n);
	REP(i,n) scanf("%d%d", C + i, D + i);

	//set N
	C[n + 1] = n + 1;
	for(int i = 1; i <= n + 1; i++) {
		for(; SCC.size() && C[*SCC.begin()] <= C[i]; SCC.erase(SCC.begin()))
			N[*SCC.begin()] = i;
		SCC.insert(i);
	}


	//Preprocessing
	MD.insert(1);
	for(blk = 1; blk <= n; blk++) {
		if(min(D[*MD.begin()], D[blk]) < blk) break;
		MD.insert(blk);
	}
	W.push_front(mkp(1,n));
	ptr = W.begin();
	for(int i = 1; i <= n; i++) {
		if(C[i] > i) {
			while(ptr != W.end() && C[i] - 1 >= ptr->sec && i <= ptr->frs) {
				prv = ptr;
				ptr ++;
				W.erase(prv);
			}
			if(ptr == W.end()) break;
			if(i > ptr->frs) {
				if(ptr->sec > C[i] - 1) 
					split(ptr, i, C[i] - 1);
				else
					ptr->sec = min(i - 1, ptr->sec);
			}
			else ptr->frs = max(ptr->frs, C[i]);
		}
		else SC.insert(i);
	}
	T.put(0, 0, mkp(0, 1));

	//Algorytm
	int top;
	int v, vv, g;
	for(int p = 1; p <= n; p++) {
		dp = T.get(p - 1); 
		dp.frs ++;
		for(auto it = W.begin(), jt = W.begin(); it != W.end();) {
			if(it->frs > it->sec) {
				jt = it;
				it++;
				W.erase(jt);
			}
			else it++;
		}
		for(auto it = ++W.begin(), prev = W.begin(); it != W.end(); it++) {
			prev = it;
			prev--;
			if(it->frs - 1 <= prev->sec) {
				it->frs = min(it->frs, prev->frs);
				it->sec = max(it->sec, prev->sec);
				W.erase(prev);
			}
		}
		for(auto pr: W) {
			if(pr.frs >= blk) break;
			T.put(pr.frs, min(pr.sec, blk - 1), dp);
		}
		//fprintf(stderr, "\n%d: blk(%d)\n", p, blk);
		//for(auto pr: W) fprintf(stderr, "(%d, %d) ", pr.frs, pr.sec);
		//fprintf(stderr, "\n");

		//delete1
		for(auto it = W.begin(); it != W.end(); it++) it->frs ++;
		//delete2
		ptr = W.begin();
		vector<int> V;
		while(SC.size()) {
			top = *SC.begin();
			if(top - p + 1 <= C[top]) {
				V.psh(top);
				SC.erase(SC.begin());
			}
			else break;
		}
		sort(V.begin(), V.end());
		for(auto top: V) {
				for(; ptr != W.end() && ptr->sec < top; ) ptr++;
				if(ptr != W.end() && ptr->frs < C[top] + p) {
					W.insert(ptr, mkp(ptr->frs, top - 1));
					ptr->frs = C[top] + p;
					ptr --;
				}
		}
		//if(SC.size() == 0) fprintf(stderr, "SC.size() = 0 at %d\n", p);
		//delete3
		ptr = W.begin();
		v = next(p);
		for(int i = p + 1; i < v; i++) {
			vv = next(i);
			g = p + C[i];
			if(g < vv) W.insert(ptr, mkp(g, vv - 1));
			i = vv - 1;
		}

		//Przesuwamy blokade
		MD.erase(p);
		MD.insert(p + 1);
		for(; blk <= n; blk++) {
			if(min(D[*MD.begin()] + p, D[blk] + p) < blk) break;
			MD.insert(blk);
			SC.insert(blk);
		}
		//fprintf(stderr, "DP[%d] = (%d,%d)\n", p - 1, dp.frs - 1, dp.sec);
	}

	dp = T.get(n);
	if(dp.sec == 0) printf("NIE\n");
	else printf("%d %d\n", dp.frs, dp.sec);
}