#include <algorithm> #include <iostream> #include <list> #include <vector> using namespace std; namespace { template <typename A, typename B> constexpr B mod(A a, B b) { return a >= 0 ? a % b : (a % b + b) % b; } class Modulo { constexpr static int M = 1000000007; int x; public: constexpr Modulo(): x(0) {} template <typename T> constexpr Modulo(T const& x_): x(mod(x_, M)) {} constexpr int get() const { return x; } }; template <typename OS> OS& operator<<(OS& s, Modulo x) { return s << x.get(); } constexpr Modulo operator+(Modulo const& a, Modulo const& b) { return Modulo{a.get() + b.get()}; } constexpr Modulo operator-(Modulo const& a, Modulo const& b) { return Modulo{a.get() - b.get()}; } constexpr Modulo operator*(Modulo const& a, Modulo const& b) { return Modulo{1LL * a.get() * b.get()}; } Modulo& operator+=(Modulo& a, Modulo const& b) { return a = a + b; } Modulo& operator-=(Modulo& a, Modulo const& b) { return a = a - b; } Modulo& operator*=(Modulo& a, Modulo const& b) { return a = a * b; } Modulo& operator++(Modulo& a) { return a += 1; } Modulo& operator--(Modulo& a) { return a -= 1; } Modulo operator++(Modulo a, int) { return a += 1; } Modulo operator--(Modulo a, int) { return a -= 1; } constexpr bool operator<(Modulo const& a, Modulo const& b) { return a.get() < b.get(); } constexpr bool operator<=(Modulo const& a, Modulo const& b) { return a.get() <= b.get(); } constexpr bool operator>(Modulo const& a, Modulo const& b) { return a.get() > b.get(); } constexpr bool operator>=(Modulo const& a, Modulo const& b) { return a.get() >= b.get(); } constexpr bool operator==(Modulo const& a, Modulo const& b) { return a.get() == b.get(); } constexpr bool operator!=(Modulo const& a, Modulo const& b) { return a.get() != b.get(); } typedef pair<int, Modulo> Result; Result const res0 = {-1, 0}; Result ms(Result const& a, Result const& b) { if (a.first == b.first) return {a.first, a.second+b.second}; if (a.first > b.first) return a; return b; } Result operator+(Result const& a, int b) { if (a.first < 0) return a; return {a.first + b, a.second}; } int rp2(int n) { int r = 1; while (r < n) r *= 2; return r; } class ST { int n; int N; vector<Result> data; public: ST(int n_): n{rp2(n_)}, data(2*n, res0) { } Result get(int i) const { i += n; return data[i]; } void set(int i, Result x) { i += n; data[i] = x; i /= 2; while (i > 0) { data[i] = ms(data[2*i], data[2*i+1]); i /= 2; } } Result query(int a, int b) { if (a >= b) return res0; a += n; Result res = data[a]; b += n; while (a/2 != b/2) { if (a % 2 == 0) res = ms(res, data[a+1]); if (b % 2 == 1) res = ms(res, data[b-1]); a /= 2; b /= 2; } return res; } }; Result solve(vector<pair<int, int>> data) { int n = data.size(); ST res(n+1); res.set(n, {0, 1}); list<pair<int, int>> act_c = {{n+1, n}}; list<pair<int, int>> act_d = {{0, n}}; for (int i = n-1; i >= 0; --i) { int c = data[i].first; int d = data[i].second; while (!act_c.empty() && act_c.front().first <= c) act_c.pop_front(); act_c.push_front({c, i}); while (!act_d.empty() && act_d.front().first >= d) act_d.pop_front(); act_d.push_front({d, i}); Result cur = res0; auto qd = act_d.begin(); int end = n; while (qd->first > 0) { int nd = qd->first; int p = qd->second; ++qd; int q = qd->second; if (p-i+1 > nd) { end = p+1; break; } if (q-i > nd) { end = i+nd; break; } } auto qc = act_c.begin(); while (qc->first <= n) { int nc = qc->first; int p = qc->second; if (p-i+1 < nc) { p = i+nc-1; } ++qc; while (qc->second - i >= qc->first) { ++qc; act_c.erase(prev(qc)); } int q = qc->second; q = min(q, end); auto r = res.query(p+1, q+1); cur = ms(cur, r); } res.set(i, cur+1); } return res.get(0); } } int main() { iostream::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<pair<int, int>> data(n); for (int i = 0; i < n; ++i) { cin >> data[i].first >> data[i].second; } auto res = solve(move(data)); if (res.first > 0) { cout << res.first << ' ' << res.second << '\n'; } else { cout << "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 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 | #include <algorithm> #include <iostream> #include <list> #include <vector> using namespace std; namespace { template <typename A, typename B> constexpr B mod(A a, B b) { return a >= 0 ? a % b : (a % b + b) % b; } class Modulo { constexpr static int M = 1000000007; int x; public: constexpr Modulo(): x(0) {} template <typename T> constexpr Modulo(T const& x_): x(mod(x_, M)) {} constexpr int get() const { return x; } }; template <typename OS> OS& operator<<(OS& s, Modulo x) { return s << x.get(); } constexpr Modulo operator+(Modulo const& a, Modulo const& b) { return Modulo{a.get() + b.get()}; } constexpr Modulo operator-(Modulo const& a, Modulo const& b) { return Modulo{a.get() - b.get()}; } constexpr Modulo operator*(Modulo const& a, Modulo const& b) { return Modulo{1LL * a.get() * b.get()}; } Modulo& operator+=(Modulo& a, Modulo const& b) { return a = a + b; } Modulo& operator-=(Modulo& a, Modulo const& b) { return a = a - b; } Modulo& operator*=(Modulo& a, Modulo const& b) { return a = a * b; } Modulo& operator++(Modulo& a) { return a += 1; } Modulo& operator--(Modulo& a) { return a -= 1; } Modulo operator++(Modulo a, int) { return a += 1; } Modulo operator--(Modulo a, int) { return a -= 1; } constexpr bool operator<(Modulo const& a, Modulo const& b) { return a.get() < b.get(); } constexpr bool operator<=(Modulo const& a, Modulo const& b) { return a.get() <= b.get(); } constexpr bool operator>(Modulo const& a, Modulo const& b) { return a.get() > b.get(); } constexpr bool operator>=(Modulo const& a, Modulo const& b) { return a.get() >= b.get(); } constexpr bool operator==(Modulo const& a, Modulo const& b) { return a.get() == b.get(); } constexpr bool operator!=(Modulo const& a, Modulo const& b) { return a.get() != b.get(); } typedef pair<int, Modulo> Result; Result const res0 = {-1, 0}; Result ms(Result const& a, Result const& b) { if (a.first == b.first) return {a.first, a.second+b.second}; if (a.first > b.first) return a; return b; } Result operator+(Result const& a, int b) { if (a.first < 0) return a; return {a.first + b, a.second}; } int rp2(int n) { int r = 1; while (r < n) r *= 2; return r; } class ST { int n; int N; vector<Result> data; public: ST(int n_): n{rp2(n_)}, data(2*n, res0) { } Result get(int i) const { i += n; return data[i]; } void set(int i, Result x) { i += n; data[i] = x; i /= 2; while (i > 0) { data[i] = ms(data[2*i], data[2*i+1]); i /= 2; } } Result query(int a, int b) { if (a >= b) return res0; a += n; Result res = data[a]; b += n; while (a/2 != b/2) { if (a % 2 == 0) res = ms(res, data[a+1]); if (b % 2 == 1) res = ms(res, data[b-1]); a /= 2; b /= 2; } return res; } }; Result solve(vector<pair<int, int>> data) { int n = data.size(); ST res(n+1); res.set(n, {0, 1}); list<pair<int, int>> act_c = {{n+1, n}}; list<pair<int, int>> act_d = {{0, n}}; for (int i = n-1; i >= 0; --i) { int c = data[i].first; int d = data[i].second; while (!act_c.empty() && act_c.front().first <= c) act_c.pop_front(); act_c.push_front({c, i}); while (!act_d.empty() && act_d.front().first >= d) act_d.pop_front(); act_d.push_front({d, i}); Result cur = res0; auto qd = act_d.begin(); int end = n; while (qd->first > 0) { int nd = qd->first; int p = qd->second; ++qd; int q = qd->second; if (p-i+1 > nd) { end = p+1; break; } if (q-i > nd) { end = i+nd; break; } } auto qc = act_c.begin(); while (qc->first <= n) { int nc = qc->first; int p = qc->second; if (p-i+1 < nc) { p = i+nc-1; } ++qc; while (qc->second - i >= qc->first) { ++qc; act_c.erase(prev(qc)); } int q = qc->second; q = min(q, end); auto r = res.query(p+1, q+1); cur = ms(cur, r); } res.set(i, cur+1); } return res.get(0); } } int main() { iostream::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<pair<int, int>> data(n); for (int i = 0; i < n; ++i) { cin >> data[i].first >> data[i].second; } auto res = solve(move(data)); if (res.first > 0) { cout << res.first << ' ' << res.second << '\n'; } else { cout << "NIE\n"; } return 0; } |