#include <cstdio> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <list> #include <queue> #include <deque> #include <cctype> #include <string> #include <vector> #include <sstream> #include <iterator> #include <numeric> #include <cmath> using namespace std; typedef vector <int > VI; typedef vector < VI > VVI; typedef long long LL; typedef vector < LL > VLL; typedef vector < double > VD; typedef vector < string > VS; typedef pair<int,int> PII; typedef vector <PII> VPII; typedef istringstream ISS; #define ALL(x) x.begin(),x.end() #define REP(i,n) for (int i=0; i<(n); ++i) #define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var) #define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var) #define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) #define PB push_back #define PF push_front #define MP(a,b) make_pair(a,b) #define ST first #define ND second #define SIZE(x) (int)x.size() const int N = 1010 * 1010; int ogr[N][2]; // [a,b] int lewo[N]; // lewo[i] - gdzie najbardziej w lewo moze sie zaczynac przedzial konczacy sie w i int n; PII q[N]; void licz_lewo() { int a = 0, b = 0; int pos = n-1; FORD(i,n-1,0) { while (pos >= 0 && ogr[pos][1] >= i-pos+1 && (a == b || q[a].ST >= i-pos+1)) { while (a < b && q[b-1].ST >= ogr[pos][1]) b--; q[b++] = MP(ogr[pos][1], pos); pos--; } lewo[i] = pos+1; if (a < b && q[a].ND == i) a++; } // REP(i,n) printf("lewo[%d]=%d\n", i, lewo[i]); } const int BASE = 1024 * 1024; const int MOD = 1000000007; int drzewo[2*BASE][2]; int when_started[N]; int drzewo2[2*BASE]; void drzewo_update(int pos, int num_parts, int num_sols) { // printf("update pos=%d num_parts=%d num_sols=%d\n", pos, num_parts, num_sols); pos += BASE; while (pos >= 1) { if (drzewo[pos][0] < num_parts) { drzewo[pos][0] = num_parts; drzewo[pos][1] = num_sols; } else if (drzewo[pos][0] == num_parts) { drzewo[pos][1] = (drzewo[pos][1] + num_sols) % MOD; } pos >>= 1; } } inline void dodaj(int a, int b, int &c, int &d) { if (a > c) { c = a; d = b; } else if (a == c) { d = (d + b) % MOD; } } void drzewo_przedzial(int a, int b, int &num_parts, int &num_sols) { a += BASE; b += BASE; while (a < b) { if (a & 1){ dodaj(drzewo[a][0], drzewo[a][1], num_parts, num_sols); a++; } if (!(b & 1)) { dodaj(drzewo[b][0], drzewo[b][1], num_parts, num_sols); b--; } a >>= 1; b >>= 1; } if (a == b) dodaj(drzewo[a][0], drzewo[a][1], num_parts, num_sols); } int drzewo2_max(int a,int b) { a += BASE; b += BASE; int res = 0; while (a < b) { if (a&1) res = max(res, drzewo2[a++]); if (!(b&1)) res = max(res, drzewo2[b--]); a >>= 1; b >>= 1; } if (a == b) res = max(res, drzewo2[a]); return res; } int lewy_koniec(int a, int b, int big) { while (true) { int nbig = drzewo2_max(b-big+1, b); if (nbig == big) break; else big = nbig; } return b-big+1; } void solve() { REP(i,n) drzewo2[BASE+i] = ogr[i][0]; FORD(i,BASE-1,1) drzewo2[i] = max(drzewo2[2*i], drzewo2[2*i+1]); int x = 0; priority_queue<PII> q; REP(i,n) when_started[i] = -1; //not active REP(i,n) { x = max(x, ogr[i][0]); if (i+1 >= x) { int l = -lewy_koniec(0, i, x); //printf("wrzucam (%d,%d)\n", l, i); q.push(MP(l, i)); when_started[i] = 0; } } drzewo_update(0, 0, 1); REP(i,n) { //printf("\n\nnowa runda i=%d\n",i); //dodac nowe if (i) { int gr = i-1+ogr[i-1][0]; //dodajemy konce < gr gr = min(gr, n); int pos = i; int big = 0; while (pos < gr) { big = max(big, ogr[pos++][0]); while (pos <= gr && i+big > pos) { int npos = i+big; big = max(big, drzewo2_max(pos, min(i+big-1,n-1))); pos = npos; } if (pos <= gr) { //&& when_started[pos-1] == -1 //nowy przedzial int l = -lewy_koniec(i, pos-1, big); //printf("i=%d wrzucam (%d,%d)\n", i, l, pos-1); q.push(MP(l, pos-1)); when_started[pos-1] = i; } } } //wyrzucic skonczone while (!q.empty() && -q.top().ST == i) { PII p = q.top(); q.pop(); x = p.ND; //od when_started[x] do i wlacznie int beg = max(when_started[x], lewo[x]); //printf("x=%d beg=%d i=%d when_started=%d lewo=%d\n", x, beg, i, when_started[x], lewo[x]); if (beg <= i) { int num_parts = 0, num_sols = 0; drzewo_przedzial(beg, i, num_parts, num_sols); if (num_parts || num_sols) { num_parts++; drzewo_update(x+1, num_parts, num_sols); } } when_started[x] = -1; } } // REP(i,n+1) printf("i=%d (%d,%d)\n", i, drzewo[BASE+i][0], drzewo[BASE+i][1]); if (drzewo[BASE+n][0] == 0) puts("NIE"); else printf("%d %d\n", drzewo[BASE+n][0], drzewo[BASE+n][1] % MOD); } int main(){ scanf("%d",&n); REP(i,n) REP(j,2) scanf("%d",ogr[i]+j); licz_lewo(); solve(); return 0; }
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 | #include <cstdio> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <list> #include <queue> #include <deque> #include <cctype> #include <string> #include <vector> #include <sstream> #include <iterator> #include <numeric> #include <cmath> using namespace std; typedef vector <int > VI; typedef vector < VI > VVI; typedef long long LL; typedef vector < LL > VLL; typedef vector < double > VD; typedef vector < string > VS; typedef pair<int,int> PII; typedef vector <PII> VPII; typedef istringstream ISS; #define ALL(x) x.begin(),x.end() #define REP(i,n) for (int i=0; i<(n); ++i) #define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var) #define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var) #define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) #define PB push_back #define PF push_front #define MP(a,b) make_pair(a,b) #define ST first #define ND second #define SIZE(x) (int)x.size() const int N = 1010 * 1010; int ogr[N][2]; // [a,b] int lewo[N]; // lewo[i] - gdzie najbardziej w lewo moze sie zaczynac przedzial konczacy sie w i int n; PII q[N]; void licz_lewo() { int a = 0, b = 0; int pos = n-1; FORD(i,n-1,0) { while (pos >= 0 && ogr[pos][1] >= i-pos+1 && (a == b || q[a].ST >= i-pos+1)) { while (a < b && q[b-1].ST >= ogr[pos][1]) b--; q[b++] = MP(ogr[pos][1], pos); pos--; } lewo[i] = pos+1; if (a < b && q[a].ND == i) a++; } // REP(i,n) printf("lewo[%d]=%d\n", i, lewo[i]); } const int BASE = 1024 * 1024; const int MOD = 1000000007; int drzewo[2*BASE][2]; int when_started[N]; int drzewo2[2*BASE]; void drzewo_update(int pos, int num_parts, int num_sols) { // printf("update pos=%d num_parts=%d num_sols=%d\n", pos, num_parts, num_sols); pos += BASE; while (pos >= 1) { if (drzewo[pos][0] < num_parts) { drzewo[pos][0] = num_parts; drzewo[pos][1] = num_sols; } else if (drzewo[pos][0] == num_parts) { drzewo[pos][1] = (drzewo[pos][1] + num_sols) % MOD; } pos >>= 1; } } inline void dodaj(int a, int b, int &c, int &d) { if (a > c) { c = a; d = b; } else if (a == c) { d = (d + b) % MOD; } } void drzewo_przedzial(int a, int b, int &num_parts, int &num_sols) { a += BASE; b += BASE; while (a < b) { if (a & 1){ dodaj(drzewo[a][0], drzewo[a][1], num_parts, num_sols); a++; } if (!(b & 1)) { dodaj(drzewo[b][0], drzewo[b][1], num_parts, num_sols); b--; } a >>= 1; b >>= 1; } if (a == b) dodaj(drzewo[a][0], drzewo[a][1], num_parts, num_sols); } int drzewo2_max(int a,int b) { a += BASE; b += BASE; int res = 0; while (a < b) { if (a&1) res = max(res, drzewo2[a++]); if (!(b&1)) res = max(res, drzewo2[b--]); a >>= 1; b >>= 1; } if (a == b) res = max(res, drzewo2[a]); return res; } int lewy_koniec(int a, int b, int big) { while (true) { int nbig = drzewo2_max(b-big+1, b); if (nbig == big) break; else big = nbig; } return b-big+1; } void solve() { REP(i,n) drzewo2[BASE+i] = ogr[i][0]; FORD(i,BASE-1,1) drzewo2[i] = max(drzewo2[2*i], drzewo2[2*i+1]); int x = 0; priority_queue<PII> q; REP(i,n) when_started[i] = -1; //not active REP(i,n) { x = max(x, ogr[i][0]); if (i+1 >= x) { int l = -lewy_koniec(0, i, x); //printf("wrzucam (%d,%d)\n", l, i); q.push(MP(l, i)); when_started[i] = 0; } } drzewo_update(0, 0, 1); REP(i,n) { //printf("\n\nnowa runda i=%d\n",i); //dodac nowe if (i) { int gr = i-1+ogr[i-1][0]; //dodajemy konce < gr gr = min(gr, n); int pos = i; int big = 0; while (pos < gr) { big = max(big, ogr[pos++][0]); while (pos <= gr && i+big > pos) { int npos = i+big; big = max(big, drzewo2_max(pos, min(i+big-1,n-1))); pos = npos; } if (pos <= gr) { //&& when_started[pos-1] == -1 //nowy przedzial int l = -lewy_koniec(i, pos-1, big); //printf("i=%d wrzucam (%d,%d)\n", i, l, pos-1); q.push(MP(l, pos-1)); when_started[pos-1] = i; } } } //wyrzucic skonczone while (!q.empty() && -q.top().ST == i) { PII p = q.top(); q.pop(); x = p.ND; //od when_started[x] do i wlacznie int beg = max(when_started[x], lewo[x]); //printf("x=%d beg=%d i=%d when_started=%d lewo=%d\n", x, beg, i, when_started[x], lewo[x]); if (beg <= i) { int num_parts = 0, num_sols = 0; drzewo_przedzial(beg, i, num_parts, num_sols); if (num_parts || num_sols) { num_parts++; drzewo_update(x+1, num_parts, num_sols); } } when_started[x] = -1; } } // REP(i,n+1) printf("i=%d (%d,%d)\n", i, drzewo[BASE+i][0], drzewo[BASE+i][1]); if (drzewo[BASE+n][0] == 0) puts("NIE"); else printf("%d %d\n", drzewo[BASE+n][0], drzewo[BASE+n][1] % MOD); } int main(){ scanf("%d",&n); REP(i,n) REP(j,2) scanf("%d",ogr[i]+j); licz_lewo(); solve(); return 0; } |