#include<cstdio> #include<algorithm> #include<iostream> #include<fstream> #include<iomanip> #include<vector> #include<map> #include<set> #include<stack> #define SC(n) scanf("%d", &n) #define SC2(n, m) scanf("%d %d", &n, &m) #define SCC(c) scanf(" %c", &c) #define SCS(b) scanf("%s", b) #define REP(i, n) for(int i = 0; i < (n); ++i) #define FORE(i, a, b) for(int i = (a); i <= (b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define FORIT(i, c, typ) for(typ::iterator i = c.begin(); i != c.end(); ++i) #define PR(n) printf("%d ", (int) (n)) #define PRN(n) printf("%d\n", (int) (n)) #define elif else if #define pb push_back #define mp make_pair #define xx first #define yy second #define all(v) v.begin(), v.end() #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef pair<int, pi> pii; typedef vector<pii> vpii; typedef vector<vpi> vvpi; typedef map<int, vpi> mvpi; const int NMAX = 1000*1000 + 7, MOD = 1000*1000*1000 + 7, TMAX = (1<<21)+5; int n, dfss; int c[NMAX], d[NMAX], b[NMAX], t[NMAX], been[NMAX]; int mag, tc[TMAX], td[TMAX]; void init() { mag = 1; while(mag < n) mag *= 2; REP(i, n) { // bb -> 0 tc[mag + i] = c[i]; td[mag + i] = d[i]; } FORE(i, mag + n, 2*mag-1) { tc[i] = 0; td[i] = n; } FORD(i, mag - 1, 1) { tc[i] = max(tc[2*i], tc[2*i+1]); td[i] = min(td[2*i], td[2*i+1]); } } int maxC(int l, int r) { prd("maxC %d %d: ", l, r); l += mag; r += mag; int ans = 0; while(l < r) { if(l % 2 == 1) { ans = max(ans, tc[l]); l = l/2 + 1; } else l /= 2; if(r % 2 == 0) { ans = max(ans, tc[r]); r = r/2 - 1; } else r /= 2; } if(l == r) ans = max(ans, tc[l]); prd("%d\n", ans); return ans; } int minD(int l, int r) { prd("midD %d %d: ", l, r); l += mag; r += mag; int ans = n; while(l < r) { if(l % 2 == 1) { ans = min(ans, td[l]); l = l/2 + 1; } else l /= 2; if(r % 2 == 0) { ans = min(ans, td[r]); r = r/2 - 1; } else r /= 2; } if(l == r) ans = min(ans, td[l]); prd("%d\n", ans); return ans; } void dfs(int pos) { if(0) { ++dfss; //if(dfss % 100 == 0) printf("%d\n", dfss); } int cc = c[pos], dd = d[pos]; int len = 1, bb = 0, tt = 1, j1 = pos, j2 = pos; while(j2 < n - 1 && len <= dd) { prd("j2: %d\n", j2); if(cc <= len) { if(been[j2] == 0) { dfs(j2 + 1); been[j2] = 1; } int bj = b[j2 + 1]; if(bj) { if(bj >= bb) { bb = bj + 1; tt = t[j2 + 1]; } else if(bj + 1 == bb) tt = (tt + t[j2 + 1]) % MOD; } } int j3 = j2; if(j2 + 1 >= pos + cc - 1) { ++j2; cc = max(cc, c[j2]); dd = min(dd, d[j2]); ++len; } else { j2 = pos + cc - 1; if(j2 >= n) break; cc = max(cc, maxC(j1, j2)); dd = min(dd, minD(j1, j2)); len = j2 - pos + 1; } j1 = j3; } if(j2 == n-1 && cc <= len && len <= dd) { if(bb == 0) { bb = 1; tt = 1; } } prd("dfs %d: %d %d\n", pos, bb, tt); t[pos] = tt; b[pos] = bb; } int main() { SC(n); REP(i, n) { SC2(c[i], d[i]); } init(); dfs(0); if(b[0]) printf("%d %d\n", b[0], t[0]); else printf("NIE"); 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 | #include<cstdio> #include<algorithm> #include<iostream> #include<fstream> #include<iomanip> #include<vector> #include<map> #include<set> #include<stack> #define SC(n) scanf("%d", &n) #define SC2(n, m) scanf("%d %d", &n, &m) #define SCC(c) scanf(" %c", &c) #define SCS(b) scanf("%s", b) #define REP(i, n) for(int i = 0; i < (n); ++i) #define FORE(i, a, b) for(int i = (a); i <= (b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define FORIT(i, c, typ) for(typ::iterator i = c.begin(); i != c.end(); ++i) #define PR(n) printf("%d ", (int) (n)) #define PRN(n) printf("%d\n", (int) (n)) #define elif else if #define pb push_back #define mp make_pair #define xx first #define yy second #define all(v) v.begin(), v.end() #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef pair<int, pi> pii; typedef vector<pii> vpii; typedef vector<vpi> vvpi; typedef map<int, vpi> mvpi; const int NMAX = 1000*1000 + 7, MOD = 1000*1000*1000 + 7, TMAX = (1<<21)+5; int n, dfss; int c[NMAX], d[NMAX], b[NMAX], t[NMAX], been[NMAX]; int mag, tc[TMAX], td[TMAX]; void init() { mag = 1; while(mag < n) mag *= 2; REP(i, n) { // bb -> 0 tc[mag + i] = c[i]; td[mag + i] = d[i]; } FORE(i, mag + n, 2*mag-1) { tc[i] = 0; td[i] = n; } FORD(i, mag - 1, 1) { tc[i] = max(tc[2*i], tc[2*i+1]); td[i] = min(td[2*i], td[2*i+1]); } } int maxC(int l, int r) { prd("maxC %d %d: ", l, r); l += mag; r += mag; int ans = 0; while(l < r) { if(l % 2 == 1) { ans = max(ans, tc[l]); l = l/2 + 1; } else l /= 2; if(r % 2 == 0) { ans = max(ans, tc[r]); r = r/2 - 1; } else r /= 2; } if(l == r) ans = max(ans, tc[l]); prd("%d\n", ans); return ans; } int minD(int l, int r) { prd("midD %d %d: ", l, r); l += mag; r += mag; int ans = n; while(l < r) { if(l % 2 == 1) { ans = min(ans, td[l]); l = l/2 + 1; } else l /= 2; if(r % 2 == 0) { ans = min(ans, td[r]); r = r/2 - 1; } else r /= 2; } if(l == r) ans = min(ans, td[l]); prd("%d\n", ans); return ans; } void dfs(int pos) { if(0) { ++dfss; //if(dfss % 100 == 0) printf("%d\n", dfss); } int cc = c[pos], dd = d[pos]; int len = 1, bb = 0, tt = 1, j1 = pos, j2 = pos; while(j2 < n - 1 && len <= dd) { prd("j2: %d\n", j2); if(cc <= len) { if(been[j2] == 0) { dfs(j2 + 1); been[j2] = 1; } int bj = b[j2 + 1]; if(bj) { if(bj >= bb) { bb = bj + 1; tt = t[j2 + 1]; } else if(bj + 1 == bb) tt = (tt + t[j2 + 1]) % MOD; } } int j3 = j2; if(j2 + 1 >= pos + cc - 1) { ++j2; cc = max(cc, c[j2]); dd = min(dd, d[j2]); ++len; } else { j2 = pos + cc - 1; if(j2 >= n) break; cc = max(cc, maxC(j1, j2)); dd = min(dd, minD(j1, j2)); len = j2 - pos + 1; } j1 = j3; } if(j2 == n-1 && cc <= len && len <= dd) { if(bb == 0) { bb = 1; tt = 1; } } prd("dfs %d: %d %d\n", pos, bb, tt); t[pos] = tt; b[pos] = bb; } int main() { SC(n); REP(i, n) { SC2(c[i], d[i]); } init(); dfs(0); if(b[0]) printf("%d %d\n", b[0], t[0]); else printf("NIE"); return 0; } |