Skip to content

Commit 32697c2

Browse files
committedJun 21, 2022
feat: add golang solution to lc problem: No.0715
No.0715.Range Module
1 parent 77d2ea0 commit 32697c2

File tree

3 files changed

+313
-0
lines changed

3 files changed

+313
-0
lines changed
 

‎solution/0700-0799/0715.Range Module/README.md

+106
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,112 @@ public:
396396
*/
397397
```
398398
399+
### **Go**
400+
401+
```go
402+
const N int = 1e9
403+
404+
type node struct {
405+
lch *node
406+
rch *node
407+
added bool
408+
lazy int
409+
}
410+
411+
type segmentTree struct {
412+
root *node
413+
}
414+
415+
func newSegmentTree() *segmentTree {
416+
return &segmentTree{
417+
root: new(node),
418+
}
419+
}
420+
421+
func (t *segmentTree) update(n *node, l, r, i, j, x int) {
422+
if l >= i && r <= j {
423+
n.added = x == 1
424+
n.lazy = x
425+
return
426+
}
427+
t.pushdown(n)
428+
m := int(uint(l+r) >> 1)
429+
if i <= m {
430+
t.update(n.lch, l, m, i, j, x)
431+
}
432+
if j > m {
433+
t.update(n.rch, m+1, r, i, j, x)
434+
}
435+
t.pushup(n)
436+
}
437+
438+
func (t *segmentTree) query(n *node, l, r, i, j int) bool {
439+
if l >= i && r <= j {
440+
return n.added
441+
}
442+
t.pushdown(n)
443+
v := true
444+
m := int(uint(l+r) >> 1)
445+
if i <= m {
446+
v = v && t.query(n.lch, l, m, i, j)
447+
}
448+
if j > m {
449+
v = v && t.query(n.rch, m+1, r, i, j)
450+
}
451+
return v
452+
}
453+
454+
func (t *segmentTree) pushup(n *node) {
455+
n.added = n.lch.added && n.rch.added
456+
}
457+
458+
func (t *segmentTree) pushdown(n *node) {
459+
if n.lch == nil {
460+
n.lch = new(node)
461+
}
462+
if n.rch == nil {
463+
n.rch = new(node)
464+
}
465+
if n.lazy != 0 {
466+
n.lch.added = n.lazy == 1
467+
n.rch.added = n.lazy == 1
468+
n.lch.lazy = n.lazy
469+
n.rch.lazy = n.lazy
470+
n.lazy = 0
471+
}
472+
}
473+
474+
type RangeModule struct {
475+
t *segmentTree
476+
}
477+
478+
func Constructor() RangeModule {
479+
return RangeModule{
480+
t: newSegmentTree(),
481+
}
482+
}
483+
484+
func (this *RangeModule) AddRange(left int, right int) {
485+
this.t.update(this.t.root, 1, N, left, right-1, 1)
486+
}
487+
488+
func (this *RangeModule) QueryRange(left int, right int) bool {
489+
return this.t.query(this.t.root, 1, N, left, right-1)
490+
}
491+
492+
func (this *RangeModule) RemoveRange(left int, right int) {
493+
this.t.update(this.t.root, 1, N, left, right-1, -1)
494+
}
495+
496+
/**
497+
* Your RangeModule object will be instantiated and called as such:
498+
* obj := Constructor();
499+
* obj.AddRange(left,right);
500+
* param_2 := obj.QueryRange(left,right);
501+
* obj.RemoveRange(left,right);
502+
*/
503+
```
504+
399505
### **...**
400506

401507
```

‎solution/0700-0799/0715.Range Module/README_EN.md

