ac-library-rb 0.5.2 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -0
  3. data/README.md +9 -2
  4. data/ac-library-rb.gemspec +1 -0
  5. data/bin/lock_lib.rb +5 -1
  6. data/document_en/dsu.md +2 -2
  7. data/document_en/max_flow.md +1 -1
  8. data/document_en/min_cost_flow.md +1 -1
  9. data/document_en/segtree.md +22 -5
  10. data/document_en/two_sat.md +1 -1
  11. data/document_ja/dsu.md +1 -3
  12. data/document_ja/lazy_segtree.md +9 -7
  13. data/document_ja/max_flow.md +1 -1
  14. data/document_ja/min_cost_flow.md +1 -1
  15. data/document_ja/scc.md +4 -3
  16. data/document_ja/segtree.md +42 -9
  17. data/document_ja/two_sat.md +1 -1
  18. data/lib/ac-library-rb/version.rb +1 -1
  19. data/lib/convolution.rb +21 -0
  20. data/lib/core_ext/all.rb +11 -0
  21. data/lib/crt.rb +3 -1
  22. data/lib/dsu.rb +11 -8
  23. data/lib/fenwick_tree.rb +22 -8
  24. data/lib/floor_sum.rb +33 -10
  25. data/lib/lazy_segtree.rb +29 -3
  26. data/lib/max_flow.rb +10 -4
  27. data/lib/min_cost_flow.rb +13 -7
  28. data/lib/scc.rb +21 -13
  29. data/lib/segtree.rb +28 -22
  30. data/lib/two_sat.rb +7 -5
  31. data/lib_helpers/ac-library-rb.rb +24 -0
  32. data/lib_lock/ac-library-rb/convolution.rb +21 -0
  33. data/lib_lock/ac-library-rb/core_ext/all.rb +11 -0
  34. data/lib_lock/ac-library-rb/core_ext/modint.rb +3 -3
  35. data/lib_lock/ac-library-rb/crt.rb +3 -1
  36. data/lib_lock/ac-library-rb/dsu.rb +11 -8
  37. data/lib_lock/ac-library-rb/fenwick_tree.rb +22 -8
  38. data/lib_lock/ac-library-rb/floor_sum.rb +33 -10
  39. data/lib_lock/ac-library-rb/lazy_segtree.rb +29 -3
  40. data/lib_lock/ac-library-rb/max_flow.rb +10 -4
  41. data/lib_lock/ac-library-rb/min_cost_flow.rb +13 -7
  42. data/lib_lock/ac-library-rb/scc.rb +21 -13
  43. data/lib_lock/ac-library-rb/segtree.rb +28 -22
  44. data/lib_lock/ac-library-rb/two_sat.rb +7 -5
  45. metadata +20 -4
  46. data/lib_lock/ac-library-rb.rb +0 -22
@@ -3,14 +3,10 @@ module AcLibraryRb
3
3
  class Segtree
4
4
  attr_reader :d, :op, :n, :leaf_size, :log
5
5
 
6
- # new(e){ |x, y| }
7
6
  # new(v, e){ |x, y| }
8
7
  # new(v, op, e)
9
- def initialize(a0, a1 = nil, a2 = nil, &block)
10
- if a1.nil?
11
- @e, @op = a0, proc(&block)
12
- v = []
13
- elsif a2.nil?
8
+ def initialize(a0, a1, a2 = nil, &block)
9
+ if a2.nil?
14
10
  @e, @op = a1, proc(&block)
15
11
  v = (a0.is_a?(Array) ? a0 : [@e] * a0)
16
12
  else
@@ -38,7 +34,18 @@ module AcLibraryRb
38
34
  end
39
35
  alias [] get
40
36
 
41
- def prod(l, r)
37
+ def prod(l, r = nil)
38
+ if r.nil? # if 1st argument l is Range
39
+ if r = l.end
40
+ r += @n if r < 0
41
+ r += 1 unless l.exclude_end?
42
+ else
43
+ r = @n
44
+ end
45
+ l = l.begin
46
+ l += @n if l < 0
47
+ end
48
+
42
49
  return @e if l == r
43
50
 
44
51
  sml = @e
@@ -128,21 +135,20 @@ module AcLibraryRb
128
135
  @d[k] = @op.call(@d[2 * k], @d[2 * k + 1])
129
136
  end
130
137
 
131
- def inspect
132
- t = 0
133
- res = "SegmentTree @e = #{@e}, @n = #{@n}, @leaf_size = #{@leaf_size} @op = #{@op}\n "
134
- a = @d[1, @d.size - 1]
135
- a.each_with_index do |e, i|
136
- res << e.to_s << ' '
137
- if t == i && i < @leaf_size
138
- res << "\n "
139
- t = t * 2 + 2
140
- end
141
- end
142
- res
143
- end
138
+ # def inspect # for debug
139
+ # t = 0
140
+ # res = "Segtree @e = #{@e}, @n = #{@n}, @leaf_size = #{@leaf_size} @op = #{@op}\n "
141
+ # a = @d[1, @d.size - 1]
142
+ # a.each_with_index do |e, i|
143
+ # res << e.to_s << ' '
144
+ # if t == i && i < @leaf_size
145
+ # res << "\n "
146
+ # t = t * 2 + 2
147
+ # end
148
+ # end
149
+ # res
150
+ # end
144
151
  end
