// Artur Kraska, II UWr #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <cmath> #include <list> #include <set> #include <map> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) ; #define M 1000000007 #define INF 1000000007 using namespace std; int n, S; int d_minn[3000007], d_maks[3000007]; struct elem { int minn, maks; int w, ile; }; elem tab[1000007]; static inline int wez_minn(const int &nr, const int &p, const int &k, const int &pocz, const int &kon) { if(pocz == p && kon == k) { return d_minn[nr]; } int s = (p+k)>>1; if(kon <= s) return wez_minn(nr*2, p, s, pocz, kon); if(pocz > s) return wez_minn(nr*2+1, s+1, k, pocz, kon); return max(wez_minn(nr*2, p, s, pocz, s), wez_minn(nr*2+1, s+1, k, s+1, kon)); } static inline int wez_maks(const int &nr, const int &p, const int &k, const int &pocz, const int &kon) { if(pocz == p && kon == k) { return d_maks[nr]; } int s = (p+k)>>1; if(kon <= s) return wez_maks(nr*2, p, s, pocz, kon); if(pocz > s) return wez_maks(nr*2+1, s+1, k, pocz, kon); return min(wez_maks(nr*2, p, s, pocz, s), wez_maks(nr*2+1, s+1, k, s+1, kon)); } int main() { scanf("%d", &n); S = 1; while(S < n) S *= 2; forr(i, n) { scanf("%d %d", &tab[i].minn, &tab[i].maks); d_minn[S+i] = tab[i].minn; d_maks[S+i] = tab[i].maks; } FOR(i, S+n, 2*S) { d_minn[i] = -1; d_maks[i] = INF; } FORD(i, S-1, 1) { d_minn[i] = max(d_minn[i*2], d_minn[i*2+1]); d_maks[i] = min(d_maks[i*2], d_maks[i*2+1]); } tab[n].ile = 1; tab[n].w = 0; tab[n].minn = INF; tab[n].maks = 0; FORD(i, n-1, 0) { deb(cout << "i: " << i << endl); tab[i].w = 0; tab[i].ile = 0; int minn = wez_minn(1, 0, S-1, i, min(n-1, i+tab[i].minn-1)); int maks = wez_maks(1, 0, S-1, i, min(n-1, i+tab[i].minn-1)); for(int j=i+tab[i].minn-1; j < n && j-i+1 <= maks; j++) { minn = max(minn, tab[j].minn); maks = min(maks, tab[j].maks); deb(cout << "j: " << j << ", maks: " << maks << ", minn: " << minn << endl); if(j-i+1 <= maks && j-i+1 >= minn && tab[j+1].ile > 0) { if(tab[j+1].w+1 > tab[i].w) { tab[i].w = tab[j+1].w+1; tab[i].ile = tab[j+1].ile; } else if(tab[j+1].w+1 == tab[i].w) { tab[i].ile = (tab[i].ile + tab[j+1].ile) % M; } } } } if(tab[0].w > 0) printf("%d %d\n", tab[0].w, tab[0].ile); else printf("NIE\n"); 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 | // Artur Kraska, II UWr #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <cmath> #include <list> #include <set> #include <map> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) ; #define M 1000000007 #define INF 1000000007 using namespace std; int n, S; int d_minn[3000007], d_maks[3000007]; struct elem { int minn, maks; int w, ile; }; elem tab[1000007]; static inline int wez_minn(const int &nr, const int &p, const int &k, const int &pocz, const int &kon) { if(pocz == p && kon == k) { return d_minn[nr]; } int s = (p+k)>>1; if(kon <= s) return wez_minn(nr*2, p, s, pocz, kon); if(pocz > s) return wez_minn(nr*2+1, s+1, k, pocz, kon); return max(wez_minn(nr*2, p, s, pocz, s), wez_minn(nr*2+1, s+1, k, s+1, kon)); } static inline int wez_maks(const int &nr, const int &p, const int &k, const int &pocz, const int &kon) { if(pocz == p && kon == k) { return d_maks[nr]; } int s = (p+k)>>1; if(kon <= s) return wez_maks(nr*2, p, s, pocz, kon); if(pocz > s) return wez_maks(nr*2+1, s+1, k, pocz, kon); return min(wez_maks(nr*2, p, s, pocz, s), wez_maks(nr*2+1, s+1, k, s+1, kon)); } int main() { scanf("%d", &n); S = 1; while(S < n) S *= 2; forr(i, n) { scanf("%d %d", &tab[i].minn, &tab[i].maks); d_minn[S+i] = tab[i].minn; d_maks[S+i] = tab[i].maks; } FOR(i, S+n, 2*S) { d_minn[i] = -1; d_maks[i] = INF; } FORD(i, S-1, 1) { d_minn[i] = max(d_minn[i*2], d_minn[i*2+1]); d_maks[i] = min(d_maks[i*2], d_maks[i*2+1]); } tab[n].ile = 1; tab[n].w = 0; tab[n].minn = INF; tab[n].maks = 0; FORD(i, n-1, 0) { deb(cout << "i: " << i << endl); tab[i].w = 0; tab[i].ile = 0; int minn = wez_minn(1, 0, S-1, i, min(n-1, i+tab[i].minn-1)); int maks = wez_maks(1, 0, S-1, i, min(n-1, i+tab[i].minn-1)); for(int j=i+tab[i].minn-1; j < n && j-i+1 <= maks; j++) { minn = max(minn, tab[j].minn); maks = min(maks, tab[j].maks); deb(cout << "j: " << j << ", maks: " << maks << ", minn: " << minn << endl); if(j-i+1 <= maks && j-i+1 >= minn && tab[j+1].ile > 0) { if(tab[j+1].w+1 > tab[i].w) { tab[i].w = tab[j+1].w+1; tab[i].ile = tab[j+1].ile; } else if(tab[j+1].w+1 == tab[i].w) { tab[i].ile = (tab[i].ile + tab[j+1].ile) % M; } } } } if(tab[0].w > 0) printf("%d %d\n", tab[0].w, tab[0].ile); else printf("NIE\n"); return 0; } |