+106
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,112 @@ public:
379379
*/
380380
```
381381
382+
### **Go**
383+
384+
```go
385+
const N int = 1e9
386+
387+
type node struct {
388+
lch *node
389+
rch *node
390+
added bool
391+
lazy int
392+
}
393+
394+
type segmentTree struct {
395+
root *node
396+
}
397+
398+
func newSegmentTree() *segmentTree {
399+
return &segmentTree{
400+
root: new(node),
401+
}
402+
}
403+
404+
func (t *segmentTree) update(n *node, l, r, i, j, x int) {
405+
if l >= i && r <= j {
406+
n.added = x == 1
407+
n.lazy = x
408+
return
409+
}
410+
t.pushdown(n)
411+
m := int(uint(l+r) >> 1)
412+
if i <= m {
413+
t.update(n.lch, l, m, i, j, x)
414+
}
415+
if j > m {
416+
t.update(n.rch, m+1, r, i, j, x)
417+
}
418+
t.pushup(n)
419+
}
420+
421+
func (t *segmentTree) query(n *node, l, r, i, j int) bool {
422+
if l >= i && r <= j {
423+
return n.added
424+
}
425+
t.pushdown(n)
426+
v := true
427+
m := int(uint(l+r) >> 1)
428+
if i <= m {
429+
v = v && t.query(n.lch, l, m, i, j)
430+
}
431+
if j > m {
432+
v = v && t.query(n.rch, m+1, r, i, j)
433+
}
434+
return v
435+
}
436+
437+
func (t *segmentTree) pushup(n *node) {
438+
n.added = n.lch.added && n.rch.added
439+
}
440+
441+
func (t *segmentTree) pushdown(n *node) {
442+
if n.lch == nil {
443+
n.lch = new(node)
444+
}
445+
if n.rch == nil {
446+
n.rch = new(node)
447+
}
448+
if n.lazy != 0 {
449+
n.lch.added = n.lazy == 1
450+
n.rch.added = n.lazy == 1
451+
n.lch.lazy = n.lazy
452+
n.rch.lazy = n.lazy
453+
n.lazy = 0
454+
}
455+
}
456+
457+
type RangeModule struct {
458+
t *segmentTree
459+
}
460+
461+
func Constructor() RangeModule {
462+
return RangeModule{
463+
t: newSegmentTree(),
464+
}
465+
}
466+
467+
func (this *RangeModule) AddRange(left int, right int) {
468+
this.t.update(this.t.root, 1, N, left, right-1, 1)
469+
}
470+
471+
func (this *RangeModule) QueryRange(left int, right int) bool {
472+
return this.t.query(this.t.root, 1, N, left, right-1)
473+
}
474+
475+
func (this *RangeModule) RemoveRange(left int, right int) {
476+
this.t.update(this.t.root, 1, N, left, right-1, -1)
477+
}
478+
479+
/**
480+
* Your RangeModule object will be instantiated and called as such:
481+
* obj := Constructor();
482+
* obj.AddRange(left,right);
483+
* param_2 := obj.QueryRange(left,right);
484+
* obj.RemoveRange(left,right);
485+
*/
486+
```
487+
382488
### **...**
383489

384490
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const N int = 1e9
2+
3+
type node struct {
4+
lch *node
5+
rch *node
6+
added bool
7+
lazy int
8+
}
9+
10+
type segmentTree struct {
11+
root *node
12+
}
13+
14+
func newSegmentTree() *segmentTree {
15+
return &segmentTree{
16+
root: new(node),
17+
}
18+
}
19+
20+
func (t *segmentTree) update(n *node, l, r, i, j, x int) {
21+
if l >= i && r <= j {
22+
n.added = x == 1
23+
n.lazy = x
24+
return
25+
}
26+
t.pushdown(n)
27+
m := int(uint(l+r) >> 1)
28+
if i <= m {
29+
t.update(n.lch, l, m, i, j, x)
30+
}
31+
if j > m {
32+
t.update(n.rch, m+1, r, i, j, x)
33+
}
34+
t.pushup(n)
35+
}
36+
37+
func (t *segmentTree) query(n *node, l, r, i, j int) bool {
38+
if l >= i && r <= j {
39+
return n.added
40+
}
41+
t.pushdown(n)
42+
v := true
43+
m := int(uint(l+r) >> 1)
44+
if i <= m {
45+
v = v && t.query(n.lch, l, m, i, j)
46+
}
47+
if j > m {
48+
v = v && t.query(n.rch, m+1, r, i, j)
49+
}
50+
return v
51+
}
52+
53+
func (t *segmentTree) pushup(n *node) {
54+
n.added = n.lch.added && n.rch.added
55+
}
56+
57+
func (t *segmentTree) pushdown(n *node) {
58+
if n.lch == nil {
59+
n.lch = new(node)
60+
}
61+
if n.rch == nil {
62+
n.rch = new(node)
63+
}
64+
if n.lazy != 0 {
65+
n.lch.added = n.lazy == 1
66+
n.rch.added = n.lazy == 1
67+
n.lch.lazy = n.lazy
68+
n.rch.lazy = n.lazy
69+
n.lazy = 0
70+
}
71+
}
72+
73+
type RangeModule struct {
74+
t *segmentTree
75+
}
76+
77+
func Constructor() RangeModule {
78+
return RangeModule{
79+
t: newSegmentTree(),
80+
}
81+
}
82+
83+
func (this *RangeModule) AddRange(left int, right int) {
84+
this.t.update(this.t.root, 1, N, left, right-1, 1)
85+
}
86+
87+
func (this *RangeModule) QueryRange(left int, right int) bool {
88+
return this.t.query(this.t.root, 1, N, left, right-1)
89+
}
90+
91+
func (this *RangeModule) RemoveRange(left int, right int) {
92+
this.t.update(this.t.root, 1, N, left, right-1, -1)
93+
}
94+
95+
/**
96+
* Your RangeModule object will be instantiated and called as such:
97+
* obj := Constructor();
98+
* obj.AddRange(left,right);
99+
* param_2 := obj.QueryRange(left,right);
100+
* obj.RemoveRange(left,right);
101+
*/

0 commit comments

Comments
 (0)