# 增强版输入输出挂
# 说明
原文链接:输入输出挂备份 在输入挂的基础上做了平滑使用的封装
# 使用
如同cin
,cout
一样平滑输入输出
int a;
long long b;
char c;
string d;
char str[100];
fin >> a >> b >> c >> d >> str << a << b << c << d << str;(wwww)
1
2
3
4
5
6
2
3
4
5
6
# Tips
WARNING
若不存在宏定义ONLINE_JUDGE
,输入输入将使用cin
# 代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <list>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <complex>
using ll = long long;
/*
* 输入挂
* 场场AK buff
*/
namespace IO {
using namespace std;
class endln {
};
class iofstream {
private:
int idx;
bool eof;
char buf[100005], *ps, *pe;
char bufout[100005], outtmp[50], *pout, *pend;
inline void rnext() {
if (++ps == pe)
pe = (ps = buf) + fread(buf, sizeof(char), sizeof(buf) / sizeof(char), stdin), eof = true;
//eof = ps != pe;
}
inline void write() {
fwrite(bufout, sizeof(char), pout - bufout, stdout);
pout = bufout;
}
public:
iofstream() : idx(-1), eof(true) {
pe = (ps = buf) + 1;
pend = (pout = bufout) + 100005;
}
~iofstream() {
write();
}
template<class T>
inline bool fin(T &ans) {
#ifdef ONLINE_JUDGE
ans = 0;
T f = 1;
if (ps == pe) {
return eof = false;
}
do {
rnext();
if ('-' == *ps) f = -1;
} while (!isdigit(*ps) && ps != pe);
if (ps == pe) {
return eof = false;
}
do {
ans = (ans << 1) + (ans << 3) + *ps - 48;
rnext();
} while (isdigit(*ps) && ps != pe);
ans *= f;
#else
cin >> ans;
#endif
return true;
}
template<class T>
inline bool fdb(T &ans) {
#ifdef ONLINE_JUDGE
ans = 0;
T f = 1;
if (ps == pe) return false;//EOF
do {
rnext();
if ('-' == *ps) f = -1;
} while (!isdigit(*ps) && ps != pe);
if (ps == pe) return false;//EOF
do {
ans = ans * 10 + *ps - 48;
rnext();
} while (isdigit(*ps) && ps != pe);
ans *= f;
if (*ps == '.') {
rnext();
T small = 0;
do {
small = small * 10 + *ps - 48;
rnext();
} while (isdigit(*ps) && ps != pe);
while (small >= 1) {
small /= 10;
}
ans += small;
}
#else
cin >> ans;
#endif
return true;
}
/*
* 输出挂
* 超强 超快
*/
inline bool out_char(const char c) {
#ifdef ONLINE_JUDGE
*(pout++) = c;
if (pout == pend) write();
#else
cout << c;
#endif
return true;
}
inline bool out_str(const char *s) {
#ifdef ONLINE_JUDGE
while (*s) {
*(pout++) = *(s++);
if (pout == pend) write();
}
#else
cout << s;
#endif
return true;
}
template<class T>
inline bool out_double(T x, int idx) {
char str[50];
string format = "%";
if (~idx) {
format += '.';
format += (char) (idx + '0');
}
format += "f";
sprintf(str, format.c_str(), x);
out_str(str);
return true;
}
template<class T>
inline bool out_int(T x) {
#ifdef ONLINE_JUDGE
if (!x) {
out_char('0');
return true;
}
if (x < 0) x = -x, out_char('-');
int len = 0;
while (x) {
outtmp[len++] = x % 10 + 48;
x /= 10;
}
outtmp[len] = 0;
for (int i = 0, j = len - 1; i < j; ++i, --j) swap(outtmp[i], outtmp[j]);
out_str(outtmp);
#else
cout << x;
#endif
return true;
}
inline iofstream &operator<<(const double &x) {
out_double(x, idx);
return *this;
}
inline iofstream &operator<<(const int &x) {
out_int(x);
return *this;
}
inline iofstream &operator<<(const unsigned long long &x) {
out_int(x);
return *this;
}
inline iofstream &operator<<(const unsigned &x) {
out_int(x);
return *this;
}
inline iofstream &operator<<(const long &x) {
out_int(x);
return *this;
}
inline iofstream &operator<<(const ll &x) {
out_int(x);
return *this;
}
inline iofstream &operator<<(const endln &x) {
out_char('\n');
return *this;
}
inline iofstream &operator<<(const char *x) {
out_str(x);
return *this;
}
inline iofstream &operator<<(const string &x) {
out_str(x.c_str());
return *this;
}
inline iofstream &operator<<(const char &x) {
out_char(x);
return *this;
}
inline bool setw(int x) {
if (x >= 0) {
idx = x;
return true;
}
return false;
}
inline iofstream &operator>>(int &x) {
if (!fin(x) && !x)eof = false;
return *this;
}
inline iofstream &operator>>(ll &x) {
if (!fin(x) && !x)eof = false;
return *this;
}
inline iofstream &operator>>(double &x) {
if (!fdb(x) && x == 0.0)eof = false;
return *this;
}
inline iofstream &operator>>(float &x) {
if (!fdb(x) && x == 0.0)eof = false;
return *this;
}
inline iofstream &operator>>(unsigned &x) {
if (!fin(x) && !x)eof = false;
return *this;
}
inline iofstream &operator>>(unsigned long long &x) {
if (!fin(x) && !x)eof = false;
return *this;
}
inline explicit operator bool() {
return eof;
}
inline char getchar() {
#ifdef ONLINE_JUDGE
if (ps == pe) {
return eof = false;
}
rnext();
if (ps + 1 == pe)
eof = false;
return *ps;
#else
return std::getchar();
#endif
}
inline iofstream &operator>>(char *str) {
#ifdef ONLINE_JUDGE
if (ps == pe) {
eof = false;//EOF
return *this;
}
do {
rnext();
} while (isspace(*ps) && iscntrl(*ps) && ps != pe);
if (ps == pe) {
eof = false;//EOF
return *this;
}
do {
*str = *ps;
++str;
rnext();
} while (!(isspace(*ps) || iscntrl(*ps)) && ps != pe);
*str = '\0';
return *this;
#else
cin >> str;
return *this;
#endif
}
inline iofstream &operator>>(string &str) {
#ifdef ONLINE_JUDGE
str.clear();
if (ps == pe) {
eof = false;//EOF
return *this;
}
do {
rnext();
} while (isspace(*ps) && iscntrl(*ps) && ps != pe);
if (ps == pe) {
eof = false;//EOF
return *this;
}
do {
str += *ps;
rnext();
} while (!(isspace(*ps) || iscntrl(*ps)) && ps != pe);
return *this;
#else
cin >> str;
return *this;
#endif
}
};
static iofstream fin;
static endln ln;
}
using IO::fin;
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
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