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 FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define st first
#define nd second

using namespace std;

typedef pair<int, int> PII;

const int MAXN = 1000010;
const int MAXD = 2 * 1024 * 1024 + 3;
const int MOD = 1000000007;

struct result{
	int siz, cnt;
	result() : siz(-1), cnt(0) {}
	result(int s, int c) : siz(s), cnt(c) {}
};

inline result operator+(const result &a, const result &b){
	if(a.siz != b.siz){
		if(a.siz > b.siz)
			return a;
		return b;
	}
	return result(a.siz, (a.cnt + b.cnt) % MOD);
}

struct minqueue{
	queue<int> Q;
	deque<int> D;
	void push(int x){
		Q.push(x);
		while(!D.empty() && D.back() > x)
			D.pop_back();
		D.push_back(x);
	}
	void pop(){
		if(D.front() == Q.front())
			D.pop_front();
		Q.pop();
	}
	int min(){
		return D.front();
	}
	int size(){
		return Q.size();
	}
};

int n, d;
result tree[MAXD];

inline result tget(int a, int b){
	//printf("get %d %d\t", a, b);
	result r;
	a += d;
	b += d;
	while(a < b){
		if(a&1){
			r = r + tree[a];
			++a;
		}
		if((b&1)==0){
			r = r + tree[b];
			--b;
		}
		a /= 2;
		b /= 2;
	}
	if(a == b)
		r = r + tree[a];
	//printf("%d %d\n", r.siz, r.cnt);
	return r;
}

inline void tset(int a, result b){
	a += d;
	tree[a] = b;
	a /= 2;
	while(a){
		tree[a] = tree[2*a] + tree[2*a+1];
		a /= 2;
	}
}

int mi[MAXN], ma[MAXN];
int sp[MAXN];
vector<int> A, B, ST;
vector<int> activate[MAXN];
minqueue limq;
int repr[MAXN];

int find(int a){
	return repr[a]==a?a:repr[a]=find(repr[a]);
}

int main(){
	scanf("%d", &n);
	++n;
	d = 1;
	while(d < n) d *= 2;
	mi[0] = n + 3;
	ma[0] = 0;
	tset(0, result(0,1));
	ST.push_back(0);
	FWD(i,1,n){
		scanf("%d %d", &mi[i], &ma[i]);
		
		result r;

		limq.push(ma[i]);
		while(limq.size() > limq.min()) limq.pop();
		int lim = i - limq.size();

		repr[i] = i;

		while(mi[ST.back()] <= mi[i]) ST.pop_back();
		sp[i] = ST.size();
		ST.push_back(i);

		int ta = ST[ST.size()-2] + mi[i];
		if(ta <= i)
			A.push_back(i);
		else if(ta < n)
			activate[ta].push_back(i);

		A.insert(A.end(), activate[i].begin(), activate[i].end());

		for(int a : A){
			int p = sp[a];
			if((int)ST.size() <= p || find(ST[p]) != a)
				continue;
			r = r + tget(max(lim, find(ST[p-1])), min(i-mi[a], ST[p]-1));
		}
		for(int a : A){
			int p = sp[a];
			if((int)ST.size() <= p || find(ST[p]) != a)
				continue;
			if(i-mi[a] >= a){
				repr[ST[p]] = find(ST[p-1]);
				continue;
			}
			B.push_back(a);
		}
		
						
//		for(int j = 1; j < (int)ST.size(); ++j)
//			r = r + tget(max(lim, ST[j-1]), min(i-mi[ST[j]], ST[j]-1));

		A.clear();
		A.swap(B);
		if(r.siz != -1)
			++r.siz;
		tset(i, r);
	}
	result r = tree[d+n-1];
	if(r.siz == -1)
		printf("NIE\n");
	else
		printf("%d %d\n", r.siz, r.cnt);
	return 0;
}