Problem solution · Go

Codeforces 1736C2 — Good Subarrays (Hard Version)

Codeforces 1736C2 — Good Subarrays (Hard Version): 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
81 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 1736C2 — Good Subarrays (Hard Version), 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

  • 81 lines of Go from the credited upstream file 1736C2.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 1736C2 — Good Subarrays (Hard Version) · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // https://github.com/EndlessChengfunc cf1736C2(in io.Reader, _w io.Writer) {	out := bufio.NewWriter(_w)	defer out.Flush()	var n, m, l, l2, r2, x int	Fscan(in, &n)	ans := n * (n + 1) / 2	right := make([]int, n)	right2 := make([]int, n)	for i := range right {		right[i] = n		right2[i] = n	}	a := make([]int, n)	for i := range a {		Fscan(in, &a[i])		v := a[i] - i - 1		for l2 < r2 && -l2 > v {			right2[l2] = i			l2++		}		for l < i && -l > v {			ans -= n - i			right[l] = i			l++			r2++		}	} 	sumR := make([]int, n+1)	sumInc := make([]int, n+1)	for i, v := range right {		sumR[i+1] = sumR[i] + v		sumInc[i+1] = sumInc[i] + right2[i] - v	} 	ge := make([]int, n+1)	p := 0	for i := range ge {		for right[p] < i {			p++		}		ge[i] = p	} 	Fscan(in, &m)	for range m {		Fscan(in, &p, &x)		p--		tar := x - p - 1 // 变为 tar		l := ge[p+1] // 第一个包含 p 的区间		if x <= a[p] {			if tar >= -l {				Fprintln(out, ans) // 无影响			} else {				r := -tar				// 左端点为 l,l+1,...,r-1 的区间,右开端点从 right[i] 缩小为 p				Fprintln(out, ans-(sumR[r]-sumR[l]-(r-l)*p))			}		} else {			if p == 0 || right[p-1] < p-1 {				Fprintln(out, ans) // 无影响			} else {				ll := max(ge[p], -tar)				// 左端点为 ll,ll+1,...,l-1 的区间,都受到 a[p] 影响,右端点从 right[i] 扩大为 right2[i]				Fprintln(out, ans+sumInc[l]-sumInc[ll])			}		}	}} //func main() { cf1736C2(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 ↗