一些日常工具集合(C++代码片段)
一些日常工具集合(C++代碼片段)
——工欲善其事,必先利其器
盡管不會(huì)松松松,但是至少維持一個(gè)比較小的常數(shù)還是比較好的
在此之前依然要保證算法的正確性以及代碼的可寫性
本文依然會(huì)持久更新,因?yàn)橐淮螌懖煌?/p>
Tools1:算法
這個(gè)的重要性就不強(qiáng)調(diào)了,輕則多$log$,重則爆$n^2$,更令人窒息者為多項(xiàng)式和非多項(xiàng)式的區(qū)別
設(shè)計(jì)一個(gè)好的算法,首先不要想著如何去用$O(n^2)$碾壓$O(n)$,而是先想如何實(shí)現(xiàn)$O(n)$才是比較好的
洛咕日報(bào)15期(霸占評論區(qū)三天2333),關(guān)于基數(shù)排序的
Tools2:IO
IO輸出要么沒有顯著優(yōu)化,要么直接從TLE優(yōu)化到AC,在另一篇博客有介紹https://www.cnblogs.com/CreeperLKF/p/8448568.html
然后下面放上一些我平時(shí)用的貼在代碼前面且具有不同功能的一些東西:
1. 短一些,只有一個(gè)小的工具包,沒有使用要求
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. 長一些,分類討論一些開關(guān),然后不是所有東西保證效率,功能多
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下可以使用調(diào)試多參數(shù)調(diào)試?Debug(arg1, arg2...)?,建議搭配dot可以直接圖論題中繪圖,可以不刪除調(diào)試代碼交到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. 只能讀非負(fù)整數(shù),然后用的是更快的讀入,但是在本地都開了文件輸入輸出
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 C++ 116. 最簡單的快讀
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 快讀由于最近改了一下,所以可能還會(huì)有一點(diǎn)Bug
Main_Guard調(diào)用方法就是在main函數(shù)開頭寫?Main_Guard main_guard;?如果需要讀入的話就在后面帶括號和文件名,輸出的話就帶兩個(gè)字符串
這樣的好處就是一定會(huì)調(diào)用析構(gòu)函數(shù)
Tools3:__builtin_
一個(gè)非常妙而且實(shí)用的工具,有些C++庫函數(shù)會(huì)調(diào)用到,不過自己去學(xué)會(huì)調(diào)用肯定會(huì)比較妙,整個(gè)Builtin家族非常大(看文檔https://gcc.gnu.org/onlinedocs/gcc/,具體可以在https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#Other-Builtins查找),這里只介紹一些常用的。下面的函數(shù)的參數(shù)以及返回值對照了gcc文檔。
二進(jìn)制相關(guān)
(注意下面函數(shù)定義在unsigned int(或int)上,如果需要使用unsigned long long(或long long)版則可以在函數(shù)名后面加上ll,例如__builtin_ffsll接受long long的參數(shù))(還有一個(gè)__builtin_clrsb看不懂什么玩意)。
CPU分支預(yù)測優(yōu)化
?long __builtin_expect (long exp, long c)?
函數(shù)的作用就是引導(dǎo)CPU在分支預(yù)測的時(shí)候選擇某個(gè)if分支執(zhí)行,以前靠這個(gè)東西直接把一道題卡到了0ms,單點(diǎn)甩Rank2接近8ms,可見有些情況下效果還是不錯(cuò)的
食用方法:exp那里填寫你希望預(yù)測的一個(gè)表達(dá)式,建議不要寫的太復(fù)雜了,c填寫你預(yù)測表達(dá)式大概率會(huì)是什么值,然后直接用在if內(nèi)即可,例如?if(__builtin_expect(a === b, 0))?
效果:如果寫的比較好的話有一些大優(yōu)化,否則會(huì)導(dǎo)致CPU頻繁罰時(shí)
例子:例如你知道某個(gè)數(shù)據(jù)結(jié)構(gòu)它的體型龐大而且你有特意地防止node指針指向NULL,但是如果你不放心的話那么可以加一個(gè)這個(gè)。例如表達(dá)式$i%100000000==1$一般會(huì)比較難成立,可以優(yōu)化,而$i%10==1$就不需要這樣優(yōu)化了
提前寫入緩存
?void __builtin_prefetch (const void *addr, ...)?
就是提前把數(shù)據(jù)取到緩存
gcc的參數(shù)表最后面還有兩個(gè)可選參數(shù),rw和locality,表示使用這個(gè)數(shù)據(jù)是讀或者寫(1是寫0是讀),以及這個(gè)數(shù)據(jù)的時(shí)間局部性。
讀或者寫就不說了
時(shí)間局部性表示這個(gè)數(shù)據(jù)在被訪問之后在一定時(shí)間內(nèi)還會(huì)不會(huì)再次訪問(而不是距離第一次訪問還有多久),決定了這個(gè)數(shù)據(jù)在緩存中的“壽命”。0表示沒有時(shí)間局部性,也就是很長時(shí)間內(nèi)不再訪問,而3表示高時(shí)間局部性,表示訪問了時(shí)候很快又會(huì)訪問。這個(gè)參數(shù)一定是個(gè)編譯時(shí)候的常量,默認(rèn)為3.
?
Tools4:細(xì)節(jié)
首先register和inline標(biāo)記應(yīng)該都會(huì)吧。register表示建議編譯器將變量放在編譯器中,C++11及以上為了防止濫用漸漸開始忽略這個(gè)東西,在C++17時(shí)你會(huì)被提醒這個(gè)東西已經(jīng)不能用了。inline在我講快讀那篇文章的末尾有https://www.cnblogs.com/CreeperLKF/p/8448568.html
例如下面Luogu上兩份A+B Problem(找LZL123測試的),代碼相差無幾,但是時(shí)間差了584ms,空間差了63971kb,差距竟然只有1000的數(shù)據(jù)范圍
見提交記錄https://www.luogu.org/record/show?rid=6205849和https://www.luogu.org/recordnew/show/5650350
代碼區(qū)別:
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 }?關(guān)于register:這種東西比較奇怪,現(xiàn)代CPU確實(shí)先進(jìn)了不少,但是某些OJ上是有效的,例如提交記錄
沒有優(yōu)化
register優(yōu)化
O2優(yōu)化
O2+register
可能存在干擾因素,有興趣的可以自己去各大OJ調(diào)查一下(例如上次我在HDU加了優(yōu)化之后還變慢了)
Tools5:強(qiáng)力Random工具
這個(gè)Random工具就是用mt19937(C++ Reference?Wiki)加上Linux下的/dev/random和/dev/urandom以及某種非常高精度的時(shí)鐘實(shí)現(xiàn)的,速度可觀,可以在Windows下運(yùn)行
然后測試效果海星,其中的宏也已經(jīng)定義好,開袋即食,您的造數(shù)據(jù)好幫手
注意如果要大量生成數(shù)據(jù)的話不要使用/dev/random,否則會(huì)比較卡(如果必須的話你可以手動(dòng)調(diào)節(jié)一下中間的Init_Rand_Buf)
注意某些函數(shù)可能會(huì)被成功inline,調(diào)試的時(shí)候如果發(fā)現(xiàn)這個(gè)函數(shù)“沒有調(diào)用”不要慌
持續(xù)更新QWQ
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~Random_Part~~~~~~~~~~~~~~~~~~~~~~~~ 2 3 //食用說明:在一開始需要調(diào)用Rand_Initialize 4 //如果需求量和速度要求不高的話可以打開DO_NOT_USE_URANDOM 5 //如果在Linux的話可以打開RAND_BUF 6 //這樣可以讓隨機(jī)位取地比較好 7 //為什么要每8位取1次?因?yàn)榘l(fā)現(xiàn)如果8位取8個(gè)的話好像隨機(jī)性不優(yōu)? 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~~~~~~~~~~~~~~~~~~~~~~~~?
?Tools6:其他的東西
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~~~~~~~~~~~~~~~~~~~?
關(guān)于Graph:就是有時(shí)候同一道題可能會(huì)有很多張圖,然后你要調(diào)試的話就可以把這個(gè)東西寫開,然后為每一種圖加一個(gè)Debug,或者是有時(shí)候需要同時(shí)維護(hù)有向圖和無向圖
關(guān)于Matrix:就不解釋了QWQ
轉(zhuǎn)載于:https://www.cnblogs.com/CreeperLKF/p/9314916.html
總結(jié)
以上是生活随笔為你收集整理的一些日常工具集合(C++代码片段)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java实例---计算器实例
- 下一篇: 下载旧版本的JDK