正文
增加 86 行删除 74 行
123456789101112131415161718192021226 行未改动2930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123UPD 2025.9.3 更改为引用,`read` 需要提供参数:要输入到的变量或数组元素有 Bug 记得一定要反馈!谢谢!使用说明:1. 输入整数到变量 $x$,用 `read(x)`。2. 输入一个字符,用 `getc()` 返回值为读入的字符。3. 输入一个 C 风格字符串 `s`,也就是字符数组(`char` 数组)、`char *`,用 `readChars(s)`,需要注意的是他的下标从 $0$ 开始,如果需要下标从 $1$ 开始可以使用类似 `scanf` 的技巧使用 `readChars(s + 1)`。4. 输出一个数 $x$,用 `write(x)`。5. 输出一个字符串 $s$,如果是 `string` 用 `writeString(s, s.length())`,如果是 `char`,用 `writeString(s, strlen(s))`,下标从 $0$ 开始。PS:别问我为什么不写读入 `string` 的函数,因为要么慢要么容易越界,大家可以先读入 `char` 数组然后再用某些函数转换,或者根据题目数据范围自己写一个 `readString()`。注意事项:**最近代码有了较大更新,请仔细阅读注意事项。**再次提醒:`read` 需要提供参数:要输入到的变量或数组元素1. 这玩意儿输入的话不能和 scanf 和 cin 混用,不然会炸(手写缓冲区)。2. 输出同理,而且输出换行和空格分别用 `endline;` 和 `putc(' ')`6 行未改动char s[N];...readString(s + 1);...```错误示范:```cpp...char *s;readString(s + 1);...``````cppFILE *inputfile = stdin, *outputfile = stdout; // 当然大多数人应该不会 fopen 只会 freopen,所以这玩意儿删掉然后把下面的 inputfile 改成 stdin,outputfile 改成 stdout 也没毛病namespace IO {const int isiz = (1 << 20) + 1, osiz = (1 << 20) + 1; // 如果 MLE 可以改int len = 0;char ibuf[isiz], outbuf[osiz];char *iBegin = ibuf, *iEnd = ibuf;inline char getc() {if (iBegin == iEnd) {iBegin = ibuf;iEnd = iBegin + fread(ibuf, 1, isiz, inputfile);if (iBegin == iEnd) return EOF;}return *(iBegin++);}#define endline putc('\n')template<typename T>inline void read(T &x){char ch = getc();x = 0;bool t = 0;while (ch < '0' || ch > '9') {if (ch == EOF) return;if (ch == '-') {t = true;}ch = getc();}while (ch >= '0' && ch <= '9') {x *= 10;x += ch - '0';ch = getc();}if (t) x = -x;}void readChars(char *s) {char ch = getc();int len = 0;while (ch == ' ' || ch == '\n' || ch == '\r') {if (ch == EOF) break;else ch = getc();}while (ch != ' ' && ch != '\n' && ch != '\r' && ch != -1) {s[len++] = ch;ch = getc();}}void flush() {fwrite(outbuf, 1, len, outputfile);len = 0;}void putc(char ch) {outbuf[len++] = ch;if (len >= osiz - 1) flush(); // 冗余}template<typename T>void write(T x){if (x == 0) {putc('0');return;}if (x < 0) {putc('-');x = -x;}if (x > 9) {write(x / 10);}putc(x % 10 + '0');}template<typename T>void writeString(T s, size_t len) { // 突然发现两个代码是一样的for (int i = 0; i < len; i++) {putc(s[i]);}}}```123456789101112131415161718196 行未改动2627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135有 Bug 记得一定要反馈!谢谢!使用说明:1. 输入整数到变量 $x$,用 `read(x)`。2. 输入一个字符,用 `getc()` 返回值为读入的字符。3. 输入一个 C 风格字符串 `s`,也就是字符数组(`char` 数组)、`char *`,用 `readChars(s)`,需要注意的是他的下标从 $0$ 开始,如果需要下标从 $1$ 开始可以使用类似 `scanf` 的技巧使用 `readChars(s + 1)`。4. 输出一个数 $x$,用 `write(x)`。5. 输出一个字符串 $s$,如果是 `string` 用 `writeString(s, s.length())`,如果是 `char` 数组/指针,用 `writeString(s, strlen(s))`,下标从 $0$ 开始。6. 输入/输出字符 $c$ 用 `getc(x)`/`putc(x)`。PS:别问我为什么不写读入 `string` 的函数,因为要么慢要么容易越界,大家可以先读入 `char` 数组然后再用某些函数转换,或者根据题目数据范围自己写一个 `readString()`。注意事项:再次提醒:`read` 需要提供参数:要输入到的变量或数组元素1. 这玩意儿输入的话不能和 scanf 和 cin 混用,不然会炸(手写缓冲区)。2. 输出同理,而且输出换行和空格分别用 `endline;` 和 `putc(' ')`6 行未改动char s[N];...readChars(s + 1);...```错误示范:```cpp...char *s;readChars(s + 1);...``````cppnamespace IO {const int isiz = (1 << 20) + 1, osiz = (1 << 20) + 1;char ibuf[isiz];char *iBegin = ibuf, *iEnd = ibuf;int len = 0;char outbuf[osiz];inline char getc() {if (iBegin == iEnd) {iBegin = ibuf;iEnd = iBegin + fread(ibuf, 1, isiz, stdin);if (iBegin == iEnd) return EOF;}return *(iBegin++);}template<typename T>inline void read(T &x){char ch = getc();x = 0;bool t = 0;while (ch < '0' || ch > '9') {if (ch == EOF) return;if (ch == '-') {t = true;}ch = getc();}while (ch >= '0' && ch <= '9') {x *= 10;x += ch - '0';ch = getc();}if (t) x = -x;}void readChars(char *s) {char ch = getc();int len = 0;while (ch == ' ' || ch == '\n' || ch == '\r') {if (ch == EOF) break;else ch = getc();}while (ch != ' ' && ch != '\n' && ch != '\r' && ch != -1) {s[len++] = ch;ch = getc();}}void flush() {fwrite(outbuf, 1, len, stdout);len = 0;}void putc(char ch) {outbuf[len++] = ch;if (len >= osiz - 1) flush();}template<typename T>void write(T x){if (x == 0) {putc('0');return;}if (x < 0) {putc('-');x = -x;}if (x > 9) {write(x / 10);}putc(x % 10 + '0');}template<typename T>void writeString(T s, size_t len) {for (int i = 0; i < len; i++) {putc(s[i]);}}#define endline putc('\n')#define space putc(' ')}using IO::read;using IO::getc;using IO::readChars;using IO::flush;using IO::putc;using IO::write;using IO::writeString;```