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
#include <stdio.h>
#include <assert.h>

#define N_MAX 1000000

int n;
int c[N_MAX];
int d[N_MAX];
int p[N_MAX];


int s[N_MAX];
int s_pos;

int max_p, max_n, cur_p;

void print_result()
{
        int i, j;

        i = 0;
        for(i = 0; i < n; i += p[i]) {
                printf("(%d", i+1);
                for(j = i + 1; j < i + p[i]; ++j) {
                        printf(" %d", j+1);
                }
                printf(")");
        }
        printf("\n");
}

void print_p()
{
        int i = 0;
        for(i = 0; i < n; ++i) {
                printf("%d-%d  ", i, p[i]);
        }
        printf("\n");
}

void s_init()
{
        s_pos = 0;
}

void push(int x)
{
        s[s_pos++] = x;
}

int pop()
{
        assert(s_pos > 0);
        s_pos--;
        return s[s_pos];
}


int check()
{
        int i, j, err, x;

        max_p = 0;
        max_n = 0;
        cur_p = 0;

        for(i = 0; i < n; ++i) {
                p[i] = c[i]-1;
        }

        push(1);

        i = 0;
        while(1) {
                while(i >= 0 && i < n) {
                        //print_p();
                        p[i]++;
                        //printf("i = %d\n", i);
                        //printf("p[%d] = %d\n", i, p[i]);
                        if (p[i] > d[i]) {
                                p[i] = c[i]-1;
                                x = pop();
                                i -= x;
                                cur_p--;
                                continue;
                        }

                        for(j = i+1; j < i+p[i]; ++j) {
                                err = 0;
                                if (p[i] < c[j] || p[i] > d[j]) {
                                        err = 1;
                                        break;
                                }
                        }
                        if (err) {
                                continue;
                        }
                        push(p[i]);
                        i += p[i];
                        cur_p++;
                }
                if (i < 0) {
                        return max_p;
                }
//                print_result();
                if (max_p < cur_p) {
                        max_p = cur_p;
                        max_n = 1;
                } else {
                        if (max_p == cur_p) {
                                max_n++;
                        }
                }
                //printf("max %d %d\n", max_p, max_n);
                //printf("cur %d\n", cur_p);

                x = pop();
                i -= x;
                cur_p--;
        }
}

int main()
{
        int i;

        scanf("%d\n", &n);
        for(i = 0; i < n; ++i) {
                scanf("%d %d\n", &c[i], &d[i]);
        }

        if (check()) {
                printf("%d %d\n", max_p, max_n);
        } else {
                printf("NIE\n");
        }

        return 0;
}