Problem solution · Go

Codeforces 2042F — Two Subarrays

Codeforces 2042F — Two Subarrays: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
Source
EndlessCheng Codeforces Go
Length
118 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Codeforces 2042F — Two Subarrays, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 118 lines of Go from the credited upstream file 2042F.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 2042F — Two Subarrays · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"math/bits") // https://github.com/EndlessChengconst inf42 int = 1e18 type mat42 [5][5]int type seg42 []struct {	l, r int	val  mat42} func newVal42(a, b int) mat42 {	return mat42{		{0, a + b, a + b*2, -inf42, -inf42},		{-inf42, a, a + b, -inf42, -inf42},		{-inf42, -inf42, 0, a + b, a + b*2},		{-inf42, -inf42, -inf42, a, a + b},		{-inf42, -inf42, -inf42, -inf42, 0},	}} func (seg42) mergeInfo(a, b mat42) (c mat42) {	for i := range 5 {		for j := range 5 {			c[i][j] = -inf42		}	}	for i := range 5 {		for k := i; k < 5; k++ {			for j := k; j < 5; j++ {				c[i][j] = max(c[i][j], a[i][k]+b[k][j])			}		}	}	return} func (t seg42) build(a [][2]int, o, l, r int) {	t[o].l, t[o].r = l, r	if l == r {		t[o].val = newVal42(a[l][0], a[l][1])		return	}	m := (l + r) >> 1	t.build(a, o<<1, l, m)	t.build(a, o<<1|1, m+1, r)	t.maintain(o)} func (t seg42) update(o, i, a, b int) {	if t[o].l == t[o].r {		t[o].val = newVal42(a, b)		return	}	m := (t[o].l + t[o].r) >> 1	if i <= m {		t.update(o<<1, i, a, b)	} else {		t.update(o<<1|1, i, a, b)	}	t.maintain(o)} func (t seg42) maintain(o int) {	t[o].val = t.mergeInfo(t[o<<1].val, t[o<<1|1].val)} func (t seg42) query(o, l, r int) mat42 {	if l <= t[o].l && t[o].r <= r {		return t[o].val	}	m := (t[o].l + t[o].r) >> 1	if r <= m {		return t.query(o<<1, l, r)	}	if m < l {		return t.query(o<<1|1, l, r)	}	return t.mergeInfo(t.query(o<<1, l, r), t.query(o<<1|1, l, r))} func cf2042F(in io.Reader, _w io.Writer) {	out := bufio.NewWriter(_w)	defer out.Flush()	var n, q, op, p, x int	Fscan(in, &n)	a := make([][2]int, n)	for i := range a {		Fscan(in, &a[i][0])	}	for i := range a {		Fscan(in, &a[i][1])	} 	t := make(seg42, 2<<bits.Len(uint(n-1)))	t.build(a, 1, 0, n-1)	for Fscan(in, &q); q > 0; q-- {		Fscan(in, &op, &p, &x)		p--		if op < 3 {			a[p][op-1] = x			t.update(1, p, a[p][0], a[p][1])		} else {			Fprintln(out, t.query(1, p, x-1)[0][4])		}	}} //func main() { cf2042F(bufio.NewReader(os.Stdin), os.Stdout) } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