Skip to content

Commit 091a18a

Browse files
committed
feat: add weekly contest 493 and biweekly contest 178
1 parent a673a50 commit 091a18a

60 files changed

Lines changed: 4297 additions & 3 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

solution/1600-1699/1622.Fancy Sequence/README.md

Lines changed: 332 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,338 @@ public:
503503
*/
504504
```
505505
506+
#### Go
507+
508+
```go
509+
const MOD int64 = 1e9 + 7
510+
511+
type Node struct {
512+
left *Node
513+
right *Node
514+
l int
515+
r int
516+
mid int
517+
v int64
518+
add int64
519+
mul int64
520+
}
521+
522+
func newNode(l, r int) *Node {
523+
return &Node{
524+
l: l,
525+
r: r,
526+
mid: (l + r) >> 1,
527+
mul: 1,
528+
}
529+
}
530+
531+
type SegmentTree struct {
532+
root *Node
533+
}
534+
535+
func newSegmentTree() *SegmentTree {
536+
return &SegmentTree{
537+
root: newNode(1, 100001),
538+
}
539+
}
540+
541+
func (t *SegmentTree) modifyAdd(l, r int, inc int64) {
542+
t.modifyAddNode(l, r, inc, t.root)
543+
}
544+
545+
func (t *SegmentTree) modifyAddNode(l, r int, inc int64, node *Node) {
546+
if l > r {
547+
return
548+
}
549+
550+
if node.l >= l && node.r <= r {
551+
node.v = (node.v + int64(node.r-node.l+1)*inc) % MOD
552+
node.add = (node.add + inc) % MOD
553+
return
554+
}
555+
556+
t.pushdown(node)
557+
558+
if l <= node.mid {
559+
t.modifyAddNode(l, r, inc, node.left)
560+
}
561+
562+
if r > node.mid {
563+
t.modifyAddNode(l, r, inc, node.right)
564+
}
565+
566+
t.pushup(node)
567+
}
568+
569+
func (t *SegmentTree) modifyMul(l, r int, m int64) {
570+
t.modifyMulNode(l, r, m, t.root)
571+
}
572+
573+
func (t *SegmentTree) modifyMulNode(l, r int, m int64, node *Node) {
574+
if l > r {
575+
return
576+
}
577+
578+
if node.l >= l && node.r <= r {
579+
node.v = node.v * m % MOD
580+
node.add = node.add * m % MOD
581+
node.mul = node.mul * m % MOD
582+
return
583+
}
584+
585+
t.pushdown(node)
586+
587+
if l <= node.mid {
588+
t.modifyMulNode(l, r, m, node.left)
589+
}
590+
591+
if r > node.mid {
592+
t.modifyMulNode(l, r, m, node.right)
593+
}
594+
595+
t.pushup(node)
596+
}
597+
598+
func (t *SegmentTree) query(l, r int) int {
599+
return int(t.queryNode(l, r, t.root))
600+
}
601+
602+
func (t *SegmentTree) queryNode(l, r int, node *Node) int64 {
603+
if l > r {
604+
return 0
605+
}
606+
607+
if node.l >= l && node.r <= r {
608+
return node.v
609+
}
610+
611+
t.pushdown(node)
612+
613+
var v int64
614+
615+
if l <= node.mid {
616+
v = (v + t.queryNode(l, r, node.left)) % MOD
617+
}
618+
619+
if r > node.mid {
620+
v = (v + t.queryNode(l, r, node.right)) % MOD
621+
}
622+
623+
return v
624+
}
625+
626+
func (t *SegmentTree) pushup(node *Node) {
627+
node.v = (node.left.v + node.right.v) % MOD
628+
}
629+
630+
func (t *SegmentTree) pushdown(node *Node) {
631+
632+
if node.left == nil {
633+
node.left = newNode(node.l, node.mid)
634+
}
635+
636+
if node.right == nil {
637+
node.right = newNode(node.mid+1, node.r)
638+
}
639+
640+
if node.add != 0 || node.mul != 1 {
641+
642+
add := node.add
643+
mul := node.mul
644+
645+
left := node.left
646+
right := node.right
647+
648+
left.v = (left.v*mul + int64(left.r-left.l+1)*add) % MOD
649+
right.v = (right.v*mul + int64(right.r-right.l+1)*add) % MOD
650+
651+
left.add = (left.add*mul + add) % MOD
652+
right.add = (right.add*mul + add) % MOD
653+
654+
left.mul = left.mul * mul % MOD
655+
right.mul = right.mul * mul % MOD
656+
657+
node.add = 0
658+
node.mul = 1
659+
}
660+
}
661+
662+
type Fancy struct {
663+
n int
664+
tree *SegmentTree
665+
}
666+
667+
func Constructor() Fancy {
668+
return Fancy{
669+
tree: newSegmentTree(),
670+
}
671+
}
672+
673+
func (f *Fancy) Append(val int) {
674+
f.n++
675+
f.tree.modifyAdd(f.n, f.n, int64(val))
676+
}
677+
678+
func (f *Fancy) AddAll(inc int) {
679+
f.tree.modifyAdd(1, f.n, int64(inc))
680+
}
681+
682+
func (f *Fancy) MultAll(m int) {
683+
f.tree.modifyMul(1, f.n, int64(m))
684+
}
685+
686+
func (f *Fancy) GetIndex(idx int) int {
687+
if idx >= f.n {
688+
return -1
689+
}
690+
return f.tree.query(idx+1, idx+1)
691+
}
692+
```
693+
694+
#### TypeScript
695+
696+
```ts
697+
const MOD = 1000000007n;
698+
699+
class Node {
700+
left: Node | null = null;
701+
right: Node | null = null;
702+
703+
l: number;
704+
r: number;
705+
mid: number;
706+
707+
v = 0n;
708+
add = 0n;
709+
mul = 1n;
710+
711+
constructor(l: number, r: number) {
712+
this.l = l;
713+
this.r = r;
714+
this.mid = (l + r) >> 1;
715+
}
716+
}
717+
718+
class SegmentTree {
719+
root: Node;
720+
721+
constructor() {
722+
this.root = new Node(1, 100001);
723+
}
724+
725+
modifyAdd(l: number, r: number, inc: bigint, node: Node = this.root): void {
726+
if (l > r) return;
727+
728+
if (node.l >= l && node.r <= r) {
729+
node.v = (node.v + BigInt(node.r - node.l + 1) * inc) % MOD;
730+
node.add = (node.add + inc) % MOD;
731+
return;
732+
}
733+
734+
this.pushdown(node);
735+
736+
if (l <= node.mid) this.modifyAdd(l, r, inc, node.left!);
737+
if (r > node.mid) this.modifyAdd(l, r, inc, node.right!);
738+
739+
this.pushup(node);
740+
}
741+
742+
modifyMul(l: number, r: number, m: bigint, node: Node = this.root): void {
743+
if (l > r) return;
744+
745+
if (node.l >= l && node.r <= r) {
746+
node.v = (node.v * m) % MOD;
747+
node.add = (node.add * m) % MOD;
748+
node.mul = (node.mul * m) % MOD;
749+
return;
750+
}
751+
752+
this.pushdown(node);
753+
754+
if (l <= node.mid) this.modifyMul(l, r, m, node.left!);
755+
if (r > node.mid) this.modifyMul(l, r, m, node.right!);
756+
757+
this.pushup(node);
758+
}
759+
760+
query(l: number, r: number, node: Node = this.root): bigint {
761+
if (l > r) return 0n;
762+
763+
if (node.l >= l && node.r <= r) return node.v;
764+
765+
this.pushdown(node);
766+
767+
let v = 0n;
768+
769+
if (l <= node.mid) v = (v + this.query(l, r, node.left!)) % MOD;
770+
if (r > node.mid) v = (v + this.query(l, r, node.right!)) % MOD;
771+
772+
return v;
773+
}
774+
775+
pushup(node: Node): void {
776+
node.v = (node.left!.v + node.right!.v) % MOD;
777+
}
778+
779+
pushdown(node: Node): void {
780+
if (!node.left) node.left = new Node(node.l, node.mid);
781+
if (!node.right) node.right = new Node(node.mid + 1, node.r);
782+
783+
if (node.add !== 0n || node.mul !== 1n) {
784+
const add = node.add;
785+
const mul = node.mul;
786+
787+
const left = node.left!;
788+
const right = node.right!;
789+
790+
left.v = (left.v * mul + BigInt(left.r - left.l + 1) * add) % MOD;
791+
right.v = (right.v * mul + BigInt(right.r - right.l + 1) * add) % MOD;
792+
793+
left.add = (left.add * mul + add) % MOD;
794+
right.add = (right.add * mul + add) % MOD;
795+
796+
left.mul = (left.mul * mul) % MOD;
797+
right.mul = (right.mul * mul) % MOD;
798+
799+
node.add = 0n;
800+
node.mul = 1n;
801+
}
802+
}
803+
}
804+
805+
class Fancy {
806+
n = 0;
807+
tree = new SegmentTree();
808+
809+
append(val: number): void {
810+
this.n++;
811+
this.tree.modifyAdd(this.n, this.n, BigInt(val));
812+
}
813+
814+
addAll(inc: number): void {
815+
this.tree.modifyAdd(1, this.n, BigInt(inc));
816+
}
817+
818+
multAll(m: number): void {
819+
this.tree.modifyMul(1, this.n, BigInt(m));
820+
}
821+
822+
getIndex(idx: number): number {
823+
if (idx >= this.n) return -1;
824+
return Number(this.tree.query(idx + 1, idx + 1));
825+
}
826+
}
827+
828+
/**
829+
* Your Fancy object will be instantiated and called as such:
830+
* var obj = new Fancy()
831+
* obj.append(val)
832+
* obj.addAll(inc)
833+
* obj.multAll(m)
834+
* var param_4 = obj.getIndex(idx)
835+
*/
836+
```
837+
506838
<!-- tabs:end -->
507839

508840
<!-- solution:end -->

0 commit comments

Comments
 (0)