——工欲善其事,必先利其器html
儘管不會鬆鬆鬆,可是至少維持一個比較小的常數仍是比較好的node
在此以前依然要保證算法的正確性以及代碼的可寫性linux
本文依然會持久更新,由於一次寫不完ios
這個的重要性就不強調了,輕則多$log$,重則爆$n^2$,更使人窒息者爲多項式和非多項式的區別c++
設計一個好的算法,首先不要想着如何去用$O(n^2)$碾壓$O(n)$,而是先想如何實現$O(n)$纔是比較好的git
洛咕日報15期(霸佔評論區三天2333),關於基數排序的算法
IO輸出要麼沒有顯著優化,要麼直接從TLE優化到AC,在另外一篇博客有介紹http://www.javashuo.com/article/p-pyddlhsy-dc.html緩存
而後下面放上一些我平時用的貼在代碼前面且具備不一樣功能的一些東西:數據結構
1. 短一些,只有一個小的工具包,沒有使用要求dom
1 #include <cstdio> 2 #include <cctype> 3 #include <cstring> 4 5 //User's Lib 6 7 using namespace std; 8 9 char buf[11111111], *pc = buf; 10 11 struct Main_Guard{ 12 Main_Guard(){ 13 fread(buf, 1, 11111111, stdin); 14 } 15 Main_Guard(const char *ins){ 16 freopen(ins, "r", stdin); 17 fread(buf, 1, 11111111, stdin); 18 } 19 Main_Guard(const char *ins, const char *ous){ 20 freopen(ins, "r", stdin); 21 freopen(ous, "w", stdout); 22 fread(buf, 1, 11111111, stdin); 23 } 24 ~ Main_Guard(){ 25 fclose(stdin), fclose(stdout); 26 } 27 }; 28 29 static inline int read(){ 30 int num = 0; 31 char c, sf = 1; 32 while(isspace(c = *pc++)); 33 if(c == 45) sf = -1, c = *pc ++; 34 while(num = num * 10 + c - 48, isdigit(c = *pc++)); 35 return num * sf; 36 } 37 38 namespace LKF{ 39 template <typename T> 40 extern inline T abs(T tar){ 41 return tar < 0 ? -tar : tar; 42 } 43 template <typename T> 44 extern inline void swap(T &a, T &b){ 45 T t = a; 46 a = b; 47 b = t; 48 } 49 template <typename T> 50 extern inline void upmax(T &x, const T &y){ 51 if(x < y) x = y; 52 } 53 template <typename T> 54 extern inline void upmin(T &x, const T &y){ 55 if(x > y) x = y; 56 } 57 template <typename T> 58 extern inline T max(T a, T b){ 59 return a > b ? a : b; 60 } 61 template <typename T> 62 extern inline T min(T a, T b){ 63 return a < b ? a : b; 64 } 65 } 66 67 //Source Code
2. 長一些,分類討論一些開關,而後不是全部東西保證效率,功能多
1 //Created By Creeper_LKF 2 //Caution::We used "pragma" in the code 3 #include <cstdio> 4 #include <cctype> 5 #include <cassert> 6 #include <cstdlib> 7 #include <cstring> 8 #include <iostream> 9 10 #ifdef __gnu_linux__ 11 12 #include <fcntl.h> 13 #include <unistd.h> 14 #include <sys/mman.h> 15 16 #endif 17 18 #if __cplusplus < 201103L 19 20 #include <stdarg.h> 21 22 #endif 23 24 //Algorithm Heads 25 26 27 using namespace std; 28 29 //Debug Port 30 31 // #define DEBUG_PORT 32 #define DEBUG 33 34 #ifdef ONLINE_JUDGE 35 #undef DEBUG_PORT 36 #undef DEBUG 37 #endif 38 39 #ifdef DEBUG_PORT 40 #if __cplusplus >= 201103L 41 #ifdef DEBUG 42 template<typename T> 43 extern inline void Debug(T tar){ 44 cerr << tar << endl; 45 } 46 template<typename Head, typename T, typename... Tail> 47 extern inline void Debug(Head head, T mid, Tail... tail){ 48 cerr << head << ' '; 49 Debug(mid, tail...); 50 } 51 #else 52 template<typename Head, typename T, typename... Tail> 53 extern inline void Debug(Head, T, Tail...){ 54 return ; 55 } 56 #endif 57 #else 58 # pragma message "Warning : C++11 Not Use" 59 #ifdef DEBUG 60 template <typename T> 61 extern inline void Debug(T tar){ 62 cerr << tar << endl; 63 } 64 #else 65 template <typename T> 66 extern inline void Debug(T){ 67 return ; 68 } 69 #endif 70 #endif 71 #else 72 template<typename Head, typename T, typename... Tail> 73 extern inline void Debug(Head, T, Tail...){ 74 return ; 75 } 76 template <typename T> 77 extern inline void Debug(T){ 78 return ; 79 } 80 #endif 81 82 const char file_name[] = "b"; 83 84 #define NAME_SPACE 85 #define USING 86 87 #ifdef NAME_SPACE 88 namespace LKF{ 89 #endif 90 #define SF_READ 91 #define EOF_READ 92 // #define ONLINE_JUDGE 93 #define WRITE_ENDL 94 // #define FAST_WRITE 95 #define SPLIT_WRITE 96 const size_t MAX_BUF_SIZE = 50000000; 97 98 #define NEED_FILE 99 100 #ifdef FAST_WRITE 101 char outp[MAX_BUF_SIZE], *op = outp; 102 #endif 103 104 #ifdef ONLINE_JUDGE 105 #undef NEED_FILE 106 #endif 107 108 #ifdef FAST_WRITE 109 #ifndef WRITE_ENDL 110 #define WRITE_ENDL 111 #endif 112 #endif 113 114 extern inline void FILE_OPT(){ 115 #ifdef NEED_FILE 116 #define FILE_NAME file_name 117 char IN_FILE[sizeof(FILE_NAME) + 5], OUT_FILE[sizeof(FILE_NAME) + 5]; 118 strcpy(IN_FILE, FILE_NAME), strcpy(OUT_FILE, FILE_NAME); 119 strcat(IN_FILE, ".in"), strcat(OUT_FILE, ".out"); 120 freopen(IN_FILE, "r", stdin); 121 freopen(OUT_FILE, "w", stdout); 122 #endif 123 } 124 125 #ifdef __gnu_linux__ 126 127 char *pc; 128 129 struct Main_Guard{ 130 Main_Guard(){ 131 #ifndef ONLINE_JUDGE 132 FILE_OPT(); 133 pc = (char *) mmap(NULL, lseek(0, 0, SEEK_END), PROT_READ, MAP_PRIVATE, 0, 0); 134 #endif 135 } 136 ~ Main_Guard(){ 137 #ifdef FAST_WRITE 138 fwrite(outp, 1, op - outp, stdout); 139 #endif 140 fclose(stdin), fclose(stdout); 141 } 142 }; 143 144 #else 145 146 char buf[MAX_BUF_SIZE], *pc = buf; 147 148 struct Main_Guard{ 149 Main_Guard(){ 150 FILE_OPT(); 151 fread(buf, 1, MAX_BUF_SIZE, stdin); 152 } 153 ~ Main_Guard(){ 154 #ifdef FAST_WRITE 155 fwrite(outp, 1, op - outp, stdout); 156 #endif 157 fclose(stdin), fclose(stdout); 158 } 159 }; 160 161 #endif 162 163 inline char read_ch(){ 164 char c; 165 while(isspace(c = *pc ++)); 166 return c; 167 } 168 169 #ifdef EOF_READ 170 171 #ifdef SF_READ 172 173 template<typename T> 174 static inline void read(T &num){ 175 num = 0; 176 char c, sf = 1; 177 while(isspace(c = *pc++)); 178 if(c == 45) sf = -1, c = *pc ++; 179 while(num = num * 10 + c - 48, isdigit(c = *pc++)); 180 num *= sf; 181 } 182 183 static inline int read(){ 184 int num = 0; 185 char c, sf = 1; 186 while(isspace(c = *pc++)); 187 if(c == 45) sf = -1, c = *pc ++; 188 while(num = num * 10 + c - 48, isdigit(c = *pc++)); 189 return num * sf; 190 } 191 192 static inline double read_dec(){ 193 double num = 0, decs = 1; 194 char c, sf = 1; 195 while(isspace(c = *pc ++)); 196 if(c == '-') sf = -1, c = *pc ++; 197 while(num = num * 10 + c - 48, isdigit(c = *pc ++)); 198 if(c != '.') return num * sf; 199 c = *pc ++; 200 while(num += (decs *= 0.1) * (c - 48), isdigit(c = *pc ++)); 201 return num * sf; 202 } 203 204 #else 205 206 template<typename T> 207 static inline T read(T &num){ 208 num = 0; 209 char c; 210 while (isspace(c = *pc++)); 211 while (num = num * 10 + c - 48, isdigit(c = *pc++)); 212 return num; 213 } 214 215 static inline int read(){ 216 int num = 0; 217 char c; 218 while (isspace(c = *pc++)); 219 while (num = num * 10 + c - 48, isdigit(c = *pc++)); 220 return num; 221 } 222 223 static inline double read_dec(){ 224 double num = 0, decs = 1; 225 char c; 226 while(isspace(c = *pc ++)); 227 while(num = num * 10 + c - 48, isdigit(c = *pc ++)); 228 if(c != '.') return num; 229 c = *pc ++; 230 while(num += (c - 48) * (decs *= 0.1), isdigit(c = *pc ++)); 231 return num; 232 } 233 234 #endif 235 236 #else 237 238 #ifdef SF_READ 239 240 template<typename T> 241 static inline void read(T &num){ 242 num = 0; 243 char c, sf = 1; 244 while((c = *pc++) < 45); 245 if(c == 45) sf = -1, c = *pc ++; 246 while(num = num * 10 + c - 48, (c = *pc++) >= 48); 247 num *= sf; 248 } 249 250 static inline int read(){ 251 int num = 0; 252 char c, sf = 1; 253 while((c = *pc++) < 45); 254 if(c == 45) sf = -1, c = *pc ++; 255 while(num = num * 10 + c - 48, (c = *pc++) >= 48); 256 return num * sf; 257 } 258 259 static inline double read_dec(){ 260 double num = 0, decs = 1; 261 char c, sf = 1; 262 while(isspace(c = *pc ++)); 263 if(c == '-') sf = -1, c = *pc ++; 264 while(num = num * 10 + c - 48, isdigit(c = *pc ++)); 265 if(c != '.') return num * sf; 266 c = *pc ++; 267 while(num += (decs *= 0.1) * (c - 48), isdigit(c = *pc ++)); 268 return num * sf; 269 } 270 271 #else 272 273 template<typename T> 274 static inline T read(T &num){ 275 num = 0; 276 char c; 277 while ((c = *pc++) < 48); 278 while (num = num * 10 + c - 48, (c = *pc++) >= 48); 279 return num; 280 } 281 282 static inline int read(){ 283 int num = 0; 284 char c; 285 while ((c = *pc++) < 48); 286 while (num = num * 10 + c - 48, (c = *pc++) >= 48); 287 return num; 288 } 289 290 static inline double read_dec(){ 291 double num = 0, decs = 1; 292 char c; 293 while(isspace(c = *pc ++)); 294 while(num = num * 10 + c - 48, isdigit(c = *pc ++)); 295 if(c != '.') return num; 296 c = *pc ++; 297 while(num += (c - 48) * (decs *= 0.1), isdigit(c = *pc ++)); 298 return num; 299 } 300 301 #endif 302 303 #endif 304 305 #ifdef FAST_WRITE 306 template <typename T> 307 inline void Call_Write(char Split, T tar){ 308 char buf[20]; 309 int top = 0; 310 if(tar == 0) *op ++ = 48; 311 else { 312 if(tar < 0) *op ++ = '-', tar = -tar; 313 while(tar) buf[++top] = tar % 10, tar /= 10; 314 while(top) *op ++ = buf[top --] ^ 48; 315 } 316 *op ++ = Split; 317 } 318 template <typename T> 319 inline void Call_Write(T tar){ 320 char buf[20]; 321 int top = 0; 322 if(tar == 0) *op ++ = 48; 323 else { 324 if(tar < 0) *op ++ = '-', tar = -tar; 325 while(tar) buf[++top] = tar % 10, tar /= 10; 326 while(top) *op ++ = buf[top --] ^ 48; 327 } 328 } 329 #endif 330 331 #ifdef FAST_WRITE 332 333 extern inline void write(){ 334 *op ++ = '\n'; 335 } 336 337 template<typename T> 338 extern inline void write(T tar){ 339 Call_Write(tar); 340 #ifdef WRITE_ENDL 341 write(); 342 #endif 343 } 344 345 #if __cplusplus >= 201103L 346 347 template<typename T> 348 extern inline void write(char, T tar){ 349 Call_Write(tar); 350 #ifdef WRITE_ENDL 351 write(); 352 #endif 353 } 354 355 template<typename Head, typename T, typename... Tail> 356 extern inline void write(char Split, Head head, T mid, Tail... tail){ 357 Call_Write(Split, head); 358 write(Split, mid, tail...); 359 } 360 361 #else 362 363 template <typename T> 364 extern inline void write(char Split, T tar){ 365 Call_Write(tar); 366 #ifdef WRITE_ENDL 367 write(); 368 #endif 369 } 370 371 #endif 372 373 #else 374 375 extern inline void write(){ 376 cout << endl; 377 } 378 379 template<typename T> 380 extern inline void write(T tar){ 381 cout << tar; 382 #ifdef WRITE_ENDL 383 write(); 384 #endif 385 } 386 387 #if __cplusplus >= 201103L 388 389 template<typename T> 390 extern inline void write(char Split, T tar){ 391 cout << tar << Split; 392 #ifdef WRITE_ENDL 393 write(); 394 #endif 395 } 396 397 template<typename Head, typename T, typename... Tail> 398 extern inline void write(char Split, Head head, T mid, Tail... tail){ 399 #ifdef SPLIT_WRITE 400 cout << head << Split; 401 #else 402 cout << head; 403 #endif 404 write(Split, mid, tail...); 405 } 406 407 #else 408 409 template <typename T> 410 extern inline void write(char Split, T tar){ 411 cout << tar << Split; 412 #ifdef WRITE_ENDL 413 write(); 414 #endif 415 } 416 417 #endif 418 419 #endif 420 421 template <typename T> 422 extern inline void upmax(T &x, const T &y){ 423 if(x < y) x = y; 424 } 425 template <typename T> 426 extern inline void upmin(T &x, const T &y){ 427 if(x > y) x = y; 428 } 429 430 #if __cplusplus >= 201103L 431 432 template<typename T> 433 extern inline T max(T tar){ 434 return tar; 435 } 436 437 template<typename T> 438 extern inline T min(T tar){ 439 return tar; 440 } 441 442 template <typename Head, typename T, typename... Tail> 443 extern inline Head max(Head head, T mid, Tail... tail){ 444 Head tmp = max(mid, tail...); 445 return head > tmp ? head : tmp; 446 } 447 template <typename Head, typename T, typename... Tail> 448 extern inline Head min(Head head, T mid, Tail... tail){ 449 Head tmp = min(mid, tail...); 450 return head < tmp ? head : tmp; 451 } 452 453 #else 454 455 template <typename T> 456 extern inline T max(T a, T b){ 457 return a > b ? a : b; 458 } 459 template <typename T> 460 extern inline T min(T a, T b){ 461 return a < b ? a : b; 462 } 463 464 #endif 465 466 template <typename T> 467 extern inline T abs(T tar){ 468 return tar < 0 ? -tar : tar; 469 } 470 template <typename T> 471 extern inline void swap(T &a, T &b){ 472 T t = a; 473 a = b; 474 b = t; 475 } 476 #ifdef NAME_SPACE 477 } 478 #endif 479 480 //Algorithm 481 482 #ifdef NAME_SPACE 483 namespace LKF{ 484 #endif 485 486 template <class Tn, size_t ArraySize> 487 struct Queue{ 488 size_t s, t; 489 Tn q[ArraySize]; 490 Queue(){ 491 s = 1, t = 0; 492 } 493 inline void clear(){ 494 s = 1, t = 0; 495 } 496 inline bool empty(){ 497 return s > t; 498 } 499 inline size_t size(){ 500 return t - s + 1; 501 } 502 inline void push(Tn tar){ 503 q[++ t] = tar; 504 } 505 inline void pop_front(){ 506 s ++; 507 } 508 inline void pop_back(){ 509 t --; 510 } 511 inline Tn front(){ 512 return q[s]; 513 } 514 inline Tn back(){ 515 return q[t]; 516 } 517 }; 518 519 template <class Tn, size_t ArraySize> 520 struct Stack{ 521 size_t t; 522 Tn s[ArraySize]; 523 Stack(){ 524 t = 0; 525 } 526 inline void clear(){ 527 t = 0; 528 } 529 inline bool empty(){ 530 return t == 0; 531 } 532 inline size_t size(){ 533 return t; 534 } 535 inline void push(Tn tar){ 536 s[++ t] = tar; 537 } 538 inline Tn top(){ 539 return s[t]; 540 } 541 inline void pop(){ 542 t --; 543 } 544 }; 545 546 #ifdef NAME_SPACE 547 } 548 #endif 549 550 #ifdef USING 551 552 #ifdef NAME_SPACE 553 using LKF::pc; 554 using LKF::read_ch; 555 using LKF::read_dec; 556 using LKF::read; 557 using LKF::write; 558 using LKF::upmax; 559 using LKF::upmin; 560 using LKF::max; 561 using LKF::min; 562 using LKF::abs; 563 // using LKF::swap; 564 #else 565 using ::pc; 566 using ::read_ch; 567 using ::read_dec; 568 using ::read; 569 using ::write; 570 using ::upmax; 571 using ::upmin; 572 using ::max; 573 using ::min; 574 using ::abs; 575 // using ::swap; 576 #endif 577 578 #endif 579 580 //Source Code
3. C++11下可使用調試多參數調試 Debug(arg1, arg2...) ,建議搭配dot能夠直接圖論題中繪圖,能夠不刪除調試代碼交到Luogu上
1 #include <cstdio> 2 #include <cctype> 3 #include <cstring> 4 #include <iostream> 5 6 //User's Lib 7 8 using namespace std; 9 10 // #define DEBUG_PORT 11 #define DEBUG 12 13 #ifdef ONLINE_JUDGE 14 #undef DEBUG_PORT 15 #undef DEBUG 16 #endif 17 18 #ifdef DEBUG_PORT 19 #if __cplusplus >= 201103L 20 #ifdef DEBUG 21 template<typename T> 22 extern inline void Debug(T tar){ 23 cerr << tar << endl; 24 } 25 template<typename Head, typename T, typename... Tail> 26 extern inline void Debug(Head head, T mid, Tail... tail){ 27 cerr << head << ' '; 28 Debug(mid, tail...); 29 } 30 #else 31 template<typename Head, typename T, typename... Tail> 32 extern inline void Debug(Head, T, Tail...){ 33 return ; 34 } 35 #endif 36 #else 37 # pragma message "Warning : C++11 Not Use" 38 #ifdef DEBUG 39 template <typename T> 40 extern inline void Debug(T tar){ 41 cerr << tar << endl; 42 } 43 #else 44 template <typename T> 45 extern inline void Debug(T){ 46 return ; 47 } 48 #endif 49 #endif 50 #else 51 template<typename Head, typename T, typename... Tail> 52 extern inline void Debug(Head, T, Tail...){ 53 return ; 54 } 55 template <typename T> 56 extern inline void Debug(T){ 57 return ; 58 } 59 #endif 60 61 char buf[11111111], *pc = buf; 62 63 struct Main_Guard{ 64 Main_Guard(){ 65 fread(buf, 1, 11111111, stdin); 66 } 67 Main_Guard(const char *ins){ 68 freopen(ins, "r", stdin); 69 fread(buf, 1, 11111111, stdin); 70 } 71 Main_Guard(const char *ins, const char *ous){ 72 freopen(ins, "r", stdin); 73 freopen(ous, "w", stdout); 74 fread(buf, 1, 11111111, stdin); 75 } 76 ~ Main_Guard(){ 77 fclose(stdin), fclose(stdout); 78 } 79 }; 80 81 static inline int read(){ 82 int num = 0; 83 char c, sf = 1; 84 while(isspace(c = *pc++)); 85 if(c == 45) sf = -1, c = *pc ++; 86 while(num = num * 10 + c - 48, isdigit(c = *pc++)); 87 return num * sf; 88 } 89 90 namespace LKF{ 91 template <typename T> 92 extern inline T abs(T tar){ 93 return tar < 0 ? -tar : tar; 94 } 95 template <typename T> 96 extern inline void swap(T &a, T &b){ 97 T t = a; 98 a = b; 99 b = t; 100 } 101 template <typename T> 102 extern inline void upmax(T &x, const T &y){ 103 if(x < y) x = y; 104 } 105 template <typename T> 106 extern inline void upmin(T &x, const T &y){ 107 if(x > y) x = y; 108 } 109 template <typename T> 110 extern inline T max(T a, T b){ 111 return a > b ? a : b; 112 } 113 template <typename T> 114 extern inline T min(T a, T b){ 115 return a < b ? a : b; 116 } 117 } 118 119 //Source Code
4. 只能讀非負整數,而後用的是更快的讀入,可是在本地都開了文件輸入輸出
1 #include <cstdio> 2 3 #include <fcntl.h> 4 #include <unistd.h> 5 #include <sys/mman.h> 6 7 //User's Lib 8 9 using namespace std; 10 11 char *pc; 12 13 char outp[1111111], *op = outp; 14 15 struct Main_Guard{ 16 Main_Guard(){ 17 pc = (char *) mmap(NULL, lseek(0, 0, SEEK_END), PROT_READ, MAP_PRIVATE, 0, 0); 18 } 19 Main_Guard(const char *ins){ 20 freopen(ins, "r", stdin); 21 pc = (char *) mmap(NULL, lseek(0, 0, SEEK_END), PROT_READ, MAP_PRIVATE, 0, 0); 22 } 23 Main_Guard(const char *ins, const char *ous){ 24 freopen(ins, "r", stdin); 25 freopen(ous, "w", stdout); 26 pc = (char *) mmap(NULL, lseek(0, 0, SEEK_END), PROT_READ, MAP_PRIVATE, 0, 0); 27 } 28 ~ Main_Guard(){ 29 fwrite(outp, 1, op - outp, stdout); 30 fclose(stdin), fclose(stdout); 31 } 32 }; 33 34 inline int read(){ 35 int num = 0; 36 char c; 37 while((c = *pc ++) < 48); 38 while(num = num * 10 + c - 48, (c = *pc ++) >= 48); 39 return num; 40 } 41 42 inline void Write(const char *tar){ 43 for(register int i = 0, lim = strlen(tar); i < lim; i++) 44 *op ++ = tar[i]; 45 } 46 47 inline void Write(const int &tar){//You Need To Write '-' and '\n' By Hand 48 if(!tar) return ; 49 Write(tar / 10); 50 *op ++ = (tar % 10) ^ 48; 51 } 52 53 namespace LKF{ 54 template <typename T> 55 extern inline T abs(T tar){ 56 return tar < 0 ? -tar : tar; 57 } 58 template <typename T> 59 extern inline void swap(T &a, T &b){ 60 T t = a; 61 a = b; 62 b = t; 63 } 64 template <typename T> 65 extern inline void upmax(T &x, const T &y){ 66 if(x < y) x = y; 67 } 68 template <typename T> 69 extern inline void upmin(T &x, const T &y){ 70 if(x > y) x = y; 71 } 72 template <typename T> 73 extern inline T max(T a, T b){ 74 return a > b ? a : b; 75 } 76 template <typename T> 77 extern inline T min(T a, T b){ 78 return a < b ? a : b; 79 } 80 } 81 82 //Source Code
5. C++11特性用,刪掉了分類討論
1 #include <cstdio> 2 #include <cctype> 3 #include <cstring> 4 #include <iostream> 5 6 //User's Lib 7 8 using namespace std; 9 10 #define DEBUG 11 12 #ifdef ONLINE_JUDGE 13 #undef DEBUG 14 #endif 15 16 #ifdef DEBUG 17 template<typename T> 18 extern inline void Debug(T tar){ 19 cerr << tar << endl; 20 } 21 template<typename Head, typename T, typename... Tail> 22 extern inline void Debug(Head head, T mid, Tail... tail){ 23 cerr << head << ' '; 24 Debug(mid, tail...); 25 } 26 #else 27 template<typename Head, typename T, typename... Tail> 28 extern inline void Debug(Head, T, Tail...){ 29 return ; 30 } 31 #endif 32 33 char buf[11111111], *pc = buf; 34 35 struct Main_Guard{ 36 Main_Guard(){ 37 fread(buf, 1, 11111111, stdin); 38 } 39 Main_Guard(const char *ins){ 40 freopen(ins, "r", stdin); 41 fread(buf, 1, 11111111, stdin); 42 } 43 Main_Guard(const char *ins, const char *ous){ 44 freopen(ins, "r", stdin); 45 freopen(ous, "w", stdout); 46 fread(buf, 1, 11111111, stdin); 47 } 48 ~ Main_Guard(){ 49 fclose(stdin), fclose(stdout); 50 } 51 }; 52 53 static inline int read(){ 54 int num = 0; 55 char c, sf = 1; 56 while(isspace(c = *pc++)); 57 if(c == 45) sf = -1, c = *pc ++; 58 while(num = num * 10 + c - 48, isdigit(c = *pc++)); 59 return num * sf; 60 } 61 62 namespace LKF{ 63 template <typename T> 64 extern inline void upmax(T &x, const T &y){ 65 if(x < y) x = y; 66 } 67 template <typename T> 68 extern inline void upmin(T &x, const T &y){ 69 if(x > y) x = y; 70 } 71 72 template<typename T> 73 extern inline T max(T tar){ 74 return tar; 75 } 76 77 template<typename T> 78 extern inline T min(T tar){ 79 return tar; 80 } 81 82 template <typename Head, typename T, typename... Tail> 83 extern inline Head max(Head head, T mid, Tail... tail){ 84 Head tmp = max(mid, tail...); 85 return head > tmp ? head : tmp; 86 } 87 template <typename Head, typename T, typename... Tail> 88 extern inline Head min(Head head, T mid, Tail... tail){ 89 Head tmp = min(mid, tail...); 90 return head < tmp ? head : tmp; 91 } 92 93 template <typename T> 94 extern inline T abs(T tar){ 95 return tar < 0 ? -tar : tar; 96 } 97 template <typename T> 98 extern inline void swap(T &a, T &b){ 99 T t = a; 100 a = b; 101 b = t; 102 } 103 } 104 105 //Source Code
6. 最簡單的快讀
1 #include <cstdio> 2 3 using namespace std; 4 5 char buf[11111111], *pc = buf; 6 7 struct Main_Guard{ 8 Main_Guard(){ 9 fread(buf, 1, 11111111, stdin); 10 } 11 Main_Guard(const char *ins){ 12 freopen(ins, "r", stdin); 13 fread(buf, 1, 11111111, stdin); 14 } 15 Main_Guard(const char *ins, const char *ous){ 16 freopen(ins, "r", stdin); 17 freopen(ous, "w", stdout); 18 fread(buf, 1, 11111111, stdin); 19 } 20 ~ Main_Guard(){ 21 fclose(stdin), fclose(stdout); 22 } 23 }; 24 25 inline int read(){ 26 int num = 0; 27 char c; 28 while((c = *pc ++) < 48); 29 while(num = num * 10 + c - 48, (c = *pc ++) >= 48); 30 return num; 31 } 32 33 //Source Code
因爲最近改了一下,因此可能還會有一點Bug
Main_Guard調用方法就是在main函數開頭寫 Main_Guard main_guard; 若是須要讀入的話就在後面帶括號和文件名,輸出的話就帶兩個字符串
這樣的好處就是必定會調用析構函數
一個很是妙並且實用的工具,有些C++庫函數會調用到,不過本身去學會調用確定會比較妙,整個Builtin家族很是大(看文檔https://gcc.gnu.org/onlinedocs/gcc/,具體能夠在https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#Other-Builtins查找),這裏只介紹一些經常使用的。下面的函數的參數以及返回值對照了gcc文檔。
(注意下面函數定義在unsigned int(或int)上,若是須要使用unsigned long long(或long long)版則能夠在函數名後面加上ll,例如__builtin_ffsll接受long long的參數)(還有一個__builtin_clrsb看不懂什麼玩意)。
long __builtin_expect (long exp, long c)
函數的做用就是引導CPU在分支預測的時候選擇某個if分支執行,之前靠這個東西直接把一道題卡到了0ms,單點甩Rank2接近8ms,可見有些狀況下效果仍是不錯的
食用方法:exp那裏填寫你但願預測的一個表達式,建議不要寫的太複雜了,c填寫你預測表達式大機率會是什麼值,而後直接用在if內便可,例如 if(__builtin_expect(a === b, 0))
效果:若是寫的比較好的話有一些大優化,不然會致使CPU頻繁罰時
例子:例如你知道某個數據結構它的體型龐大並且你有特地地防止node指針指向NULL,可是若是你不放心的話那麼能夠加一個這個。例如表達式$i%100000000==1$通常會比較難成立,能夠優化,而$i%10==1$就不須要這樣優化了
void __builtin_prefetch (const void *addr, ...)
就是提早把數據取到緩存
gcc的參數表最後面還有兩個可選參數,rw和locality,表示使用這個數據是讀或者寫(1是寫0是讀),以及這個數據的時間局部性。
讀或者寫就不說了
時間局部性表示這個數據在被訪問以後在必定時間內還會不會再次訪問(而不是距離第一次訪問還有多久),決定了這個數據在緩存中的「壽命」。0表示沒有時間局部性,也就是很長時間內再也不訪問,而3表示高時間局部性,表示訪問了時候很快又會訪問。這個參數必定是個編譯時候的常量,默認爲3.
首先register和inline標記應該都會吧。register表示建議編譯器將變量放在編譯器中,C++11及以上爲了防止濫用漸漸開始忽略這個東西,在C++17時你會被提醒這個東西已經不能用了。inline在我講快讀那篇文章的末尾有http://www.javashuo.com/article/p-pyddlhsy-dc.html
例以下面Luogu上兩份A+B Problem(找LZL123測試的),代碼相差無幾,可是時間差了584ms,空間差了63971kb,差距居然只有1000的數據範圍
見提交記錄https://www.luogu.org/record/show?rid=6205849和https://www.luogu.org/recordnew/show/5650350
代碼區別:
584ms:
1 #include <cstdio> 2 int x[1<<24]; 3 int main() 4 { 5 int a,b,ans=0; 6 for(int i=1;i<=1<<24;++i) 7 x[i]++,ans+=x[i]; 8 scanf("%d%d",&a,&b); 9 printf("%d",a+b); 10 return 0; 11 }
0ms:
1 #include <cstdio> 2 int x[1<<24+1000]; 3 int main() 4 { 5 int a,b,ans=0; 6 for(int i=1;i<=1<<24+1000;++i) 7 x[i]++,ans+=x[i]; 8 scanf("%d%d",&a,&b); 9 printf("%d",a+b); 10 return 0; 11 }
關於register:這種東西比較奇怪,現代CPU確實先進了很多,可是某些OJ上是有效的,例如提交記錄
可能存在干擾因素,有興趣的能夠本身去各大OJ調查一下(例如上次我在HDU加了優化以後還變慢了)
這個Random工具就是用mt19937(C++ Reference Wiki)加上Linux下的/dev/random和/dev/urandom以及某種很是高精度的時鐘實現的,速度可觀,能夠在Windows下運行
而後測試效果海星,其中的宏也已經定義好,開袋即食,您的造數據好幫手
注意若是要大量生成數據的話不要使用/dev/random,不然會比較卡(若是必須的話你能夠手動調節一下中間的Init_Rand_Buf)
注意某些函數可能會被成功inline,調試的時候若是發現這個函數「沒有調用」不要慌
持續更新QWQ
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~Random_Part~~~~~~~~~~~~~~~~~~~~~~~~ 2 3 //食用說明:在一開始須要調用Rand_Initialize 4 //若是需求量和速度要求不高的話能夠打開DO_NOT_USE_URANDOM 5 //若是在Linux的話能夠打開RAND_BUF 6 //這樣可讓隨機位取地比較好 7 //爲何要每8位取1次?由於發現若是8位取8個的話好像隨機性不優? 8 9 #include <chrono> 10 #include <random> 11 #include <algorithm> 12 13 #ifdef __gnu_linux__ 14 #define RAND_BUF 15 // #define DO_NOT_USE_URANDOM 16 #endif 17 18 #ifdef RAND_BUF 19 #include <fcntl.h> 20 #include <unistd.h> 21 #include <sys/types.h> 22 #include <sys/stat.h> 23 #endif 24 25 using namespace std; 26 27 mt19937 Generator; 28 29 #define BUF_SIZE 8193 30 31 #ifdef RAND_BUF 32 char rand_buf[BUF_SIZE], *p1 = rand_buf, *p2 = rand_buf; 33 int buf_times, rand_fd; 34 inline void Init_Rand_Buf(){ 35 buf_times ++; 36 if(buf_times < 127) p2 = (p1 = rand_buf) + read(rand_fd, rand_buf, sizeof(rand_buf)); 37 if(buf_times == 127 || p1 + BUF_SIZE != p2){ 38 if(buf_times == 127) buf_times = 0; 39 for(int i = 0; i < BUF_SIZE; i++) rand_buf[i] = Generator(); 40 p2 = (p1 = rand_buf) + BUF_SIZE; 41 } 42 } 43 inline int Rand_Bit(){ 44 if(p1 == p2) Init_Rand_Buf(); 45 return (*p1 ++ & 1); 46 } 47 #endif 48 49 inline void Rand_Initialize(){ 50 unsigned seed1 = chrono::system_clock::now().time_since_epoch().count(); 51 52 #ifdef RAND_BUF 53 #ifdef DO_NOT_USE_URANDOM 54 rand_fd = open("/dev/random", 0); 55 #else 56 rand_fd = open("/dev/urandom", 0); 57 #endif 58 Init_Rand_Buf(); 59 *(-- p2) = seed1 & 0xff; 60 *(-- p2) = (seed1 >> 8) & 0xff; 61 *(-- p2) = (seed1 >> 16) & 0xff; 62 *(-- p2) = (seed1 >> 24) & 0xff; 63 p2 ++, p2 ++, p2 ++, p2 ++; 64 seed_seq seed2(p1, p2); 65 Generator = mt19937(seed2); 66 #else 67 Generator mt19937(seed1); 68 #endif 69 } 70 71 inline unsigned Rand(){ 72 return Generator(); 73 } 74 75 inline unsigned Rand(unsigned up){ 76 return Rand() % up + 1; 77 } 78 79 inline unsigned Rand(unsigned down, unsigned up){ 80 return Rand() % (up - down + 1) + down; 81 } 82 83 inline double Rand_Float(){ 84 return (double)Rand() / (unsigned)0xffffffff; 85 } 86 87 inline unsigned long long Rand_ull(){ 88 return (unsigned long long)Rand() * Rand(); 89 } 90 91 #ifndef RAND_BUF 92 inline int Rand_Bit(){ 93 return Rand() & 1; 94 } 95 #endif 96 97 inline unsigned Rand_Number(unsigned up){ 98 return Rand() % up; 99 } 100 101 template <typename Iterator_Type> 102 inline void Random_Shuffle(Iterator_Type __first, Iterator_Type __last){ 103 random_shuffle(__first, __last, Rand_Number); 104 } 105 106 //~~~~~~~~~~~~~~~~~~~~~~~~~~~Random_Part~~~~~~~~~~~~~~~~~~~~~~~~
1 //~~~~~~~~~~~~~~~~~~~~~~~Graph_Code~~~~~~~~~~~~~~~~~~~~ 2 3 template <size_t VS, size_t ES> 4 struct __Graph_Base{ 5 int tot; 6 int beginx[VS], endx[ES], nxt[ES]; 7 __Graph_Base() : tot(1){ 8 memset(beginx, 0, sizeof(beginx)); 9 } 10 inline void clear(){ 11 tot = 1; 12 memset(beginx, 0, sizeof(beginx)); 13 } 14 }; 15 16 template <size_t VS, size_t ES> 17 struct Graph : __Graph_Base<VS, ES>{ 18 19 using __Graph_Base<VS, ES>::tot; 20 using __Graph_Base<VS, ES>::nxt; 21 using __Graph_Base<VS, ES>::beginx; 22 using __Graph_Base<VS, ES>::endx; 23 24 inline void clear(){ 25 __Graph_Base<VS, ES>::clear(); 26 } 27 inline void add_edge(const int &u, const int &v){ 28 nxt[++ tot] = beginx[u], beginx[u] = tot, endx[tot] = v; 29 nxt[++ tot] = beginx[v], beginx[v] = tot, endx[tot] = u; 30 } 31 }; 32 33 //~~~~~~~~~~~~~~~~~~~~~~~Graph_Code~~~~~~~~~~~~~~~~~~~~ 34 35 //~~~~~~~~~~~~~~~~~~~~~~~Matrix_Code~~~~~~~~~~~~~~~~~~~ 36 37 template <size_t Matrix_Size> 38 struct Matrix{ 39 int a[Matrix_Size][Matrix_Size]; 40 41 Matrix(){ 42 memset(a, 0, sizeof(a)); 43 } 44 45 Matrix(const int &tar){ 46 memset(a, 0, sizeof(a)); 47 for(register int i = 1; i <= MAX; i++) 48 a[i][i] = tar; 49 } 50 51 inline int * operator [] (const int &tar){ 52 return a[tar]; 53 } 54 55 inline const int * operator [] (const int &tar) const { 56 return a[tar]; 57 } 58 59 inline Matrix operator * (const Matrix &tar) const { 60 Matrix ret; 61 for(register int i = 1; i <= MAX; i++) 62 for(register int k = 1; k <= MAX; k++) 63 for(register int j = 1; j <= MAX; j++) 64 ret[i][j] += a[i][k] * tar.a[k][j]; 65 return ret; 66 } 67 68 inline Matrix & operator *= (const Matrix &tar){ 69 return *this = (*this) * tar; 70 } 71 72 template <typename T> 73 inline Matrix operator ^ (register T tar) const { 74 Matrix ret(1), tmp = *this; 75 while(tar){ 76 if(tar & 1) ret *= tmp; 77 tmp *= tmp; 78 tar >>= 1; 79 } 80 return ret; 81 } 82 83 template <typename T> 84 inline Matrix & operator ^= (const T &tar){ 85 return *this = (*this) ^ tar; 86 } 87 88 template <typename T> 89 inline Matrix pow(register T tar, Matrix Init_Matrix) const { 90 Matrix ret(1); 91 while(tar){ 92 if(tar & 1) ret *= Init_Matrix; 93 Init_Matrix *= Init_Matrix; 94 tar >>= 1; 95 } 96 return ret; 97 } 98 99 }; 100 101 //~~~~~~~~~~~~~~~~~~~~~~~Matrix_Code~~~~~~~~~~~~~~~~~~~
關於Graph:就是有時候同一道題可能會有不少張圖,而後你要調試的話就能夠把這個東西寫開,而後爲每一種圖加一個Debug,或者是有時候須要同時維護有向圖和無向圖
關於Matrix:就不解釋了QWQ