145
152
 
146
- SegTree = Segtree
147
- SegmentTree = Segtree
153
+ SegTree = Segtree
148
154
  end
@@ -4,23 +4,25 @@ module AcLibraryRb
4
4
  # TwoSAT
5
5
  # Reference: https://github.com/atcoder/ac-library/blob/master/atcoder/twosat.hpp
6
6
  class TwoSAT
7
- def initialize(n = 0)
7
+ def initialize(n)
8
8
  @n = n
9
9
  @answer = Array.new(n)
10
- @scc = SCCGraph.new(2 * n)
10
+ @scc = SCC.new(2 * n)
11
11
  end
12
12
 
13
13
  attr_reader :answer
14
14
 
15
15
  def add_clause(i, f, j, g)
16
- raise RangeError unless (0...@n).cover?(i) && (0...@n).cover?(j)
16
+ unless 0 <= i && i < @n and 0 <= j && j < @n
17
+ raise ArgumentError.new("i:#{i} and j:#{j} must be in (0...#{@n})")
18
+ end
17
19
 
18
20
  @scc.add_edge(2 * i + (f ? 0 : 1), 2 * j + (g ? 1 : 0))
19
21
  @scc.add_edge(2 * j + (g ? 0 : 1), 2 * i + (f ? 1 : 0))
20
22
  nil
21
23
  end
22
24
 
23
- def satisfiable
25
+ def satisfiable?
24
26
  id = @scc.send(:scc_ids)[1]
25
27
  @n.times do |i|
26
28
  return false if id[2 * i] == id[2 * i + 1]
@@ -29,7 +31,7 @@ module AcLibraryRb
29
31
  end
30
32
  true
31
33
  end
32
- alias satisfiable? satisfiable
34
+ alias satisfiable satisfiable?
33
35
  end
34
36
 
35
37
  TwoSat = TwoSAT
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ac-library-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - universato
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-18 00:00:00.000000000 Z
11
+ date: 2021-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: |-
42
56
  ac-library-rb is a ruby port of AtCoder Library (ACL).
43
57
  DSU(UnionFind), FenwickTree, PriorityQueue, Segtree, SCC, 2-SAT, suffix_array, lcp_array, z_algorithm, crt, inv_mod, floor_sum, max_flow, min_cost_flow......
@@ -90,6 +104,7 @@ files:
90
104
  - document_ja/two_sat.md
91
105
  - lib/ac-library-rb/version.rb
92
106
  - lib/convolution.rb
107
+ - lib/core_ext/all.rb
93
108
  - lib/core_ext/modint.rb
94
109
  - lib/crt.rb
95
110
  - lib/dsu.rb
@@ -108,9 +123,10 @@ files:
108
123
  - lib/suffix_array.rb
109
124
  - lib/two_sat.rb
110
125
  - lib/z_algorithm.rb
126
+ - lib_helpers/ac-library-rb.rb
111
127
  - lib_helpers/ac-library-rb/all.rb
112
- - lib_lock/ac-library-rb.rb
113
128
  - lib_lock/ac-library-rb/convolution.rb
129
+ - lib_lock/ac-library-rb/core_ext/all.rb
114
130
  - lib_lock/ac-library-rb/core_ext/modint.rb
115
131
  - lib_lock/ac-library-rb/crt.rb
116
132
  - lib_lock/ac-library-rb/dsu.rb
@@ -151,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
167
  - !ruby/object:Gem::Version
152
168
  version: '0'
153
169
  requirements: []
154
- rubygems_version: 3.2.15
170
+ rubygems_version: 3.2.22
155
171
  signing_key:
156
172
  specification_version: 4
157
173
  summary: ac-library-rb is a ruby port of AtCoder Library (ACL).
@@ -1,22 +0,0 @@
1
- module AcLibraryRb
2
- end
3
- include AcLibraryRb
4
-
5
- require_relative './ac-library-rb/convolution'
6
- require_relative './ac-library-rb/crt'
7
- require_relative './ac-library-rb/dsu'
8
- require_relative './ac-library-rb/fenwick_tree'
9
- require_relative './ac-library-rb/floor_sum'
10
- require_relative './ac-library-rb/inv_mod'
11
- require_relative './ac-library-rb/lazy_segtree'
12
- require_relative './ac-library-rb/lcp_array'
13
- require_relative './ac-library-rb/max_flow'
14
- require_relative './ac-library-rb/min_cost_flow'
15
- require_relative './ac-library-rb/modint'
16
- require_relative './ac-library-rb/pow_mod'
17
- require_relative './ac-library-rb/priority_queue'
18
- require_relative './ac-library-rb/scc'
19
- require_relative './ac-library-rb/segtree'
20
- require_relative './ac-library-rb/suffix_array'
21
- require_relative './ac-library-rb/two_sat'
22
- require_relative './ac-library-rb/z_algorithm'