//#define READ_NUM_TEST_CASES //#define ASSERTS // force asserts on online judge //#define OLD_COMPILER // (SOLUTION IS AT THE END OF THIS FILE) ////////////////////////// PROTOTYPING TEMPLATE ///////////////////////////////// // // Implementation: Adam Blaszkiewicz (atablash.pl), 2012-2014 /* This program is licensed under GNU General Public License version 3. See <http://www.gnu.org/licenses/>. */ // // // enable c++ iostream; only if no fastio //#define IOSTREAM //#define NO_UNLOCKED // fread_unlocked, fwrite_unlocked #define mp make_pair #define it iterator #define be begin() #define en end() #define sz size() #define fi first #define se second #define pub push_back #define puf push_front #define pob pop_back() #define pof pop_front() #define bs binary_search #define lb lower_bound #define ub upper_bound #define fo3(i,a,b) for(int i=(int)(a); i<(int)(b); ++i) #define of3(i,a,b) for(int i=(int)((b)-1); i>=(int)(a); --i) #define fo2(i,n) fo3(i,0,n) #define of2(i,n) of3(i,0,n) #define fo(...) VA_SEL(fo,__VA_ARGS__) #define of(...) VA_SEL(of,__VA_ARGS__) #define inl inline __attribute__ ((always_inline)) #define ninl __attribute__ ((noinline)) #ifdef OLD_COMPILER #include<vector> #include<map> #include<queue> #include<algorithm> #include<string> #include<stdint.h> #include<cstring> #include<iostream> #else #include<bits/stdc++.h> #endif #ifdef OLD_COMPILER #else #include<cstdint> #endif #include<utility> namespace ab{ typedef int8_t i8; typedef uint8_t ui8; typedef int16_t i16; typedef uint16_t ui16; typedef int32_t i32; typedef uint32_t ui32; typedef int64_t i64; typedef uint64_t ui64; // old: typedef i64 ll; typedef ui64 ull; typedef std::pair<i32,i32> pii; typedef unsigned int uint; typedef unsigned short ushort; typedef unsigned char uchar; struct VOID {}; template<typename I>struct FloatFrom{typedef VOID R;}; template<>struct FloatFrom<i32>{typedef double R;}; template<>struct FloatFrom<ui32>{typedef double R;}; template<>struct FloatFrom<i64>{typedef long double R;}; template<>struct FloatFrom<ui64>{typedef long double R;}; template<>struct FloatFrom<i16>{typedef float R;}; template<>struct FloatFrom<ui16>{typedef float R;}; template<>struct FloatFrom<i8>{typedef float R;}; template<>struct FloatFrom<ui8>{typedef float R;}; template<>struct FloatFrom<float>{typedef float R;}; template<>struct FloatFrom<double>{typedef double R;}; template<>struct FloatFrom<long double>{typedef long double R;}; template<typename I>struct Double{typedef VOID R;}; template<>struct Double<i8>{typedef i16 R;}; template<>struct Double<ui8>{typedef ui16 R;}; template<>struct Double<i16>{typedef i32 R;}; template<>struct Double<ui16>{typedef ui32 R;}; template<>struct Double<i32>{typedef i64 R;}; template<>struct Double<ui32>{typedef ui64 R;}; template<>struct Double<float>{typedef float R;}; template<>struct Double<double>{typedef double R;}; template<>struct Double<long double>{typedef long double R;}; #if defined __GNUC__ and ( __WORDSIZE == 64 ) typedef int128_t i128; typedef uint128_t ui128; template<>struct Double<i64>{typedef i128 R;}; template<>struct Double<ui64>{typedef ui128 R;}; #endif } #define FASTIO #ifdef NO_FASTIO #undef FASTIO #endif #ifdef DEBUG #undef NDEBUG #pragma message "Compiling in DEBUG mode." #undef FASTIO #ifdef _WIN32 #include<windows.h> inline void ec(int a){static HANDLE h=GetStdHandle(-12);SetConsoleTextAttribute(h,a?12:0xf);} inline void oc(int a){static HANDLE h=GetStdHandle(-11);SetConsoleTextAttribute(h,a?10:0xf);} #else // *nix includes for debug #endif #else #define NDEBUG #define ec(x) #define oc(x) #endif #include <cstdio> #include <cfloat> #ifdef OLD_COMPILER // TODO #else #include<type_traits> #endif #define _CAT(a,b) a##b #define CAT(a,b) _CAT(a,b) #define _10TH(a,b,c,d,e,f,g,h,i,CNT,...) CNT #define VA_CNT(...) _10TH(__VA_ARGS__,9,8,7,6,5,4,3,2,1) #define VA_SEL(a,...) CAT(a,VA_CNT(__VA_ARGS__))(__VA_ARGS__) #define _VA_GTONE(...) _10TH(__VA_ARGS__,1,1,1,1,1,1,1,1,0) #define VA_GTONE(a,...) CAT(a,_VA_GTONE(__VA_ARGS__))(__VA_ARGS__) #define foreach(e,c) for(auto&e:c) #define MIN_GCC(a,b,c) (__GNUC__ > (a) || \ (__GNUC__ == (a) && (__GNUC_MINOR__ > (b) || \ (__GNUC_MINOR__ == (b) && \ __GNUC_PATCHLEVEL__ >= (c))))) #define L0(str) fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__) #define L1(str,...) fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__,__VA_ARGS__) #define L(...) VA_GTONE(L,__VA_ARGS__) #ifdef NDEBUG #define D if(0) #define A(x) #define TEST_FLOAT(x) #define ASSERT_FLOAT(x) #else #define A(x) assert(x); #define _TEST_FLOAT(x) ((x) <= LDBL_MAX && (x) >= -LDBL_MAX) #define ASSERT_FLOAT(f) A(_TEST_FLOAT(f)) #define D #endif #define V(x) (E<<#x<<": "<<(x)<<N) #define XX {E<<"XX LINE "<<__LINE__<<N;} #if defined DEBUG or defined ASSERTS #undef NDEBUG #else #define NDEBUG #endif #ifdef _WIN32 // TODO #ifndef NO_UNLOCKED #define NO_UNLOCKED #endif //#define fread_unlocked _fread_nolock //#define fwrite_unlocked _fwrite_nolock #endif #define all(x) (x).be,(x).en #define type(x) __typeof(x) const char N='\n'; const char S=' '; using namespace std; using namespace __gnu_cxx; namespace ab{} using namespace ab; template<class T>inline T cbitl(T x,int i){return (x<<i) ^ (x>>(sizeof(T)*8-i));} namespace std{ #if defined __GNUC__ and not MIN_GCC(4,2,0) template<class T>struct hash<T>{} #endif // 1,2,last (!!!!!) template<>template<class A,class B>struct hash<map<A,B>>{ size_t operator()(const map<A,B>& m) const{ size_t r = m.size(); if(!r)return r; auto i=m.be; r^=hash<type(i)>()(i); ++i; r^=cbitl(hash<type(i)>()(*i),sizeof(r)*8/4); i=m.end(); --i; r^=cbitl(hash<type(i)>()(*i),sizeof(r)*8/2); return r; } }; template<>template<class A, class B>struct hash<pair<A,B>>{ size_t operator()(const pair<A,B>& p) const{ return hash<A>()(p.first) ^ cbitl(hash<B>()(p.second),sizeof(size_t)*8/2); } }; } #define rint _rint() #define ruint _ruint() #define rll _rll() #define rull _rull() #define rchar _rchar() #ifndef FASTIO inline void fin(int){}inline void fout(int){} #ifdef IOSTREAM inline int _rint(){int r;cin>>r;return r;} inline uint _ruint(){uint r;cin>>r;return r;} inline ull _rull(){ull r;cin>>r;return r;} inline char _rchar(){return cin.get();} template<typename T>inline void W(const T& i){cout<<i;} inline void W(){W('\n');} #else inline int _rint(){int r;scanf("%d",&r);return r;} inline uint _ruint(){uint r;scanf("%u",&r);return r;} inline ull _rull(){ull r;scanf("%llu",&r);return r;} inline ull _rll(){ll r;scanf("%lld",&r);return r;} inline char _rchar(){return getchar();} inline void W(char a='\n'){printf("%c",a);} inline void W(int a){printf("%d",a);} inline void W(uint a){printf("%u",a);} inline void W(const ull& a){printf("%llu",a);} inline void W(const ll& a){printf("%lld",a);} inline void W(const char *str){fputs(str,stdout);} inline void W(const double& d){printf("%.20f",d);} inline void W(const long double& d){printf("%.50Lf",d);} #endif #else #ifndef NO_UNLOCKED #define FREAD fread_unlocked #define FWRITE fwrite_unlocked #else #define FREAD fread #define FWRITE fwrite #endif // todo: test on pointers const int _IB=32768;const int _OB=32768; char _iB[_IB];int _iP = _IB;char _oB[_OB];int _oP = 0; inline void fin(int mc){int r=_IB-_iP;if(r>mc)return;fo(i,r)_iB[i]=_iB[i+_iP]; // TODO: memcpy FREAD(_iB+r,1,_iP,stdin);_iP = 0;} inline void fout(int mc){if(_OB-_oP>mc)return;FWRITE(_oB,1,_oP,stdout);_oP = 0;} inline void W(char c='\n'){_oB[_oP++]=c;} inline char _rchar(){return _iB[_iP++];} template<class T>inline T _rur(){ T r=0;char c=rchar; do c-='0',r=r*10+c,c=rchar;while(c>='0'); return r;} template<class T>inline T _rsi(){ char c=rchar; while(c<'-')c=rchar; if(c=='-')return -_rur<T>(); else{--_iP;return _rur<T>();}} template<class T>inline T _ru(){while(rchar<'0');--_iP;return _rur<T>();} inline int _rint(){fin(15);return _rsi<int>();} inline uint _ruint(){fin(15);return _ru<uint>();} inline ll _rll(){fin(25);return _rsi<ll>();} inline ull _rull(){fin(25);return _ru<ull>();} const int _rs = 20;char _r[_rs]; template<class T>inline void _w(T i){ if(numeric_limits<T>::is_signed && i<0)W('-'),i=-i; else if(i==0){W('0');return;} int pos = _rs; do{_r[--pos]='0'+i%10;i/=10;}while(i); do W(_r[pos]),++pos;while(pos<_rs);} inline void W(const int32_t& i){fout(15);_w(i);} inline void W(const uint32_t& i){fout(15);_w(i);} inline void W(const int64_t& i){fout(25);_w(i);} inline void W(const uint64_t& i){fout(25);_w(i);} inline void W(const char*str){int l=strlen(str);fout(l);memcpy(&_oB[_oP],str,l);_oP+=l;} inline void W(double d){fout(50);_oP+=sprintf(&_oB[_oP],"%.20f",d);} // todo #endif template<class A,class B>inline void W(const A&a,const B&b){W(a);W(b);} template<class A,class B,class C> inline void W(const A&a,const B&b,const C&c){W(a);W(b);W(c);} template<class A,class B,class C,class DD> inline void W(const A&a,const B&b,const C&c,const DD&dd){W(a);W(b);W(c);W(dd);} template<class T>inline void sortall(T&t){sort(t.be,t.en);} template<class T>inline void uniqueall(T&t){t.resize(unique(t.be,t.en));} inline int count1(uint i){return __builtin_popcount(i);} inline int count1(ull i){return __builtin_popcountll(i);} inline int parity(uint i){return __builtin_parity(i);} inline int parity(ull i){return __builtin_parityll(i);} inline int ilog(uint i){return 31-__builtin_clz(i);} // int log; floor(log2(i)) inline int ilog(ull i){return 63-__builtin_clzll(i);} #define powerof2(x) !(x&(x-1)) #define subset(a,b) (a&b==a) #define kbegin(k) ((1<<k)-1) #define kend(k,n) ((kbegin(k)<<n-k)+1) inline uint knext(uint a){uint b=(a|(a-1))+1;return b|((a^b)>>(__builtin_ctz(a)+2));} #define fors(x,k,n) for(uint x=kbegin(k);x<kend(k,n);x=knext(x)) template<class A,class B>inline ostream&operator<<(ostream&o,const pair<A,B>&p){o<<'<'<<p.fi<<", "<<p.se<<'>';return o;} template<class A,class B>inline ostream&operator<<(ostream&o,const map<A,B>&m){o<<N;for(auto&k:m)o<<"map["<<k.fi<<"] -> "<<k.se<<N;return o;} template<class A>inline ostream&operator<<(ostream&o,const vector<A>&v){o<<N;fo(i,v.sz)o<<"vector["<<i<<"] = "<<v[i]<<N;return o;} struct _O{template<class T>_O&operator<<(const T&t){oc(1);W(t);oc(0);return*this;}}O; struct _I{ _I&operator>>(int32_t&t){t=rint;return*this;} _I&operator>>(uint32_t&t){t=ruint;return*this;} _I&operator>>(int64_t&t){t=rll;return*this;} _I&operator>>(uint64_t&t){t=rull;return*this;} }I; struct _E{template<class T>_E&operator<<(const T&t){D{ec(1);cerr<<t;ec(0);}return*this;}}E; inline void tc(); int main() { #ifdef FASTIO setvbuf(stdin,0,_IONBF,0);setvbuf(stdout,0,_IONBF,0); #elif defined IOSTREAM ios_base::sync_with_stdio(0);cin.tie(0);cout.precision(99);cout.setf(ios::fixed,ios::floatfield); #endif #ifdef DEBUG #ifdef _WIN32 ec(0); oc(0); #else // TODO: *nix #endif #endif #ifdef READ_NUM_TEST_CASES int nz=rint;fo(i,nz) #endif tc(); #ifdef FASTIO FWRITE(_oB,1,_oP,stdout); #endif return 0; } // END OF TEMPLATE int mod = 1000000007; inline int middle(int a, int b) { int s = a+b; if(s>=0) return s/2; else return (s-1)/2; } struct newnode { pii mval; int minval; } nodes[(1<<21)+9]; inline int getmin(int node, int a, int b, int fr, int to){ if(b<=fr || to<=a) return INT_MAX; if(fr<=a && b<=to) return nodes[node].minval; //V(fr);V(to); int s = middle(a,b); auto ll = getmin(node*2,a,s,fr,to); auto rr = getmin(node*2+1,s,b,fr,to); return min(ll,rr); } inline int getmin(int fr, int to){ return getmin(1,-1,(1<<20)-1,fr,to); } inline void ssetmin(int node, int a, int b, int val, int where){ if(a+1==b){ A(where==a); nodes[node].minval = val; return; } int s = middle(a,b); if(where >= s) ssetmin(node*2+1,s,b,val,where); else ssetmin(node*2,a,s,val,where); nodes[node].minval = min(nodes[node*2].minval, nodes[node*2+1].minval); } inline void ssetmin(int val, int where){ ssetmin(1,-1,(1<<20)-1,val,where); } inline void init(){ fo(i,(1<<21)+7)nodes[i].mval = pii(-INT_MAX,0), nodes[i].minval=INT_MAX; } // max,nummax inline pii get(int node, int a, int b, int fr, int to){ if(b<=fr || to<=a) return pii(-INT_MAX,0); if(fr<=a && b<=to) return nodes[node].mval; //V(fr);V(to); int s = middle(a,b); auto ll = get(node*2,a,s,fr,to); auto rr = get(node*2+1,s,b,fr,to); if(ll.fi>rr.fi) return ll; else if(ll.fi<rr.fi) return rr; else return mp(ll.fi,(ll.se+rr.se)%mod); } inline pii get(int fr, int to){ return get(1,-1,(1<<20)-1,fr,to); } inline void sset(int node, int a, int b, pii val, int where){ if(a+1==b){ if(where != a)V(where),V(a),V(b); A(where==a); nodes[node].mval = val; return; } int s = middle(a,b); if(where >= s) sset(node*2+1,s,b,val,where); else sset(node*2,a,s,val,where); if(nodes[node*2].mval.fi > nodes[node*2+1].mval.fi) nodes[node].mval = nodes[node*2].mval; else if(nodes[node*2].mval.fi < nodes[node*2+1].mval.fi) nodes[node].mval = nodes[node*2+1].mval; else nodes[node].mval = pii(nodes[node*2].mval.fi,(nodes[node*2].mval.se + nodes[node*2+1].mval.se)%mod); } inline void sset(pii val, int where){ sset(1,-1,(1<<20)-1,val,where); } int n; //int lo[1000009]; //int hi[1000009]; #ifdef DEBUG char test[1000009]; void testenable(int i){ E<<"enabling "<<i<<N; A(test[i+1] == 0); test[i+1]=1; } void testdisable(int i){ E<<"disabling "<<i<<N; A(test[i+1] == 1); test[i+1] = 0; } #else #define testenable(a) #define testdisable(a) #endif int tme = 0; pii _results[1000009]; pii *results = _results+1; //int id = 0; vector<int> active; bitset<1000009> activebit; pii allint[1000009]; int prevs[1000009]; int nexts[1000009]; int allintsz=0; int allintback=-1; priority_queue<pii,vector<pii>,greater<pii>> triggers; int pushint(pii inte){ if(allintback!=-1)nexts[allintback]=allintsz; prevs[allintsz] = allintback; nexts[allintsz] = -1; allintback = allintsz; ++allintsz; allint[allintback] = inte; E<<"pushint "<<inte.fi<<" "<<inte.se<<" -> "<<allintback<<N; return allintback; } void delint(int i){ if(i == allintback)allintback = prevs[i]; if(prevs[i]!=-1) nexts[prevs[i]] = nexts[i]; if(nexts[i]!=-1) prevs[nexts[i]] = prevs[i]; E<<"delint "<<i<<" ("<<allint[i].fi<<" "<<allint[i].se<<")"<<N; allint[i].fi = INT_MAX; activebit[i] = 0; } inline void disablerange(int fr, int to){ E<<"disable range "<<fr<<" "<<to<<N; fo(i,fr,to){ testdisable(i); sset(pii(-INT_MAX,0),i); } } inline void enablerange(int fr, int to){ E<<"enable range "<<fr<<" "<<to<<N; fo(i,fr,to){ testenable(i); sset(results[i],i); } } void addinterval(int fr, int to) { E<<"addinterval "<<fr<<" "<<to<<N; if(fr+tme >= to){ E<<"verdict: empty"<<N; return; } int delto = to; for(;;){ int zalastdel = 0; if(allintback!=-1)zalastdel = max(zalastdel,allint[allintback].se); if(allintback==-1 || zalastdel <= fr+tme){ if(fr+tme < -1){ E<<"verdict: queue begin"<<N; disablerange(-1,delto); int ontime = -1-fr; A(ontime>tme); A(fr+ontime < to); int nid = pushint(pii(fr,to)); triggers.push(pii(ontime,nid)); return; } E<<"verdict: active"<<N; disablerange(fr+tme,delto); int nid = pushint(pii(fr,to)); // activate active.pub(nid); activebit[nid]=1; return; } A(allintback!=-1); if(allint[allintback].fi < fr){ E<<"verdict: queue"<<N; disablerange(allint[allintback].se,delto); int ontime = allint[allintback].se - (fr+tme) + tme; A(ontime > tme); A(fr+ontime < to); int nid = pushint(pii(fr,to)); triggers.push(pii(ontime,nid)); return; } // dominating bool found = activebit[allintback]; disablerange(allint[allintback].se,delto); delto = allint[allintback].fi+tme; if(found){ // active! E<<"verdict: dominating active"<<N; // deactivate activebit[allintback] = 0; } else{ // inactive E<<"verdict: dominating inactive "<<allintback<<N; } delint(allintback); } } void advancetime() { E<<"SCHEDULED ENABLING:"<<N; fo(i,active.sz){ if(activebit[active[i]]==0){ active[i--]=active.back(); active.pob; continue; } int fr = allint[active[i]].fi; int to = allint[active[i]].se; E<<"enable id "<<active[i]<<" "<<fr<<" "<<to<<N; testenable(fr+tme); sset(results[fr+tme],fr+tme); if(fr+tme + 1 == to){ delint(active[i]); active[i--] = active.back(); active.pob; } } ++tme; while(!triggers.empty() && triggers.top().fi == tme){ int wid = triggers.top().se; triggers.pop(); if(allint[wid].fi==INT_MAX)continue; // not existing anymore // activate A(allint[wid].fi+tme < allint[wid].se); activebit[wid] = 1; active.pub(wid); } } void cleanup(int to){ // todo } inline void tc() { fo(i,1000003)allint[i].fi = INT_MAX; init(); I>>n; //fo(i,n)I>>lo[i]>>hi[i]; //fo(i,n)root.ssetmin(hi[i],i); int maxsq = 0; pii gresult; results[-1] = pii(0,1); sset(pii(0,1),-1); testenable(-1); int peak = 0; tme = 0; fo(i,n){ //if(i%1000==999){ //O<<"time "<<tme<<" peak "<<peak<<N;////////// //} int clo,chi; I>>clo>>chi; A(i==tme); ssetmin(chi,i); E<<N<<"time "<<i<<N; if(chi<=maxsq) maxsq = chi; else{ ++maxsq; int bound = getmin(tme+1-maxsq,tme+1); E<<"min ptree get "<<tme+1-maxsq<<" "<<tme+1<<" "<<bound<<N; A(maxsq-1 <= bound); maxsq = min(maxsq,bound); } addinterval(-(clo-1),tme); pii result = get(tme-maxsq,tme); cleanup(tme-maxsq-5); // optional result.fi += 1; E<<"result "<<result<<N; sset(result,tme); testenable(tme); if(i==n-1)gresult = result; results[tme]=result; peak = max(peak,(int)allintsz); advancetime(); } if(gresult.fi<-10) O<<"NIE"<<N; else O<<gresult.fi<<" "<<gresult.se<<N; //O<<peak<<N; //for(;;); }
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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 | //#define READ_NUM_TEST_CASES //#define ASSERTS // force asserts on online judge //#define OLD_COMPILER // (SOLUTION IS AT THE END OF THIS FILE) ////////////////////////// PROTOTYPING TEMPLATE ///////////////////////////////// // // Implementation: Adam Blaszkiewicz (atablash.pl), 2012-2014 /* This program is licensed under GNU General Public License version 3. See <http://www.gnu.org/licenses/>. */ // // // enable c++ iostream; only if no fastio //#define IOSTREAM //#define NO_UNLOCKED // fread_unlocked, fwrite_unlocked #define mp make_pair #define it iterator #define be begin() #define en end() #define sz size() #define fi first #define se second #define pub push_back #define puf push_front #define pob pop_back() #define pof pop_front() #define bs binary_search #define lb lower_bound #define ub upper_bound #define fo3(i,a,b) for(int i=(int)(a); i<(int)(b); ++i) #define of3(i,a,b) for(int i=(int)((b)-1); i>=(int)(a); --i) #define fo2(i,n) fo3(i,0,n) #define of2(i,n) of3(i,0,n) #define fo(...) VA_SEL(fo,__VA_ARGS__) #define of(...) VA_SEL(of,__VA_ARGS__) #define inl inline __attribute__ ((always_inline)) #define ninl __attribute__ ((noinline)) #ifdef OLD_COMPILER #include<vector> #include<map> #include<queue> #include<algorithm> #include<string> #include<stdint.h> #include<cstring> #include<iostream> #else #include<bits/stdc++.h> #endif #ifdef OLD_COMPILER #else #include<cstdint> #endif #include<utility> namespace ab{ typedef int8_t i8; typedef uint8_t ui8; typedef int16_t i16; typedef uint16_t ui16; typedef int32_t i32; typedef uint32_t ui32; typedef int64_t i64; typedef uint64_t ui64; // old: typedef i64 ll; typedef ui64 ull; typedef std::pair<i32,i32> pii; typedef unsigned int uint; typedef unsigned short ushort; typedef unsigned char uchar; struct VOID {}; template<typename I>struct FloatFrom{typedef VOID R;}; template<>struct FloatFrom<i32>{typedef double R;}; template<>struct FloatFrom<ui32>{typedef double R;}; template<>struct FloatFrom<i64>{typedef long double R;}; template<>struct FloatFrom<ui64>{typedef long double R;}; template<>struct FloatFrom<i16>{typedef float R;}; template<>struct FloatFrom<ui16>{typedef float R;}; template<>struct FloatFrom<i8>{typedef float R;}; template<>struct FloatFrom<ui8>{typedef float R;}; template<>struct FloatFrom<float>{typedef float R;}; template<>struct FloatFrom<double>{typedef double R;}; template<>struct FloatFrom<long double>{typedef long double R;}; template<typename I>struct Double{typedef VOID R;}; template<>struct Double<i8>{typedef i16 R;}; template<>struct Double<ui8>{typedef ui16 R;}; template<>struct Double<i16>{typedef i32 R;}; template<>struct Double<ui16>{typedef ui32 R;}; template<>struct Double<i32>{typedef i64 R;}; template<>struct Double<ui32>{typedef ui64 R;}; template<>struct Double<float>{typedef float R;}; template<>struct Double<double>{typedef double R;}; template<>struct Double<long double>{typedef long double R;}; #if defined __GNUC__ and ( __WORDSIZE == 64 ) typedef int128_t i128; typedef uint128_t ui128; template<>struct Double<i64>{typedef i128 R;}; template<>struct Double<ui64>{typedef ui128 R;}; #endif } #define FASTIO #ifdef NO_FASTIO #undef FASTIO #endif #ifdef DEBUG #undef NDEBUG #pragma message "Compiling in DEBUG mode." #undef FASTIO #ifdef _WIN32 #include<windows.h> inline void ec(int a){static HANDLE h=GetStdHandle(-12);SetConsoleTextAttribute(h,a?12:0xf);} inline void oc(int a){static HANDLE h=GetStdHandle(-11);SetConsoleTextAttribute(h,a?10:0xf);} #else // *nix includes for debug #endif #else #define NDEBUG #define ec(x) #define oc(x) #endif #include <cstdio> #include <cfloat> #ifdef OLD_COMPILER // TODO #else #include<type_traits> #endif #define _CAT(a,b) a##b #define CAT(a,b) _CAT(a,b) #define _10TH(a,b,c,d,e,f,g,h,i,CNT,...) CNT #define VA_CNT(...) _10TH(__VA_ARGS__,9,8,7,6,5,4,3,2,1) #define VA_SEL(a,...) CAT(a,VA_CNT(__VA_ARGS__))(__VA_ARGS__) #define _VA_GTONE(...) _10TH(__VA_ARGS__,1,1,1,1,1,1,1,1,0) #define VA_GTONE(a,...) CAT(a,_VA_GTONE(__VA_ARGS__))(__VA_ARGS__) #define foreach(e,c) for(auto&e:c) #define MIN_GCC(a,b,c) (__GNUC__ > (a) || \ (__GNUC__ == (a) && (__GNUC_MINOR__ > (b) || \ (__GNUC_MINOR__ == (b) && \ __GNUC_PATCHLEVEL__ >= (c))))) #define L0(str) fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__) #define L1(str,...) fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__,__VA_ARGS__) #define L(...) VA_GTONE(L,__VA_ARGS__) #ifdef NDEBUG #define D if(0) #define A(x) #define TEST_FLOAT(x) #define ASSERT_FLOAT(x) #else #define A(x) assert(x); #define _TEST_FLOAT(x) ((x) <= LDBL_MAX && (x) >= -LDBL_MAX) #define ASSERT_FLOAT(f) A(_TEST_FLOAT(f)) #define D #endif #define V(x) (E<<#x<<": "<<(x)<<N) #define XX {E<<"XX LINE "<<__LINE__<<N;} #if defined DEBUG or defined ASSERTS #undef NDEBUG #else #define NDEBUG #endif #ifdef _WIN32 // TODO #ifndef NO_UNLOCKED #define NO_UNLOCKED #endif //#define fread_unlocked _fread_nolock //#define fwrite_unlocked _fwrite_nolock #endif #define all(x) (x).be,(x).en #define type(x) __typeof(x) const char N='\n'; const char S=' '; using namespace std; using namespace __gnu_cxx; namespace ab{} using namespace ab; template<class T>inline T cbitl(T x,int i){return (x<<i) ^ (x>>(sizeof(T)*8-i));} namespace std{ #if defined __GNUC__ and not MIN_GCC(4,2,0) template<class T>struct hash<T>{} #endif // 1,2,last (!!!!!) template<>template<class A,class B>struct hash<map<A,B>>{ size_t operator()(const map<A,B>& m) const{ size_t r = m.size(); if(!r)return r; auto i=m.be; r^=hash<type(i)>()(i); ++i; r^=cbitl(hash<type(i)>()(*i),sizeof(r)*8/4); i=m.end(); --i; r^=cbitl(hash<type(i)>()(*i),sizeof(r)*8/2); return r; } }; template<>template<class A, class B>struct hash<pair<A,B>>{ size_t operator()(const pair<A,B>& p) const{ return hash<A>()(p.first) ^ cbitl(hash<B>()(p.second),sizeof(size_t)*8/2); } }; } #define rint _rint() #define ruint _ruint() #define rll _rll() #define rull _rull() #define rchar _rchar() #ifndef FASTIO inline void fin(int){}inline void fout(int){} #ifdef IOSTREAM inline int _rint(){int r;cin>>r;return r;} inline uint _ruint(){uint r;cin>>r;return r;} inline ull _rull(){ull r;cin>>r;return r;} inline char _rchar(){return cin.get();} template<typename T>inline void W(const T& i){cout<<i;} inline void W(){W('\n');} #else inline int _rint(){int r;scanf("%d",&r);return r;} inline uint _ruint(){uint r;scanf("%u",&r);return r;} inline ull _rull(){ull r;scanf("%llu",&r);return r;} inline ull _rll(){ll r;scanf("%lld",&r);return r;} inline char _rchar(){return getchar();} inline void W(char a='\n'){printf("%c",a);} inline void W(int a){printf("%d",a);} inline void W(uint a){printf("%u",a);} inline void W(const ull& a){printf("%llu",a);} inline void W(const ll& a){printf("%lld",a);} inline void W(const char *str){fputs(str,stdout);} inline void W(const double& d){printf("%.20f",d);} inline void W(const long double& d){printf("%.50Lf",d);} #endif #else #ifndef NO_UNLOCKED #define FREAD fread_unlocked #define FWRITE fwrite_unlocked #else #define FREAD fread #define FWRITE fwrite #endif // todo: test on pointers const int _IB=32768;const int _OB=32768; char _iB[_IB];int _iP = _IB;char _oB[_OB];int _oP = 0; inline void fin(int mc){int r=_IB-_iP;if(r>mc)return;fo(i,r)_iB[i]=_iB[i+_iP]; // TODO: memcpy FREAD(_iB+r,1,_iP,stdin);_iP = 0;} inline void fout(int mc){if(_OB-_oP>mc)return;FWRITE(_oB,1,_oP,stdout);_oP = 0;} inline void W(char c='\n'){_oB[_oP++]=c;} inline char _rchar(){return _iB[_iP++];} template<class T>inline T _rur(){ T r=0;char c=rchar; do c-='0',r=r*10+c,c=rchar;while(c>='0'); return r;} template<class T>inline T _rsi(){ char c=rchar; while(c<'-')c=rchar; if(c=='-')return -_rur<T>(); else{--_iP;return _rur<T>();}} template<class T>inline T _ru(){while(rchar<'0');--_iP;return _rur<T>();} inline int _rint(){fin(15);return _rsi<int>();} inline uint _ruint(){fin(15);return _ru<uint>();} inline ll _rll(){fin(25);return _rsi<ll>();} inline ull _rull(){fin(25);return _ru<ull>();} const int _rs = 20;char _r[_rs]; template<class T>inline void _w(T i){ if(numeric_limits<T>::is_signed && i<0)W('-'),i=-i; else if(i==0){W('0');return;} int pos = _rs; do{_r[--pos]='0'+i%10;i/=10;}while(i); do W(_r[pos]),++pos;while(pos<_rs);} inline void W(const int32_t& i){fout(15);_w(i);} inline void W(const uint32_t& i){fout(15);_w(i);} inline void W(const int64_t& i){fout(25);_w(i);} inline void W(const uint64_t& i){fout(25);_w(i);} inline void W(const char*str){int l=strlen(str);fout(l);memcpy(&_oB[_oP],str,l);_oP+=l;} inline void W(double d){fout(50);_oP+=sprintf(&_oB[_oP],"%.20f",d);} // todo #endif template<class A,class B>inline void W(const A&a,const B&b){W(a);W(b);} template<class A,class B,class C> inline void W(const A&a,const B&b,const C&c){W(a);W(b);W(c);} template<class A,class B,class C,class DD> inline void W(const A&a,const B&b,const C&c,const DD&dd){W(a);W(b);W(c);W(dd);} template<class T>inline void sortall(T&t){sort(t.be,t.en);} template<class T>inline void uniqueall(T&t){t.resize(unique(t.be,t.en));} inline int count1(uint i){return __builtin_popcount(i);} inline int count1(ull i){return __builtin_popcountll(i);} inline int parity(uint i){return __builtin_parity(i);} inline int parity(ull i){return __builtin_parityll(i);} inline int ilog(uint i){return 31-__builtin_clz(i);} // int log; floor(log2(i)) inline int ilog(ull i){return 63-__builtin_clzll(i);} #define powerof2(x) !(x&(x-1)) #define subset(a,b) (a&b==a) #define kbegin(k) ((1<<k)-1) #define kend(k,n) ((kbegin(k)<<n-k)+1) inline uint knext(uint a){uint b=(a|(a-1))+1;return b|((a^b)>>(__builtin_ctz(a)+2));} #define fors(x,k,n) for(uint x=kbegin(k);x<kend(k,n);x=knext(x)) template<class A,class B>inline ostream&operator<<(ostream&o,const pair<A,B>&p){o<<'<'<<p.fi<<", "<<p.se<<'>';return o;} template<class A,class B>inline ostream&operator<<(ostream&o,const map<A,B>&m){o<<N;for(auto&k:m)o<<"map["<<k.fi<<"] -> "<<k.se<<N;return o;} template<class A>inline ostream&operator<<(ostream&o,const vector<A>&v){o<<N;fo(i,v.sz)o<<"vector["<<i<<"] = "<<v[i]<<N;return o;} struct _O{template<class T>_O&operator<<(const T&t){oc(1);W(t);oc(0);return*this;}}O; struct _I{ _I&operator>>(int32_t&t){t=rint;return*this;} _I&operator>>(uint32_t&t){t=ruint;return*this;} _I&operator>>(int64_t&t){t=rll;return*this;} _I&operator>>(uint64_t&t){t=rull;return*this;} }I; struct _E{template<class T>_E&operator<<(const T&t){D{ec(1);cerr<<t;ec(0);}return*this;}}E; inline void tc(); int main() { #ifdef FASTIO setvbuf(stdin,0,_IONBF,0);setvbuf(stdout,0,_IONBF,0); #elif defined IOSTREAM ios_base::sync_with_stdio(0);cin.tie(0);cout.precision(99);cout.setf(ios::fixed,ios::floatfield); #endif #ifdef DEBUG #ifdef _WIN32 ec(0); oc(0); #else // TODO: *nix #endif #endif #ifdef READ_NUM_TEST_CASES int nz=rint;fo(i,nz) #endif tc(); #ifdef FASTIO FWRITE(_oB,1,_oP,stdout); #endif return 0; } // END OF TEMPLATE int mod = 1000000007; inline int middle(int a, int b) { int s = a+b; if(s>=0) return s/2; else return (s-1)/2; } struct newnode { pii mval; int minval; } nodes[(1<<21)+9]; inline int getmin(int node, int a, int b, int fr, int to){ if(b<=fr || to<=a) return INT_MAX; if(fr<=a && b<=to) return nodes[node].minval; //V(fr);V(to); int s = middle(a,b); auto ll = getmin(node*2,a,s,fr,to); auto rr = getmin(node*2+1,s,b,fr,to); return min(ll,rr); } inline int getmin(int fr, int to){ return getmin(1,-1,(1<<20)-1,fr,to); } inline void ssetmin(int node, int a, int b, int val, int where){ if(a+1==b){ A(where==a); nodes[node].minval = val; return; } int s = middle(a,b); if(where >= s) ssetmin(node*2+1,s,b,val,where); else ssetmin(node*2,a,s,val,where); nodes[node].minval = min(nodes[node*2].minval, nodes[node*2+1].minval); } inline void ssetmin(int val, int where){ ssetmin(1,-1,(1<<20)-1,val,where); } inline void init(){ fo(i,(1<<21)+7)nodes[i].mval = pii(-INT_MAX,0), nodes[i].minval=INT_MAX; } // max,nummax inline pii get(int node, int a, int b, int fr, int to){ if(b<=fr || to<=a) return pii(-INT_MAX,0); if(fr<=a && b<=to) return nodes[node].mval; //V(fr);V(to); int s = middle(a,b); auto ll = get(node*2,a,s,fr,to); auto rr = get(node*2+1,s,b,fr,to); if(ll.fi>rr.fi) return ll; else if(ll.fi<rr.fi) return rr; else return mp(ll.fi,(ll.se+rr.se)%mod); } inline pii get(int fr, int to){ return get(1,-1,(1<<20)-1,fr,to); } inline void sset(int node, int a, int b, pii val, int where){ if(a+1==b){ if(where != a)V(where),V(a),V(b); A(where==a); nodes[node].mval = val; return; } int s = middle(a,b); if(where >= s) sset(node*2+1,s,b,val,where); else sset(node*2,a,s,val,where); if(nodes[node*2].mval.fi > nodes[node*2+1].mval.fi) nodes[node].mval = nodes[node*2].mval; else if(nodes[node*2].mval.fi < nodes[node*2+1].mval.fi) nodes[node].mval = nodes[node*2+1].mval; else nodes[node].mval = pii(nodes[node*2].mval.fi,(nodes[node*2].mval.se + nodes[node*2+1].mval.se)%mod); } inline void sset(pii val, int where){ sset(1,-1,(1<<20)-1,val,where); } int n; //int lo[1000009]; //int hi[1000009]; #ifdef DEBUG char test[1000009]; void testenable(int i){ E<<"enabling "<<i<<N; A(test[i+1] == 0); test[i+1]=1; } void testdisable(int i){ E<<"disabling "<<i<<N; A(test[i+1] == 1); test[i+1] = 0; } #else #define testenable(a) #define testdisable(a) #endif int tme = 0; pii _results[1000009]; pii *results = _results+1; //int id = 0; vector<int> active; bitset<1000009> activebit; pii allint[1000009]; int prevs[1000009]; int nexts[1000009]; int allintsz=0; int allintback=-1; priority_queue<pii,vector<pii>,greater<pii>> triggers; int pushint(pii inte){ if(allintback!=-1)nexts[allintback]=allintsz; prevs[allintsz] = allintback; nexts[allintsz] = -1; allintback = allintsz; ++allintsz; allint[allintback] = inte; E<<"pushint "<<inte.fi<<" "<<inte.se<<" -> "<<allintback<<N; return allintback; } void delint(int i){ if(i == allintback)allintback = prevs[i]; if(prevs[i]!=-1) nexts[prevs[i]] = nexts[i]; if(nexts[i]!=-1) prevs[nexts[i]] = prevs[i]; E<<"delint "<<i<<" ("<<allint[i].fi<<" "<<allint[i].se<<")"<<N; allint[i].fi = INT_MAX; activebit[i] = 0; } inline void disablerange(int fr, int to){ E<<"disable range "<<fr<<" "<<to<<N; fo(i,fr,to){ testdisable(i); sset(pii(-INT_MAX,0),i); } } inline void enablerange(int fr, int to){ E<<"enable range "<<fr<<" "<<to<<N; fo(i,fr,to){ testenable(i); sset(results[i],i); } } void addinterval(int fr, int to) { E<<"addinterval "<<fr<<" "<<to<<N; if(fr+tme >= to){ E<<"verdict: empty"<<N; return; } int delto = to; for(;;){ int zalastdel = 0; if(allintback!=-1)zalastdel = max(zalastdel,allint[allintback].se); if(allintback==-1 || zalastdel <= fr+tme){ if(fr+tme < -1){ E<<"verdict: queue begin"<<N; disablerange(-1,delto); int ontime = -1-fr; A(ontime>tme); A(fr+ontime < to); int nid = pushint(pii(fr,to)); triggers.push(pii(ontime,nid)); return; } E<<"verdict: active"<<N; disablerange(fr+tme,delto); int nid = pushint(pii(fr,to)); // activate active.pub(nid); activebit[nid]=1; return; } A(allintback!=-1); if(allint[allintback].fi < fr){ E<<"verdict: queue"<<N; disablerange(allint[allintback].se,delto); int ontime = allint[allintback].se - (fr+tme) + tme; A(ontime > tme); A(fr+ontime < to); int nid = pushint(pii(fr,to)); triggers.push(pii(ontime,nid)); return; } // dominating bool found = activebit[allintback]; disablerange(allint[allintback].se,delto); delto = allint[allintback].fi+tme; if(found){ // active! E<<"verdict: dominating active"<<N; // deactivate activebit[allintback] = 0; } else{ // inactive E<<"verdict: dominating inactive "<<allintback<<N; } delint(allintback); } } void advancetime() { E<<"SCHEDULED ENABLING:"<<N; fo(i,active.sz){ if(activebit[active[i]]==0){ active[i--]=active.back(); active.pob; continue; } int fr = allint[active[i]].fi; int to = allint[active[i]].se; E<<"enable id "<<active[i]<<" "<<fr<<" "<<to<<N; testenable(fr+tme); sset(results[fr+tme],fr+tme); if(fr+tme + 1 == to){ delint(active[i]); active[i--] = active.back(); active.pob; } } ++tme; while(!triggers.empty() && triggers.top().fi == tme){ int wid = triggers.top().se; triggers.pop(); if(allint[wid].fi==INT_MAX)continue; // not existing anymore // activate A(allint[wid].fi+tme < allint[wid].se); activebit[wid] = 1; active.pub(wid); } } void cleanup(int to){ // todo } inline void tc() { fo(i,1000003)allint[i].fi = INT_MAX; init(); I>>n; //fo(i,n)I>>lo[i]>>hi[i]; //fo(i,n)root.ssetmin(hi[i],i); int maxsq = 0; pii gresult; results[-1] = pii(0,1); sset(pii(0,1),-1); testenable(-1); int peak = 0; tme = 0; fo(i,n){ //if(i%1000==999){ //O<<"time "<<tme<<" peak "<<peak<<N;////////// //} int clo,chi; I>>clo>>chi; A(i==tme); ssetmin(chi,i); E<<N<<"time "<<i<<N; if(chi<=maxsq) maxsq = chi; else{ ++maxsq; int bound = getmin(tme+1-maxsq,tme+1); E<<"min ptree get "<<tme+1-maxsq<<" "<<tme+1<<" "<<bound<<N; A(maxsq-1 <= bound); maxsq = min(maxsq,bound); } addinterval(-(clo-1),tme); pii result = get(tme-maxsq,tme); cleanup(tme-maxsq-5); // optional result.fi += 1; E<<"result "<<result<<N; sset(result,tme); testenable(tme); if(i==n-1)gresult = result; results[tme]=result; peak = max(peak,(int)allintsz); advancetime(); } if(gresult.fi<-10) O<<"NIE"<<N; else O<<gresult.fi<<" "<<gresult.se<<N; //O<<peak<<N; //for(;;); } |