-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotdebug.go
151 lines (138 loc) · 2.98 KB
/
notdebug.go
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
//go:build notdebug
package main
import (
"runtime/debug"
"syscall"
)
type IO struct {
rBuffer []byte
rBufferIndex int
rBufferEnd int
wBuffer []byte
wBufferSize uint
}
const ioBufferSize = 1 << 15
func (io *IO) nextByte() byte {
io.rBufferIndex++
if io.rBufferIndex == io.rBufferEnd {
io.rBufferEnd, _ = syscall.Read(syscall.Stdin, io.rBuffer)
io.rBufferIndex = 0
}
return io.rBuffer[io.rBufferIndex]
}
func main() {
io := IO{
rBufferIndex: -1,
rBufferEnd: 0,
rBuffer: make([]byte, ioBufferSize),
wBuffer: make([]byte, ioBufferSize*2),
}
if optimizationLevel == OptimizeTimeLimit {
debug.SetGCPercent(-1)
} else if optimizationLevel == OptimizeMemoryLimit {
debug.SetGCPercent(1)
}
solve(&io)
io.Flush()
}
func (io *IO) writeLargeBytes(b []byte) {
if len(b) > ioBufferSize {
syscall.Write(syscall.Stdout, b)
return
}
for i := 0; i < len(b); i++ {
io.wBuffer[io.wBufferSize] = b[i]
io.wBufferSize++
}
if io.wBufferSize > ioBufferSize {
syscall.Write(syscall.Stdout, io.wBuffer[:io.wBufferSize])
io.wBufferSize = 0
}
}
func (io *IO) writeSmallBytes(b []byte) {
for i := 0; i < len(b); i++ {
io.wBuffer[io.wBufferSize] = b[i]
io.wBufferSize++
}
if io.wBufferSize > ioBufferSize {
syscall.Write(syscall.Stdout, io.wBuffer[:io.wBufferSize])
io.wBufferSize = 0
}
}
func (io *IO) PrintChar(b byte) {
io.wBuffer[io.wBufferSize] = b
io.wBufferSize++
if io.wBufferSize > ioBufferSize {
syscall.Write(syscall.Stdout, io.wBuffer[:io.wBufferSize])
io.wBufferSize = 0
}
}
func (io *IO) Flush() {
syscall.Write(syscall.Stdout, io.wBuffer[:io.wBufferSize])
io.wBufferSize = 0
}
func (io *IO) PrintNewLine() {
io.wBuffer[io.wBufferSize] = '\n'
io.wBufferSize++
if io.wBufferSize > ioBufferSize {
syscall.Write(syscall.Stdout, io.wBuffer[:io.wBufferSize])
io.wBufferSize = 0
}
}
func (io *IO) Print(x any) {
switch x := x.(type) {
case string:
io.writeLargeBytes([]byte(x))
case []byte:
io.writeLargeBytes(x)
case int:
io.writeSmallBytes(Itoa(x))
case int8:
io.writeSmallBytes(Itoa(x))
case int16:
io.writeSmallBytes(Itoa(x))
case int32:
io.writeSmallBytes(Itoa(x))
case int64:
io.writeSmallBytes(Itoa(x))
case uint:
io.writeSmallBytes(Uitoa(x))
case uint8:
io.writeSmallBytes(Uitoa(x))
case uint16:
io.writeSmallBytes(Uitoa(x))
case uint32:
io.writeSmallBytes(Uitoa(x))
case uint64:
io.writeSmallBytes(Uitoa(x))
}
}
func (io *IO) Println(x any) {
switch x := x.(type) {
case string:
io.writeLargeBytes([]byte(x))
case []byte:
io.writeLargeBytes(x)
case int:
io.writeSmallBytes(Itoa(x))
case int8:
io.writeSmallBytes(Itoa(x))
case int16:
io.writeSmallBytes(Itoa(x))
case int32:
io.writeSmallBytes(Itoa(x))
case int64:
io.writeSmallBytes(Itoa(x))
case uint:
io.writeSmallBytes(Uitoa(x))
case uint8:
io.writeSmallBytes(Uitoa(x))
case uint16:
io.writeSmallBytes(Uitoa(x))
case uint32:
io.writeSmallBytes(Uitoa(x))
case uint64:
io.writeSmallBytes(Uitoa(x))
}
io.PrintNewLine()
}