#include <iostream> #include <vector> #include <set> #include <map> #include <algorithm> #include <cmath> #include <ctime> #include <cstdlib> #include <cstdio> #include <utility> #include <iomanip> #include <assert.h> #define MP make_pair #define PB push_back #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define PB push_back #define MP make_pair #ifdef LOCAL #define debug(x) {cerr <<#x <<" = " <<x <<"\n"; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(x) #define debugv(x) #endif #define make(type, x) type x; cin>>x; #define make2(type, x, y) type x, y; cin>>x>>y; #define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z; using namespace std; typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; void mini(int& a4, int b4){a4=min(a4, b4); } void maxi(int& a4, int b4){a4=max(a4, b4); } template<class T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} const ll P = 1e9 + 7; const int N = 1e6 + 5; const int M = (1 << 20); const int R = N; int min_len[N], max_len[N]; int blocked_pref[R]; // "Jezyki i narzedzia programowania I", to jednak pomagaja czlowiekowi i w algo :D template<class C> void AddInterval(vector<C>& drz, int a, int b, C v, void (*fn)(C&, C)) { if (a > b) { return; } a += M; b += M; fn(drz[a], v); fn(drz[b], v); while (a / 2 != b / 2) { if (a % 2 == 0) { fn(drz[a + 1], v); } if (b % 2 == 1) { fn(drz[b - 1], v); } a /= 2; b /= 2; } } template<class C> C ReadPoint(vector<C>& drz, int a, void (*fn)(C&, C), C res) { a += M; while (a) { fn(res, drz[a]); a /= 2; } return res; } template<class C> void AddPoint(vector<C>& drz, int a, C v, void (*fn)(C&, C)) { a += M; while (a) { fn(drz[a], v); a /= 2; } } template<class C> C ReadInterval(vector<C>& drz, int a, int b, void (*fn)(C&, C), C res) { a += M; b += M; fn(res, drz[a]); if (a != b) { fn(res, drz[b]); } while (a / 2 != b / 2) { if (a % 2 == 0) { fn(res, drz[a + 1]); } if (b % 2 == 1) { fn(res, drz[b - 1]); } a /= 2; b /= 2; } return res; } vector<int> max_diff_lr, min_l; vector<int> leftmost; int max_team[N]; int ways_num[N]; void JoinPairs(pair<int, int>& p1, pair<int, int> p2) { if (p1.first > p2.first) { } else if (p1.first == p2.first) { p1.second = (p1.second + p2.second) % P; } else { p1 = p2; } } VPII max_team_and_way_drz; void Update(int v, int l, int r) { maxi(v, ReadPoint(leftmost, v, maxi, 0)); if (l > r) { return; } // cerr<<"Update dla (v, l, r) = ("<<v<<", "<<l<<", "<<r<<")"<<endl; int max_team_from_left; ll new_ways; pair<int, int> p = ReadInterval(max_team_and_way_drz, l - 1, r - 1, JoinPairs, MP(-1, 0)); max_team_from_left = p.first; new_ways = p.second; // cerr<<"Wyszło z niego (team, ways): "<<max_team_from_left<<" "<<new_ways<<endl; if (max_team_from_left == -1) { return; } if (max_team_from_left + 1 < max_team[v]) { return; } if (max_team_from_left + 1 > max_team[v]) { ways_num[v] = 0; } max_team[v] = max_team_from_left + 1; ways_num[v] = (ways_num[v] + new_ways) % P; } struct Tree { int left_end, right_end, left_son, right_son, v; Tree() : left_end(-1), right_end(-1), left_son(-1), right_son(-1) {} void Dfs(); }; Tree vers[N]; void Tree::Dfs() { // cerr<<v<<" ma synow "<<left_son<<" i "<<right_son<<endl; if (left_son != -1) { vers[left_son].left_end = left_end; vers[left_son].right_end = v - 1; vers[left_son].Dfs(); } // cerr<<"W wierzcholku "<<v<<" aktualizuje pkty od "<<max(v, left_end + min_len[v] - 1)<< // " do "<<min(right_end, max(v, v + min_len[v] - 2))<<endl; for (int h = max(v, left_end + min_len[v] - 2); h <= min(right_end, max(v, v + min_len[v] - 2)); h++) { Update(h, ReadPoint(leftmost, h, maxi, 1), h - min_len[v] + 1); } // cerr<<"Adduje (v, team, ways) "<<v<<" "<<max_team[v]<<" "<<ways_num[v]<<endl; AddPoint(max_team_and_way_drz, v, MP(max_team[v], ways_num[v]), JoinPairs); AddInterval(leftmost, v, min(right_end, v + min_len[v] - 2), v + 1, maxi); if (right_son != -1) { vers[right_son].left_end = v + 1; vers[right_son].right_end = right_end; vers[right_son].Dfs(); } } /* VPII ways_and_ind[N]; // indexed by team_num ll HowManyWaysPref(int team_num, int l) { VPII& vec = ways_and_ind[team_num]; int kl = 0, kp = (int)(vec.size() - 1, to_ret = 0; while (kl <= kp) { int aktc = (kl + kp) / 2; if (vec[aktc].first <= l) { to_ret = vec[aktc].second; kl = aktc + 1; } else { kp = aktc - 1; } } return to_ret; } ll HowManyWaysInterval(int team_num, int l, int r) { return (P + HowManyWaysPref(team_num, r) - HowManyWaysPref(team_num, l - 1)) % P; } */ bool cmp_min_len(int a, int b) { return min_len[a] > min_len[b]; } int main() { // nie zapomnij o ll ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); make(int, n); max_diff_lr.resize(2 * M + 5); min_l.resize(2 * M + 5); max_team_and_way_drz.resize(2 * M + 5); RE (i, 2 * M) { max_diff_lr[i] = M; } leftmost.resize(2 * M + 5); RE (i, 2 * M) { max_team_and_way_drz[i] = MP(-1, 0); } max_team[0] = 0; ways_num[0] = 1; AddPoint(max_team_and_way_drz, 0, MP(0, 1), JoinPairs); RE (i, n) { cin>>min_len[i]>>max_len[i]; max_team[i] = -1; AddInterval(max_diff_lr, i, min(n, i + max_len[i] - 1), max_len[i] - 1, mini); AddInterval(min_l, i + max_len[i], n, i + 1, maxi); } RE (i, n) { //debug(i); //debug(ReadPoint(min_l, i, maxi, 0)); //debug(ReadPoint(max_diff_lr, i, mini, M)); AddInterval(leftmost, i, i, max(1, max(ReadPoint(min_l, i, maxi, 0), i - ReadPoint(max_diff_lr, i, mini, M))), maxi); //debug(i) //debug(ReadPoint(leftmost, i, maxi, 0)); } VI min_len_sorted; RE (i, n) { min_len_sorted.PB(i); } stable_sort(ALL(min_len_sorted), cmp_min_len); set<int> used_ind; //debugv(min_len_sorted); for (int i : min_len_sorted) { auto it = used_ind.lower_bound(i); int right_val = N; int left_val = N; if (it != used_ind.end()) { right_val = min_len[*it]; } int left_nei = 0; if (it != used_ind.begin()) { auto prev_it = it; prev_it--; left_nei = *prev_it; left_val = min_len[left_nei]; } vers[i].v = i; used_ind.insert(i); if (min(left_val, right_val) >= N) { continue; } if (left_val < right_val) { vers[left_nei].right_son = i; // cerr<<i<<" linkuje pod "<<left_nei<<endl; } else { vers[*it].left_son = i; // cerr<<i<<" linkuje pod "<<*it<<endl; } } vers[min_len_sorted[0]].left_end = 1; vers[min_len_sorted[0]].right_end = n; vers[min_len_sorted[0]].Dfs(); /* FOR (i, 0, n) { cerr<<i<<" "<<max_team[i]<<" "<<ways_num[i]<<endl; } */ if (max_team[n] <= 0) { cout<<"NIE"<<endl; } else { cout<<max_team[n]<<" "<<ways_num[n]<<endl; } 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 304 305 306 | #include <iostream> #include <vector> #include <set> #include <map> #include <algorithm> #include <cmath> #include <ctime> #include <cstdlib> #include <cstdio> #include <utility> #include <iomanip> #include <assert.h> #define MP make_pair #define PB push_back #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define PB push_back #define MP make_pair #ifdef LOCAL #define debug(x) {cerr <<#x <<" = " <<x <<"\n"; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(x) #define debugv(x) #endif #define make(type, x) type x; cin>>x; #define make2(type, x, y) type x, y; cin>>x>>y; #define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z; using namespace std; typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; void mini(int& a4, int b4){a4=min(a4, b4); } void maxi(int& a4, int b4){a4=max(a4, b4); } template<class T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} const ll P = 1e9 + 7; const int N = 1e6 + 5; const int M = (1 << 20); const int R = N; int min_len[N], max_len[N]; int blocked_pref[R]; // "Jezyki i narzedzia programowania I", to jednak pomagaja czlowiekowi i w algo :D template<class C> void AddInterval(vector<C>& drz, int a, int b, C v, void (*fn)(C&, C)) { if (a > b) { return; } a += M; b += M; fn(drz[a], v); fn(drz[b], v); while (a / 2 != b / 2) { if (a % 2 == 0) { fn(drz[a + 1], v); } if (b % 2 == 1) { fn(drz[b - 1], v); } a /= 2; b /= 2; } } template<class C> C ReadPoint(vector<C>& drz, int a, void (*fn)(C&, C), C res) { a += M; while (a) { fn(res, drz[a]); a /= 2; } return res; } template<class C> void AddPoint(vector<C>& drz, int a, C v, void (*fn)(C&, C)) { a += M; while (a) { fn(drz[a], v); a /= 2; } } template<class C> C ReadInterval(vector<C>& drz, int a, int b, void (*fn)(C&, C), C res) { a += M; b += M; fn(res, drz[a]); if (a != b) { fn(res, drz[b]); } while (a / 2 != b / 2) { if (a % 2 == 0) { fn(res, drz[a + 1]); } if (b % 2 == 1) { fn(res, drz[b - 1]); } a /= 2; b /= 2; } return res; } vector<int> max_diff_lr, min_l; vector<int> leftmost; int max_team[N]; int ways_num[N]; void JoinPairs(pair<int, int>& p1, pair<int, int> p2) { if (p1.first > p2.first) { } else if (p1.first == p2.first) { p1.second = (p1.second + p2.second) % P; } else { p1 = p2; } } VPII max_team_and_way_drz; void Update(int v, int l, int r) { maxi(v, ReadPoint(leftmost, v, maxi, 0)); if (l > r) { return; } // cerr<<"Update dla (v, l, r) = ("<<v<<", "<<l<<", "<<r<<")"<<endl; int max_team_from_left; ll new_ways; pair<int, int> p = ReadInterval(max_team_and_way_drz, l - 1, r - 1, JoinPairs, MP(-1, 0)); max_team_from_left = p.first; new_ways = p.second; // cerr<<"Wyszło z niego (team, ways): "<<max_team_from_left<<" "<<new_ways<<endl; if (max_team_from_left == -1) { return; } if (max_team_from_left + 1 < max_team[v]) { return; } if (max_team_from_left + 1 > max_team[v]) { ways_num[v] = 0; } max_team[v] = max_team_from_left + 1; ways_num[v] = (ways_num[v] + new_ways) % P; } struct Tree { int left_end, right_end, left_son, right_son, v; Tree() : left_end(-1), right_end(-1), left_son(-1), right_son(-1) {} void Dfs(); }; Tree vers[N]; void Tree::Dfs() { // cerr<<v<<" ma synow "<<left_son<<" i "<<right_son<<endl; if (left_son != -1) { vers[left_son].left_end = left_end; vers[left_son].right_end = v - 1; vers[left_son].Dfs(); } // cerr<<"W wierzcholku "<<v<<" aktualizuje pkty od "<<max(v, left_end + min_len[v] - 1)<< // " do "<<min(right_end, max(v, v + min_len[v] - 2))<<endl; for (int h = max(v, left_end + min_len[v] - 2); h <= min(right_end, max(v, v + min_len[v] - 2)); h++) { Update(h, ReadPoint(leftmost, h, maxi, 1), h - min_len[v] + 1); } // cerr<<"Adduje (v, team, ways) "<<v<<" "<<max_team[v]<<" "<<ways_num[v]<<endl; AddPoint(max_team_and_way_drz, v, MP(max_team[v], ways_num[v]), JoinPairs); AddInterval(leftmost, v, min(right_end, v + min_len[v] - 2), v + 1, maxi); if (right_son != -1) { vers[right_son].left_end = v + 1; vers[right_son].right_end = right_end; vers[right_son].Dfs(); } } /* VPII ways_and_ind[N]; // indexed by team_num ll HowManyWaysPref(int team_num, int l) { VPII& vec = ways_and_ind[team_num]; int kl = 0, kp = (int)(vec.size() - 1, to_ret = 0; while (kl <= kp) { int aktc = (kl + kp) / 2; if (vec[aktc].first <= l) { to_ret = vec[aktc].second; kl = aktc + 1; } else { kp = aktc - 1; } } return to_ret; } ll HowManyWaysInterval(int team_num, int l, int r) { return (P + HowManyWaysPref(team_num, r) - HowManyWaysPref(team_num, l - 1)) % P; } */ bool cmp_min_len(int a, int b) { return min_len[a] > min_len[b]; } int main() { // nie zapomnij o ll ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); make(int, n); max_diff_lr.resize(2 * M + 5); min_l.resize(2 * M + 5); max_team_and_way_drz.resize(2 * M + 5); RE (i, 2 * M) { max_diff_lr[i] = M; } leftmost.resize(2 * M + 5); RE (i, 2 * M) { max_team_and_way_drz[i] = MP(-1, 0); } max_team[0] = 0; ways_num[0] = 1; AddPoint(max_team_and_way_drz, 0, MP(0, 1), JoinPairs); RE (i, n) { cin>>min_len[i]>>max_len[i]; max_team[i] = -1; AddInterval(max_diff_lr, i, min(n, i + max_len[i] - 1), max_len[i] - 1, mini); AddInterval(min_l, i + max_len[i], n, i + 1, maxi); } RE (i, n) { //debug(i); //debug(ReadPoint(min_l, i, maxi, 0)); //debug(ReadPoint(max_diff_lr, i, mini, M)); AddInterval(leftmost, i, i, max(1, max(ReadPoint(min_l, i, maxi, 0), i - ReadPoint(max_diff_lr, i, mini, M))), maxi); //debug(i) //debug(ReadPoint(leftmost, i, maxi, 0)); } VI min_len_sorted; RE (i, n) { min_len_sorted.PB(i); } stable_sort(ALL(min_len_sorted), cmp_min_len); set<int> used_ind; //debugv(min_len_sorted); for (int i : min_len_sorted) { auto it = used_ind.lower_bound(i); int right_val = N; int left_val = N; if (it != used_ind.end()) { right_val = min_len[*it]; } int left_nei = 0; if (it != used_ind.begin()) { auto prev_it = it; prev_it--; left_nei = *prev_it; left_val = min_len[left_nei]; } vers[i].v = i; used_ind.insert(i); if (min(left_val, right_val) >= N) { continue; } if (left_val < right_val) { vers[left_nei].right_son = i; // cerr<<i<<" linkuje pod "<<left_nei<<endl; } else { vers[*it].left_son = i; // cerr<<i<<" linkuje pod "<<*it<<endl; } } vers[min_len_sorted[0]].left_end = 1; vers[min_len_sorted[0]].right_end = n; vers[min_len_sorted[0]].Dfs(); /* FOR (i, 0, n) { cerr<<i<<" "<<max_team[i]<<" "<<ways_num[i]<<endl; } */ if (max_team[n] <= 0) { cout<<"NIE"<<endl; } else { cout<<max_team[n]<<" "<<ways_num[n]<<endl; } return 0; } |