#include <vector> #include <list> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <string> #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 REP(i,a) FOR(i,0,a) #define MP make_pair #define PB push_back #define ST first #define ND second using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PII; typedef vector<PII> VII; typedef long long int LL; typedef unsigned long long int ULL; const int MAXN = 1000000+5; const int MAXV = MAXN; const int MAXT = 1<<21; const int MOD = 1000*1000*1000+7; int n; int lb[MAXN], ub[MAXN]; int dp_size[MAXN], dp_cnt[MAXN]; int tree[MAXT]; int tree_size; int active_count[MAXV], sum[MAXV]; bool killed[MAXN], active[MAXN]; int ubMaxPos; int query() { return tree[1]; } void tree_insert(int va, int val) { va = va + tree_size; tree[va] = val; va /= 2; while (va != 0) { tree[va] = max(tree[2*va], tree[2*va+1]); va /= 2; } } void insert(int i) { if (!killed[i] && !active[i] && i <= ubMaxPos) { active[i] = true; ++active_count[dp_size[i+1]]; sum[dp_size[i+1]] += dp_cnt[i+1]; sum[dp_size[i+1]] %= MOD; if (active_count[dp_size[i+1]] == 1) { tree_insert(dp_size[i+1], dp_size[i+1]); } } } void remove(int i) { if (!killed[i] && active[i]) { active[i] = false; --active_count[dp_size[i+1]]; sum[dp_size[i+1]] += MOD - dp_cnt[i+1]; sum[dp_size[i+1]] %= MOD; if (active_count[dp_size[i+1]] == 0) { tree_insert(dp_size[i+1], 0); } } } struct Interval { Interval(int _w, int _idxMin, int _idxMax) : w(_w), idxMin(_idxMin), idxMax(_idxMax) {} int lower(int idx, int ub) { if (w >= idxMin - idx + 1 && ub < idxMax - idx + 1) { FOR (i, max(idxMin, idx + ub), min(idxMax + 1, idx + w)) { remove(i); killed[i] = true; } } if (ub >= idxMin - idx + 1 && ub < idxMax - idx + 1) { return idx + ub - 1; } else if (ub < idxMin - idx + 1) { return idxMin - 1; } else { return n+1; } } void upper(int idx, int lb) { if (w <= idxMax - idx + 1 && lb > idxMin - idx + 1) { FOR (i, max(idxMin, idx + w - 1), min(min(idxMax + 1, lb + idx - 1), ubMaxPos + 1)) { remove(i); } } } int w, idxMin, idxMax; }; struct UpperIntervals { void init() { intervals.PB(Interval(n+1, n, n)); maxPos = n; ubMaxPos = n; maxPosIntervalIdx = 0; } void lower(int idx, int ub) { int i = intervals.size() - 1; Interval newInt(ub, idx, idx); while (i >= 0 && intervals[i].w > ub) { newInt.idxMax = intervals[i].idxMax; maxPos = min(maxPos, intervals[i].lower(idx+1, ub)); intervals.pop_back(); --i; } if (i >= 0 && intervals[i].w == ub) { intervals[i].idxMin = newInt.idxMin; } else { intervals.PB(newInt); } if (intervals.back().idxMin <= maxPos && maxPos <= intervals.back().idxMax) { maxPosIntervalIdx = intervals.size()-1; } } void step (int idx) { auto curr = intervals[maxPosIntervalIdx]; if (maxPos - idx == curr.w) { remove(maxPos); killed[maxPos] = true; --maxPos; if (maxPos < curr.idxMin) { ++maxPosIntervalIdx; } } ubMaxPos = maxPos; } void update(int idx, int ub) { lower(idx, ub); step(idx); } vector<Interval> intervals; int maxPos, maxPosIntervalIdx; }; struct LowerIntervals { void init() { intervals.PB(Interval(0, n, n)); activeIntervals.insert({-n, 0}); } void upper(int idx, int lb) { int i = intervals.size() - 1; Interval newInt(lb, idx, idx); while (i >= 0 && intervals[i].w <= lb) { newInt.idxMax = intervals[i].idxMax; intervals[i].upper(idx+1, lb); auto it = activeIntervals.find({-intervals[i].idxMin, i}); if (it != activeIntervals.end()) { activeIntervals.erase(it); } else { auto it = waitingIntervals.find({intervals[i].w - intervals[i].idxMax, i}); if (it != waitingIntervals.end()) { waitingIntervals.erase(it); } } intervals.pop_back(); --i; } intervals.PB(newInt); if (newInt.w - newInt.idxMax <= -idx) { activeIntervals.insert({-newInt.idxMin, intervals.size() - 1}); } else { waitingIntervals.insert({newInt.w - newInt.idxMax, intervals.size() - 1}); } } void step(int idx) { while (!activeIntervals.empty()) { auto it = activeIntervals.begin(); if (-(it->ST) > ubMaxPos) { activeIntervals.erase(it); } else { break; } } while (!waitingIntervals.empty()) { auto it = waitingIntervals.begin(); if (it->ST == 1-idx) { activeIntervals.insert({-intervals[it->ND].idxMin, it->ND}); waitingIntervals.erase(it); } else { break; } } auto it = activeIntervals.begin(); while (it != activeIntervals.end()) { auto x = idx + intervals[it->ND].w - 1; insert(x); if (x == -it->ST) { auto val = *it; activeIntervals.erase(val); } ++it; } } void update(int idx, int lb) { upper(idx, lb); step(idx); } vector<Interval> intervals; set<PII> activeIntervals, waitingIntervals; }; UpperIntervals up_int; LowerIntervals low_int; void dp(int i) { up_int.update(i, ub[i]); low_int.update(i, lb[i]); int max_size = query(); if (max_size != 0) { dp_size[i] = max_size + 1; dp_cnt[i] = sum[max_size]; } } void make_tree() { tree_size = 1; while (tree_size < n+1) { tree_size *= 2; } } void init() { dp_size[n] = 1; dp_cnt[n] = 1; up_int.init(); low_int.init(); make_tree(); } int main() { scanf("%d", &n); REP (i, n) { scanf("%d %d", &lb[i], &ub[i]); } init(); FORD (i, n-1, 0) { dp(i); } if (dp_size[0] == 0) { printf("NIE\n"); } else { printf("%d %d\n", dp_size[0]-1, dp_cnt[0]); } 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | #include <vector> #include <list> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <string> #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 REP(i,a) FOR(i,0,a) #define MP make_pair #define PB push_back #define ST first #define ND second using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PII; typedef vector<PII> VII; typedef long long int LL; typedef unsigned long long int ULL; const int MAXN = 1000000+5; const int MAXV = MAXN; const int MAXT = 1<<21; const int MOD = 1000*1000*1000+7; int n; int lb[MAXN], ub[MAXN]; int dp_size[MAXN], dp_cnt[MAXN]; int tree[MAXT]; int tree_size; int active_count[MAXV], sum[MAXV]; bool killed[MAXN], active[MAXN]; int ubMaxPos; int query() { return tree[1]; } void tree_insert(int va, int val) { va = va + tree_size; tree[va] = val; va /= 2; while (va != 0) { tree[va] = max(tree[2*va], tree[2*va+1]); va /= 2; } } void insert(int i) { if (!killed[i] && !active[i] && i <= ubMaxPos) { active[i] = true; ++active_count[dp_size[i+1]]; sum[dp_size[i+1]] += dp_cnt[i+1]; sum[dp_size[i+1]] %= MOD; if (active_count[dp_size[i+1]] == 1) { tree_insert(dp_size[i+1], dp_size[i+1]); } } } void remove(int i) { if (!killed[i] && active[i]) { active[i] = false; --active_count[dp_size[i+1]]; sum[dp_size[i+1]] += MOD - dp_cnt[i+1]; sum[dp_size[i+1]] %= MOD; if (active_count[dp_size[i+1]] == 0) { tree_insert(dp_size[i+1], 0); } } } struct Interval { Interval(int _w, int _idxMin, int _idxMax) : w(_w), idxMin(_idxMin), idxMax(_idxMax) {} int lower(int idx, int ub) { if (w >= idxMin - idx + 1 && ub < idxMax - idx + 1) { FOR (i, max(idxMin, idx + ub), min(idxMax + 1, idx + w)) { remove(i); killed[i] = true; } } if (ub >= idxMin - idx + 1 && ub < idxMax - idx + 1) { return idx + ub - 1; } else if (ub < idxMin - idx + 1) { return idxMin - 1; } else { return n+1; } } void upper(int idx, int lb) { if (w <= idxMax - idx + 1 && lb > idxMin - idx + 1) { FOR (i, max(idxMin, idx + w - 1), min(min(idxMax + 1, lb + idx - 1), ubMaxPos + 1)) { remove(i); } } } int w, idxMin, idxMax; }; struct UpperIntervals { void init() { intervals.PB(Interval(n+1, n, n)); maxPos = n; ubMaxPos = n; maxPosIntervalIdx = 0; } void lower(int idx, int ub) { int i = intervals.size() - 1; Interval newInt(ub, idx, idx); while (i >= 0 && intervals[i].w > ub) { newInt.idxMax = intervals[i].idxMax; maxPos = min(maxPos, intervals[i].lower(idx+1, ub)); intervals.pop_back(); --i; } if (i >= 0 && intervals[i].w == ub) { intervals[i].idxMin = newInt.idxMin; } else { intervals.PB(newInt); } if (intervals.back().idxMin <= maxPos && maxPos <= intervals.back().idxMax) { maxPosIntervalIdx = intervals.size()-1; } } void step (int idx) { auto curr = intervals[maxPosIntervalIdx]; if (maxPos - idx == curr.w) { remove(maxPos); killed[maxPos] = true; --maxPos; if (maxPos < curr.idxMin) { ++maxPosIntervalIdx; } } ubMaxPos = maxPos; } void update(int idx, int ub) { lower(idx, ub); step(idx); } vector<Interval> intervals; int maxPos, maxPosIntervalIdx; }; struct LowerIntervals { void init() { intervals.PB(Interval(0, n, n)); activeIntervals.insert({-n, 0}); } void upper(int idx, int lb) { int i = intervals.size() - 1; Interval newInt(lb, idx, idx); while (i >= 0 && intervals[i].w <= lb) { newInt.idxMax = intervals[i].idxMax; intervals[i].upper(idx+1, lb); auto it = activeIntervals.find({-intervals[i].idxMin, i}); if (it != activeIntervals.end()) { activeIntervals.erase(it); } else { auto it = waitingIntervals.find({intervals[i].w - intervals[i].idxMax, i}); if (it != waitingIntervals.end()) { waitingIntervals.erase(it); } } intervals.pop_back(); --i; } intervals.PB(newInt); if (newInt.w - newInt.idxMax <= -idx) { activeIntervals.insert({-newInt.idxMin, intervals.size() - 1}); } else { waitingIntervals.insert({newInt.w - newInt.idxMax, intervals.size() - 1}); } } void step(int idx) { while (!activeIntervals.empty()) { auto it = activeIntervals.begin(); if (-(it->ST) > ubMaxPos) { activeIntervals.erase(it); } else { break; } } while (!waitingIntervals.empty()) { auto it = waitingIntervals.begin(); if (it->ST == 1-idx) { activeIntervals.insert({-intervals[it->ND].idxMin, it->ND}); waitingIntervals.erase(it); } else { break; } } auto it = activeIntervals.begin(); while (it != activeIntervals.end()) { auto x = idx + intervals[it->ND].w - 1; insert(x); if (x == -it->ST) { auto val = *it; activeIntervals.erase(val); } ++it; } } void update(int idx, int lb) { upper(idx, lb); step(idx); } vector<Interval> intervals; set<PII> activeIntervals, waitingIntervals; }; UpperIntervals up_int; LowerIntervals low_int; void dp(int i) { up_int.update(i, ub[i]); low_int.update(i, lb[i]); int max_size = query(); if (max_size != 0) { dp_size[i] = max_size + 1; dp_cnt[i] = sum[max_size]; } } void make_tree() { tree_size = 1; while (tree_size < n+1) { tree_size *= 2; } } void init() { dp_size[n] = 1; dp_cnt[n] = 1; up_int.init(); low_int.init(); make_tree(); } int main() { scanf("%d", &n); REP (i, n) { scanf("%d %d", &lb[i], &ub[i]); } init(); FORD (i, n-1, 0) { dp(i); } if (dp_size[0] == 0) { printf("NIE\n"); } else { printf("%d %d\n", dp_size[0]-1, dp_cnt[0]); } return 0; } |