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
/* 3 w jednym. wzorcówka, brut i generator testów. heheszki */



#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>

#define MP make_pair
#define PB push_back
#define ST first
#define ND second

using namespace std;

typedef pair<int, int> PII;

bool db = false;

const int BASE = 1<<20;
const int MOD = 1000000007;
const int N = 1000010;
const int INF = 1000000000;

int tree_g[BASE*2];
pair<int,int> res[N];
int a[N], gora[N];
int n, b;

void add_g(int in, int x) {
	in += BASE;
	while (in) {
		tree_g[in] = min(tree_g[in], x);
		in /= 2;
	}
}

int min_g(int a, int b, int p=0, int k=BASE-1, int index=1) {
	if (MP(a, b) == MP(p, k)) {
		return tree_g[index];
	}
	int res = INF;
	int m = (p + k) / 2;
	if (a <= m) {
		res = min_g(a, min(m, b), p, m, index*2); 
	}
	if (b > m) {
		res = min(res, min_g(max(m+1, a), b, m+1, k, index*2+1));
	}
	return res;
}

struct kubelek {
	int val, pocz, kon;
	kubelek(int val, int pocz, int kon) : val(val), pocz(pocz), kon(kon) {
	}
	kubelek(){
	}
	int znajdz_akt() {
		return min(pocz + val - 1, N-2);
	}
	int znajdz_wyl() {
		return min(kon + val - 1, N-2);
	}
	int co_aktywowac(int i) {
		return i - val + 1;
	}
};

pair<int, int> tree[BASE<<1];

pair<int, int> join(pair<int, int> a, pair<int, int> b) {
	if (a.ST == b.ST) {
		return MP(a.ST, (a.ND + b.ND) % MOD);
	} else {
		return max(a, b);
	}
}

void aktywuj(int in) {
	tree[in+BASE] = res[in-1];
	in += BASE;
	in /= 2;
	while (in) {
		tree[in] = join(tree[in*2], tree[in*2+1]);
		in /= 2;
	}
}

void deaktywuj(int a, int b) {
	for (int i=a; i<=b; i++) {
		int in = i;
		tree[in+BASE] = MP(0, 0);
		in += BASE;
		in /= 2;
		while (in) {
			tree[in] = join(tree[in*2], tree[in*2+1]);
			in /= 2;
		}
	}
}

pair<int, int> wez_res(int a) {
	int in = a + BASE;
	pair<int, int> ret = tree[in];
	while (in) {
		if (in % 2 == 0) {
			ret = join(ret, tree[in+1]);
		}
		in /= 2;
	}
	if (ret.ND != 0) {
		ret.ST++;
	}
	return ret;
}

kubelek kub[N];
set<int> aktywne;
set<int> aktywowac[N], wylaczyc[N];

/*
void brut() {
	pair<int, int> rrr[n+10];
	rrr[0] = MP(0, 1);
	for (int i=1; i<=n; i++) {
		rrr[i] = MP(0, 0);
		int mi = a[i], mx = b[i];
		for (int j=i; j>0; j--) {
			mi = max(mi, a[j]);
			mx = min(mx, b[j]);
			if (i - j + 1 >= mi && i - j + 1 <= mx) {
				if (rrr[i].ST == rrr[j-1].ST) {
					rrr[i].ND += rrr[j-1].ND;
					rrr[i].ND %= MOD;
				} else if (rrr[i].ST < rrr[j-1].ST) {
					rrr[i] = rrr[j-1];
				}
			}
		}
		if (rrr[i].ND != 0) {
			rrr[i].ST++;
		}
	}
	for (int i=1; i<=n; i++) {
		if (rrr[i] != res[i]) {
			printf("blad!!!\n");
			printf("%d    %d %d  %d %d\n", i, rrr[i].ST, rrr[i].ND, res[i].ST, res[i].ND);
			printf("%d\n", n);
			for (int i=1; i<=n; i++) {
				printf("%d %d\n", a[i], b[i]);
			}
			printf("\n");
			return;
		}
	}
	printf("ok\n");
}
*/
int seed;
/*
void generuj() {
	int gr = 20;
	vector<int> v;
	while (gr--) {
		v.PB(rand()%100000 + 1);
	}
	n = 0;
	for (int i=0; i<v.size(); i++) {
		for (int j=0; j<v[i]; j++) {
			n++;
			a[n] = max(1, v[i] - rand()%50000);
			//b[n] = v[i] + rand()%500000;
                        if (n == 1000000) { i = 1000000000; break; }
		}
	}
        printf("%d\n", seed);
	printf("%d\n", n);
}
*/
////////

int main() {
        seed = time(NULL);
	srand(seed);
	for (int i=1; i<BASE*2; i++) {
		tree_g[i] = INF;
	}
	bool gen = false;
	//if (gen)generuj();
	if (!gen) scanf("%d", &n);
	for (int i=1; i<=n; i++) {
		if (!gen) scanf("%d %d", &a[i], &b);
		if (i + b <= n) {
			gora[i + b] = max(gora[i + b], i + 1);
		}
		add_g(i, b);
	}
	gora[0] = 1;
	for (int i=1; i<=n; i++) {
		gora[i] = max(gora[i], gora[i-1]);
	}
	for (int i=1; i<=n; i++) {
		int mx = min_g(gora[i], i);
		gora[i] = max(gora[i], i-mx+1);
	}

	res[0] = MP(0, 1);
	int kubelki = 0;
	for (int i=1; i<=n; i++) {
		int ppp = i;
		while (kubelki > 0 && kub[kubelki-1].val <= a[i]) {
			ppp = kub[kubelki-1].pocz;
			int ost = kub[kubelki-1].co_aktywowac(i-1);
			int kk = max(i - a[i], kub[kubelki-1].pocz-1);
			deaktywuj(kk+1, ost);
			aktywowac[kub[kubelki-1].znajdz_akt()].erase(kubelki-1);
			wylaczyc[kub[kubelki-1].znajdz_wyl()].erase(kubelki-1);
			aktywne.erase(kubelki-1);
			kubelki--;
		}
		kub[kubelki] = kubelek(a[i], ppp, i);
		aktywowac[kub[kubelki].znajdz_akt()].insert(kubelki);
		if (kub[kubelki].znajdz_akt() < i) {
			aktywne.insert(kubelki);
		}
		wylaczyc[kub[kubelki].znajdz_wyl()].insert(kubelki);
		kubelki++;

		for (set<int>::iterator it = aktywowac[i].begin();
			 	it != aktywowac[i].end(); it++) {
			aktywne.insert(*it);
		}
		for (set<int>::iterator it = aktywne.begin();
			 	it != aktywne.end(); it++) {
			aktywuj(kub[*it].co_aktywowac(i));
		}
		for (set<int>::iterator it = wylaczyc[i].begin();
			 	it != wylaczyc[i].end(); it++) {
			aktywne.erase(*it);
		}
		res[i] = wez_res(gora[i]);
	}
	if (res[n] == MP(0, 0)) {
		printf("NIE\n");
	} else {
		printf("%d %d\n", res[n].ST, res[n].ND);
	}
	//brut();
	return 0;
}