#include <cstdio> #include <vector> #include <algorithm> using namespace std; const int maxN = 1000 * 1000 + 10; const int m = 1000 * 1000 * 1000 + 7; const int r = 1 << 20; int t[maxN][2];//8 int fromTo[maxN][2];//8 int Max[maxN];//4 int Amount[maxN];//4 int n; int treeMax[r << 1];//8 int treeSum[r << 1];//8 vector<int>toAdd[maxN];//16 vector<int>mins;//4 vector<int>maxs;//4 int min_on_poz(int i); int max_on_poz(int i); void add_min(int i); void add_max(int i); void setValMax(int x, int val); int getMax(int a, int b); void setValSum(int x, int val); int getSum(int a, int b); void ps() { printf("mins:"); for(int i = 0; i < mins.size(); ++i) printf(" %d[%d]", mins[i], t[mins[i]][0]); printf("\nmaxs:"); for(int i = 0; i < maxs.size(); ++i) printf(" %d[%d]", maxs[i], t[maxs[i]][1]); printf("\n"); } int get_point(int i) { // printf("---%d---\n", i); // ps(); int a = 0, b = i; while(b - a > 1) { int sr = (a + b) >> 1; // printf("a:%d b:%d sr:%d(mn:%d, mx:%d, am: %d)\n", a, b, sr, // t[min_on_poz(sr)][0], t[max_on_poz(sr)][1], (i - sr + 1)); if(t[max_on_poz(sr)][1] < (i - sr + 1) || t[max_on_poz(sr)][1] < t[min_on_poz(sr)][0]) a = sr; else b = sr; } return a; } int I[maxN]; void ps2() { for(int i = 0; i <= n; ++i) printf("%d[%d] ", i, treeSum[r + i]); printf("\n"); } int Sum; int main() { scanf("%d", &n); t[0][0] = m; t[0][1] = 0; add_min(0); add_max(0); Amount[0] = 1; setValSum(0, 1); for(int i = 1; i <= n; ++i) { //ps2(); for(int j = 0; j < toAdd[i].size(); ++j) { setValMax(toAdd[i][j], Max[toAdd[i][j]]); setValSum(toAdd[i][j], Amount[toAdd[i][j]]); } I[i] = i; scanf("%d%d", t[i], t[i] + 1); add_min(i); add_max(i); fromTo[i][0] = max(get_point(i), i - t[i][1]); fromTo[i][1] = i - t[i][0]; toAdd[i + t[i][0] - 1].push_back(i); if(fromTo[i][0] <= fromTo[i][1]) { Max[i] = getMax(fromTo[i][0], fromTo[i][1]) + 1; Amount[i] = Sum; } if(t[i][0] == 1) { setValMax(i, Max[i]); setValSum(i, Amount[i]); } // printf("a[%d]: <%d %d> = %d\n", I[i], fromTo[i][0], fromTo[i][1], Amount[i]); }/* sort(I + 1, I + n + 1, [](int a, int b)->bool { return Max[a] < Max[b]; }); Amount[0] = 1; for(int i = 1; i <= n; ++i) { if(Max[I[i]] == 1) setValSum(0, 1); for(int j = 0; j < toAdd[i].size(); ++j) setValSum(toAdd[i][j], Amount[toAdd[i][j]]); int j; ps2(); for(j = i; Max[I[j]] == Max[I[j + 1]]; ++j) { Amount[I[j]] = getSum(fromTo[I[j]][0], fromTo[I[j]][1]); printf("a[%d]: <%d %d> = %d\n", I[j], fromTo[I[j]][0], fromTo[I[j]][1], Amount[I[j]]); } Amount[I[j]] = getSum(fromTo[I[j]][0], fromTo[I[j]][1]); printf("a[%d]: <%d %d> = %d\n", I[j], fromTo[I[j]][0], fromTo[I[j]][1], Amount[I[j]]); printf("(%d %d)\n", i, j); int x; for(x = i - 1; x > 0 && Max[I[x - 1]] == Max[I[x]]; --x) setValSum(I[x], 0); setValSum(I[x], 0); for(x = i; x <= j; ++x) setValSum(I[x], Amount[I[x]]); i = j; } for(int i = 1; i <= n; ++i) printf("%d: %d %d\n", i, Max[i], Amount[i]);*/ if(Amount[n] == 0) printf("NIE\n"); else printf("%d %d\n", Max[n], Amount[n]); return 0; } int min_on_poz(int i) { // printf("----%d-----\n", i); // ps(); int a = 0, b = mins.size() - 1; while(b - a > 1) { int sr = (a + b) >> 1; // printf("a:%d b:%d sr:%d val:%d\n", a, b, sr, mins[sr]); if(mins[sr] < i) a = sr; else b = sr; } // printf(">%d\n", mins[b]); return mins[b]; } int getSum(int a, int b) { a += r; b += r; int sum = 0; while(a < b) { if(a & 1) { sum += treeSum[a]; sum %= m; a >>= 1; ++a; } else a >>= 1; if(b & 1) b >>= 1; else { sum +=treeSum[b]; sum %= m; b >>= 1; --b; } } if(a == b) { sum += treeSum[a]; sum %= m; } return sum; } void setValSum(int x, int val) { x += r; treeSum[x] = val; x >>= 1; while(x) { treeSum[x] = 0; if(treeMax[x << 1] == treeMax[x]) treeSum[x] += treeSum[x << 1]; if(treeMax[(x << 1) + 1] == treeMax[x]) treeSum[x] += treeSum[(x << 1) + 1]; x >>= 1; } } int getMax(int a, int b) { a += r; b += r; int mx = 0; Sum = 0; while(a < b) { if(a & 1) { if(treeMax[a] > mx) Sum = 0; mx = treeMax[a] > mx ? treeMax[a] : mx; if(treeMax[a] == mx) Sum += treeSum[a]; a >>= 1; ++a; } else a >>= 1; if(b & 1) b >>= 1; else { if(treeMax[b] > mx) Sum = 0; mx = treeMax[b] > mx ? treeMax[b] : mx; if(treeMax[b] == mx) Sum += treeSum[b]; b >>= 1; --b; } } if(a == b) { if(treeMax[b] > mx) Sum = 0; mx = mx < treeMax[a] ? treeMax[b] : mx; if(treeMax[b] == mx) Sum += treeSum[b]; } return mx; } void setValMax(int x, int val) { x += r; treeMax[x] = val; x >>= 1; while(x) { treeMax[x] = treeMax[x << 1] > treeMax[(x << 1) + 1] ? treeMax[x << 1] : treeMax[(x << 1) + 1]; x >>= 1; } } int max_on_poz(int i) { int a = 0, b = maxs.size() - 1; while(b - a > 1) { int sr = (a + b) >> 1; if(maxs[sr] < i) a = sr; else b = sr; } return maxs[b]; } void add_min(int i) { while(mins.size() && t[mins[mins.size() - 1]][0] <= t[i][0]) mins.pop_back(); mins.push_back(i); } void add_max(int i) { while(maxs.size() && t[maxs[maxs.size() - 1]][1] >= t[i][1]) maxs.pop_back(); maxs.push_back(i); }
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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; const int maxN = 1000 * 1000 + 10; const int m = 1000 * 1000 * 1000 + 7; const int r = 1 << 20; int t[maxN][2];//8 int fromTo[maxN][2];//8 int Max[maxN];//4 int Amount[maxN];//4 int n; int treeMax[r << 1];//8 int treeSum[r << 1];//8 vector<int>toAdd[maxN];//16 vector<int>mins;//4 vector<int>maxs;//4 int min_on_poz(int i); int max_on_poz(int i); void add_min(int i); void add_max(int i); void setValMax(int x, int val); int getMax(int a, int b); void setValSum(int x, int val); int getSum(int a, int b); void ps() { printf("mins:"); for(int i = 0; i < mins.size(); ++i) printf(" %d[%d]", mins[i], t[mins[i]][0]); printf("\nmaxs:"); for(int i = 0; i < maxs.size(); ++i) printf(" %d[%d]", maxs[i], t[maxs[i]][1]); printf("\n"); } int get_point(int i) { // printf("---%d---\n", i); // ps(); int a = 0, b = i; while(b - a > 1) { int sr = (a + b) >> 1; // printf("a:%d b:%d sr:%d(mn:%d, mx:%d, am: %d)\n", a, b, sr, // t[min_on_poz(sr)][0], t[max_on_poz(sr)][1], (i - sr + 1)); if(t[max_on_poz(sr)][1] < (i - sr + 1) || t[max_on_poz(sr)][1] < t[min_on_poz(sr)][0]) a = sr; else b = sr; } return a; } int I[maxN]; void ps2() { for(int i = 0; i <= n; ++i) printf("%d[%d] ", i, treeSum[r + i]); printf("\n"); } int Sum; int main() { scanf("%d", &n); t[0][0] = m; t[0][1] = 0; add_min(0); add_max(0); Amount[0] = 1; setValSum(0, 1); for(int i = 1; i <= n; ++i) { //ps2(); for(int j = 0; j < toAdd[i].size(); ++j) { setValMax(toAdd[i][j], Max[toAdd[i][j]]); setValSum(toAdd[i][j], Amount[toAdd[i][j]]); } I[i] = i; scanf("%d%d", t[i], t[i] + 1); add_min(i); add_max(i); fromTo[i][0] = max(get_point(i), i - t[i][1]); fromTo[i][1] = i - t[i][0]; toAdd[i + t[i][0] - 1].push_back(i); if(fromTo[i][0] <= fromTo[i][1]) { Max[i] = getMax(fromTo[i][0], fromTo[i][1]) + 1; Amount[i] = Sum; } if(t[i][0] == 1) { setValMax(i, Max[i]); setValSum(i, Amount[i]); } // printf("a[%d]: <%d %d> = %d\n", I[i], fromTo[i][0], fromTo[i][1], Amount[i]); }/* sort(I + 1, I + n + 1, [](int a, int b)->bool { return Max[a] < Max[b]; }); Amount[0] = 1; for(int i = 1; i <= n; ++i) { if(Max[I[i]] == 1) setValSum(0, 1); for(int j = 0; j < toAdd[i].size(); ++j) setValSum(toAdd[i][j], Amount[toAdd[i][j]]); int j; ps2(); for(j = i; Max[I[j]] == Max[I[j + 1]]; ++j) { Amount[I[j]] = getSum(fromTo[I[j]][0], fromTo[I[j]][1]); printf("a[%d]: <%d %d> = %d\n", I[j], fromTo[I[j]][0], fromTo[I[j]][1], Amount[I[j]]); } Amount[I[j]] = getSum(fromTo[I[j]][0], fromTo[I[j]][1]); printf("a[%d]: <%d %d> = %d\n", I[j], fromTo[I[j]][0], fromTo[I[j]][1], Amount[I[j]]); printf("(%d %d)\n", i, j); int x; for(x = i - 1; x > 0 && Max[I[x - 1]] == Max[I[x]]; --x) setValSum(I[x], 0); setValSum(I[x], 0); for(x = i; x <= j; ++x) setValSum(I[x], Amount[I[x]]); i = j; } for(int i = 1; i <= n; ++i) printf("%d: %d %d\n", i, Max[i], Amount[i]);*/ if(Amount[n] == 0) printf("NIE\n"); else printf("%d %d\n", Max[n], Amount[n]); return 0; } int min_on_poz(int i) { // printf("----%d-----\n", i); // ps(); int a = 0, b = mins.size() - 1; while(b - a > 1) { int sr = (a + b) >> 1; // printf("a:%d b:%d sr:%d val:%d\n", a, b, sr, mins[sr]); if(mins[sr] < i) a = sr; else b = sr; } // printf(">%d\n", mins[b]); return mins[b]; } int getSum(int a, int b) { a += r; b += r; int sum = 0; while(a < b) { if(a & 1) { sum += treeSum[a]; sum %= m; a >>= 1; ++a; } else a >>= 1; if(b & 1) b >>= 1; else { sum +=treeSum[b]; sum %= m; b >>= 1; --b; } } if(a == b) { sum += treeSum[a]; sum %= m; } return sum; } void setValSum(int x, int val) { x += r; treeSum[x] = val; x >>= 1; while(x) { treeSum[x] = 0; if(treeMax[x << 1] == treeMax[x]) treeSum[x] += treeSum[x << 1]; if(treeMax[(x << 1) + 1] == treeMax[x]) treeSum[x] += treeSum[(x << 1) + 1]; x >>= 1; } } int getMax(int a, int b) { a += r; b += r; int mx = 0; Sum = 0; while(a < b) { if(a & 1) { if(treeMax[a] > mx) Sum = 0; mx = treeMax[a] > mx ? treeMax[a] : mx; if(treeMax[a] == mx) Sum += treeSum[a]; a >>= 1; ++a; } else a >>= 1; if(b & 1) b >>= 1; else { if(treeMax[b] > mx) Sum = 0; mx = treeMax[b] > mx ? treeMax[b] : mx; if(treeMax[b] == mx) Sum += treeSum[b]; b >>= 1; --b; } } if(a == b) { if(treeMax[b] > mx) Sum = 0; mx = mx < treeMax[a] ? treeMax[b] : mx; if(treeMax[b] == mx) Sum += treeSum[b]; } return mx; } void setValMax(int x, int val) { x += r; treeMax[x] = val; x >>= 1; while(x) { treeMax[x] = treeMax[x << 1] > treeMax[(x << 1) + 1] ? treeMax[x << 1] : treeMax[(x << 1) + 1]; x >>= 1; } } int max_on_poz(int i) { int a = 0, b = maxs.size() - 1; while(b - a > 1) { int sr = (a + b) >> 1; if(maxs[sr] < i) a = sr; else b = sr; } return maxs[b]; } void add_min(int i) { while(mins.size() && t[mins[mins.size() - 1]][0] <= t[i][0]) mins.pop_back(); mins.push_back(i); } void add_max(int i) { while(maxs.size() && t[maxs[maxs.size() - 1]][1] >= t[i][1]) maxs.pop_back(); maxs.push_back(i); } |