Skip to content

Commit a4e39a1

Browse files
committed
[2200] CF1593G 括号前缀和
1 parent d6cc6bd commit a4e39a1

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

main/1500-1599/1593G.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
)
8+
9+
// github.com/EndlessCheng/codeforces-go
10+
func CF1593G(_r io.Reader, _w io.Writer) {
11+
in := bufio.NewReader(_r)
12+
out := bufio.NewWriter(_w)
13+
defer out.Flush()
14+
abs := func(x int) int {
15+
if x < 0 {
16+
return -x
17+
}
18+
return x
19+
}
20+
21+
var T, q, l, r int
22+
var s string
23+
for Fscan(in, &T); T > 0; T-- {
24+
Fscan(in, &s, &q)
25+
sum := make([]int, len(s)+1)
26+
for i, b := range s {
27+
sum[i+1] = sum[i]
28+
if b < 42 {
29+
sum[i+1] += i&1<<1 - 1
30+
}
31+
}
32+
for ; q > 0; q-- {
33+
Fscan(in, &l, &r)
34+
Fprintln(out, abs(sum[r]-sum[l-1]))
35+
}
36+
}
37+
}
38+
39+
//func main() { CF1593G(os.Stdin, os.Stdout) }

main/1500-1599/1593G_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// https://codeforces.com/problemset/problem/1593/G
9+
// https://codeforces.com/problemset/status/1593/problem/G
10+
func TestCF1593G(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
3
15+
([))[)()][]]
16+
3
17+
1 12
18+
4 9
19+
3 6
20+
))))))
21+
2
22+
2 3
23+
1 4
24+
[]
25+
1
26+
1 2
27+
outputCopy
28+
0
29+
2
30+
1
31+
0
32+
0
33+
0`
34+
testutil.AssertEqualCase(t, rawText, 0, CF1593G)
35+
}

0 commit comments

Comments
 (0)