#include <cstdio> #include <algorithm> #include <vector> #include <random> #include <cstdlib> #define REP(i,n) for(int i=0; i<(n); ++i) #define FOR(i,p,k) for(int i=(p); i<=(k); ++i) #define FORD(i,p,k) for(int i=(p); i>=(k); --i) #define DEBUG if(0) template<typename T> inline void checkmin(T &a, T b){ if(a>b) a = b; } template<typename T> inline void checkmax(T &a, T b){ if(a<b) a = b; } std::mt19937 rnd(12878263); struct In { std::vector<int> L,H; void read() { int n; scanf("%d",&n); L.resize(n); H.resize(n); REP(i,n) scanf("%d%d",&L[i],&H[i]); } void gen(int n = 10000) { int x = 10; L.resize(n); REP(i,n) L[i] = rnd()%x+1; H.resize(n); REP(i,n) H[i] = rnd()%x+n; REP(i,n) if(L[i]>H[i]) std::swap(L[i],H[i]); } void print() { printf("%d\n",L.size()); REP(i,L.size()) printf("%d %d\n",L[i],H[i]); } }; struct Best { enum { M = 1000000007 }; Best(int _m = -1, int _c = 1) : m(_m), c(_c) {} int m,c; Best up() const { return Best(m<0?-1:m+1,c); } Best operator+(Best b) const { return m==b.m ? Best(m,(c+b.c)%M) : m>b.m ? *this : b; } Best & operator+=(Best b) { if(m<b.m) *this = b; else if(m==b.m) c = (c+b.c)%M; return *this; } bool operator==(Best b){ return m==-1 ? b.m==-1 : m==b.m && c==b.c; } bool operator!=(Best b){ return !(*this==b); } void print() const { if(m<0) printf("[NIE]\n"); else printf("[%d:%d]\n",m,c); } void print2() const { if(m<0) printf("[NIE] "); else printf("[%d:%d] ",m,c); } }; struct Tree { std::vector<Best> T; int hat; Tree(int n) { hat = 1; while(hat<n) hat<<=1; T.resize(hat*2); } }; struct GetTree : Tree { GetTree(int s) : Tree(s) {} int a,b; Best get(int t, int u, int v) { if(a<=u && v<=b) return T[t]; if(b<u || v<a) return Best(); int m = (u+v)/2; return get(t<<1,u,m)+get(t<<1|1,m+1,v); } Best get(int _a, int _b) // [a,b] { a = _a; b = _b; Best q = get(1,0,hat-1); DEBUG { printf("GT.get(%d;%d) = ",a,b); q.print(); } return q; } Best get(int _a){ return get(_a,_a); } void update(int a, Best q) { DEBUG { printf("GT.update(%d) = ",a); q.print(); } T[a += hat] = q; while(a>>=1) T[a] = T[a<<1]+T[a<<1|1]; DEBUG { printf("GT = "); REP(i,T.size()) T[i].print2(); puts(""); } } }; struct PutTree : Tree { PutTree(int s) : Tree(s) {} int a,b; Best q; void update(int t, int u, int v) { if(a<=u && v<=b) T[t] += q; else if(b<u || v<a) return; else { int m = (u+v)/2; update(t<<1,u,m); update(t<<1|1,m+1,v); } } void update(int _a, Best _q){ update(_a,_a,_q); } void update(int _a, int _b, Best _q) { a = _a; b = _b; q = _q; DEBUG { printf("PT.update(%d;%d) = ",a,b); q.print(); } update(1,0,hat-1); } Best get(int a) { int a2 = a; Best q = T[a += hat]; while(a>>=1) q += T[a]; DEBUG { printf("PT.get(%d) = ",a2); q.print(); } return q; } }; void make_h(const std::vector<int> &H, std::vector<int> &HR) { int n = H.size(); HR.resize(n); std::vector<int> Q(n); int qf=0,ql=0; int j = n; FORD(i,n-1,0) { while(qf<ql && H[Q[ql-1]]>H[i]) ql--; Q[ql++] = i; while(qf<ql && H[Q[qf]]<j-i) if(Q[qf]==--j) qf++; HR[i] = j-1; // [i,j] } } void print(const char *n, const std::vector<int> &v) { printf("%s = ",n); for(auto i : v) printf("%d ",i); puts(""); } Best go(const In &I) { int n = I.L.size(); // HL, HR std::vector<int> S(I.H.begin(),I.H.end()); std::vector<int> HR,HL; make_h(S,HR); std::reverse(S.begin(),S.end()); make_h(S,HL); std::reverse(HL.begin(),HL.end()); for(int &h : HL) h = n-h-1; // LL, LR std::vector<int> LL(n),LR(n); S.clear(); S.push_back(-1); REP(i,n) { while(S.size()>1 && I.L[S.back()]<I.L[i]) S.pop_back(); LL[i] = S.back(); S.push_back(i); } S.clear(); S.push_back(n); FORD(i,n-1,0) { while(S.size()>1 && I.L[S.back()]<=I.L[i]) S.pop_back(); LR[i] = S.back(); S.push_back(i); } DEBUG { print("HR",HR); print("HL",HL); print("LL",LL); print("LR",LR); } GetTree GT(n+1); GT.update(n,Best(0,1)); PutTree PT(n); FORD(i,n-1,0) { DEBUG printf("segments within [%d;%d] containing %d\n",LL[i]+1,LR[i]-1,i); if(i-LL[i]<LR[i]-i) { FOR(j,LL[i]+1,i) PT.update(j,GT.get(std::max(i+1,j+I.L[i]),std::min(LR[i],HR[j]+1)).up()); } else FOR(j,i,LR[i]-1) PT.update(std::max(HL[j],LL[i]+1),std::min(i,j-I.L[i]+1),GT.get(j+1).up()); GT.update(i,PT.get(i)); } return GT.get(0); } Best brute(const In &I) { int n = I.L.size(); //printf("I.L.size()=%d\n",I.L.size()); std::vector<Best> R(n+1); R[n] = Best(0,1); FORD(i,n-1,0) FOR(j,i+1,n) { int l = I.L[i], h = I.H[i]; FOR(k,i,j-1) { checkmin(h,I.H[k]); checkmax(l,I.L[k]); } if(j-i<=h && l<=j-i) R[i] += R[j].up(); } //REP(i,n+1) R[i].print(); return R[0]; } void test() { REP(_,3) { puts("max test"); In I; I.gen(1000000); go(I); } for(int c=0,yes=0;;c++) { In I; I.gen(); Best s = go(I); Best b = brute(I); if(s!=b) { printf("ERROR\n"); printf("s = "); s.print(); printf("b = "); b.print(); I.print(); exit(1); } if(b.m!=-1) yes++; if(!(c%100)) printf("ok %d; yes=%d\n",c,yes); } } int main(int argc, char **argv) { if(argc>1) { In I; I.gen(1000000); I.print(); Best r = go(I); fprintf(stderr,"res = %d:%d\n",r.m,r.c); return 0; } //test(); In I; I.read(); Best res = go(I); if(res.m<0) puts("NIE"); else printf("%d %d\n",res.m,res.c); 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 | #include <cstdio> #include <algorithm> #include <vector> #include <random> #include <cstdlib> #define REP(i,n) for(int i=0; i<(n); ++i) #define FOR(i,p,k) for(int i=(p); i<=(k); ++i) #define FORD(i,p,k) for(int i=(p); i>=(k); --i) #define DEBUG if(0) template<typename T> inline void checkmin(T &a, T b){ if(a>b) a = b; } template<typename T> inline void checkmax(T &a, T b){ if(a<b) a = b; } std::mt19937 rnd(12878263); struct In { std::vector<int> L,H; void read() { int n; scanf("%d",&n); L.resize(n); H.resize(n); REP(i,n) scanf("%d%d",&L[i],&H[i]); } void gen(int n = 10000) { int x = 10; L.resize(n); REP(i,n) L[i] = rnd()%x+1; H.resize(n); REP(i,n) H[i] = rnd()%x+n; REP(i,n) if(L[i]>H[i]) std::swap(L[i],H[i]); } void print() { printf("%d\n",L.size()); REP(i,L.size()) printf("%d %d\n",L[i],H[i]); } }; struct Best { enum { M = 1000000007 }; Best(int _m = -1, int _c = 1) : m(_m), c(_c) {} int m,c; Best up() const { return Best(m<0?-1:m+1,c); } Best operator+(Best b) const { return m==b.m ? Best(m,(c+b.c)%M) : m>b.m ? *this : b; } Best & operator+=(Best b) { if(m<b.m) *this = b; else if(m==b.m) c = (c+b.c)%M; return *this; } bool operator==(Best b){ return m==-1 ? b.m==-1 : m==b.m && c==b.c; } bool operator!=(Best b){ return !(*this==b); } void print() const { if(m<0) printf("[NIE]\n"); else printf("[%d:%d]\n",m,c); } void print2() const { if(m<0) printf("[NIE] "); else printf("[%d:%d] ",m,c); } }; struct Tree { std::vector<Best> T; int hat; Tree(int n) { hat = 1; while(hat<n) hat<<=1; T.resize(hat*2); } }; struct GetTree : Tree { GetTree(int s) : Tree(s) {} int a,b; Best get(int t, int u, int v) { if(a<=u && v<=b) return T[t]; if(b<u || v<a) return Best(); int m = (u+v)/2; return get(t<<1,u,m)+get(t<<1|1,m+1,v); } Best get(int _a, int _b) // [a,b] { a = _a; b = _b; Best q = get(1,0,hat-1); DEBUG { printf("GT.get(%d;%d) = ",a,b); q.print(); } return q; } Best get(int _a){ return get(_a,_a); } void update(int a, Best q) { DEBUG { printf("GT.update(%d) = ",a); q.print(); } T[a += hat] = q; while(a>>=1) T[a] = T[a<<1]+T[a<<1|1]; DEBUG { printf("GT = "); REP(i,T.size()) T[i].print2(); puts(""); } } }; struct PutTree : Tree { PutTree(int s) : Tree(s) {} int a,b; Best q; void update(int t, int u, int v) { if(a<=u && v<=b) T[t] += q; else if(b<u || v<a) return; else { int m = (u+v)/2; update(t<<1,u,m); update(t<<1|1,m+1,v); } } void update(int _a, Best _q){ update(_a,_a,_q); } void update(int _a, int _b, Best _q) { a = _a; b = _b; q = _q; DEBUG { printf("PT.update(%d;%d) = ",a,b); q.print(); } update(1,0,hat-1); } Best get(int a) { int a2 = a; Best q = T[a += hat]; while(a>>=1) q += T[a]; DEBUG { printf("PT.get(%d) = ",a2); q.print(); } return q; } }; void make_h(const std::vector<int> &H, std::vector<int> &HR) { int n = H.size(); HR.resize(n); std::vector<int> Q(n); int qf=0,ql=0; int j = n; FORD(i,n-1,0) { while(qf<ql && H[Q[ql-1]]>H[i]) ql--; Q[ql++] = i; while(qf<ql && H[Q[qf]]<j-i) if(Q[qf]==--j) qf++; HR[i] = j-1; // [i,j] } } void print(const char *n, const std::vector<int> &v) { printf("%s = ",n); for(auto i : v) printf("%d ",i); puts(""); } Best go(const In &I) { int n = I.L.size(); // HL, HR std::vector<int> S(I.H.begin(),I.H.end()); std::vector<int> HR,HL; make_h(S,HR); std::reverse(S.begin(),S.end()); make_h(S,HL); std::reverse(HL.begin(),HL.end()); for(int &h : HL) h = n-h-1; // LL, LR std::vector<int> LL(n),LR(n); S.clear(); S.push_back(-1); REP(i,n) { while(S.size()>1 && I.L[S.back()]<I.L[i]) S.pop_back(); LL[i] = S.back(); S.push_back(i); } S.clear(); S.push_back(n); FORD(i,n-1,0) { while(S.size()>1 && I.L[S.back()]<=I.L[i]) S.pop_back(); LR[i] = S.back(); S.push_back(i); } DEBUG { print("HR",HR); print("HL",HL); print("LL",LL); print("LR",LR); } GetTree GT(n+1); GT.update(n,Best(0,1)); PutTree PT(n); FORD(i,n-1,0) { DEBUG printf("segments within [%d;%d] containing %d\n",LL[i]+1,LR[i]-1,i); if(i-LL[i]<LR[i]-i) { FOR(j,LL[i]+1,i) PT.update(j,GT.get(std::max(i+1,j+I.L[i]),std::min(LR[i],HR[j]+1)).up()); } else FOR(j,i,LR[i]-1) PT.update(std::max(HL[j],LL[i]+1),std::min(i,j-I.L[i]+1),GT.get(j+1).up()); GT.update(i,PT.get(i)); } return GT.get(0); } Best brute(const In &I) { int n = I.L.size(); //printf("I.L.size()=%d\n",I.L.size()); std::vector<Best> R(n+1); R[n] = Best(0,1); FORD(i,n-1,0) FOR(j,i+1,n) { int l = I.L[i], h = I.H[i]; FOR(k,i,j-1) { checkmin(h,I.H[k]); checkmax(l,I.L[k]); } if(j-i<=h && l<=j-i) R[i] += R[j].up(); } //REP(i,n+1) R[i].print(); return R[0]; } void test() { REP(_,3) { puts("max test"); In I; I.gen(1000000); go(I); } for(int c=0,yes=0;;c++) { In I; I.gen(); Best s = go(I); Best b = brute(I); if(s!=b) { printf("ERROR\n"); printf("s = "); s.print(); printf("b = "); b.print(); I.print(); exit(1); } if(b.m!=-1) yes++; if(!(c%100)) printf("ok %d; yes=%d\n",c,yes); } } int main(int argc, char **argv) { if(argc>1) { In I; I.gen(1000000); I.print(); Best r = go(I); fprintf(stderr,"res = %d:%d\n",r.m,r.c); return 0; } //test(); In I; I.read(); Best res = go(I); if(res.m<0) puts("NIE"); else printf("%d %d\n",res.m,res.c); return 0; } |