Problem solution · Go

Codeforces 896C — Willem, Chtholly and Seniorious

Codeforces 896C — Willem, Chtholly and Seniorious: 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
136 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 896C — Willem, Chtholly and Seniorious, 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

  • 136 lines of Go from the credited upstream file 896C.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 896C — Willem, Chtholly and Seniorious · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"sort") type odtBlock896C struct {	l, r int	val  int64} type odt896C []odtBlock896C func (t *odt896C) split(mid int) int {	ot := *t	for i, b := range ot {		if b.l == mid+1 {			return i		}		if b.l <= mid && mid < b.r {			*t = append(ot[:i+1], append(odt896C{{mid + 1, b.r, b.val}}, ot[i+1:]...)...)			ot[i].r = mid			return i + 1		}	}	return len(ot)} func (t *odt896C) prepare(l, r int) (begin, end int) {	begin = t.split(l - 1)	end = t.split(r)	return} func (t *odt896C) merge(begin, end, r int, val int64) {	ot := *t	ot[begin].r = r	ot[begin].val = val	if begin+1 < end {		*t = append(ot[:begin+1], ot[end:]...)	}} func (t odt896C) add(begin, end int, val int64) {	for i := begin; i < end; i++ {		t[i].val += val	}} func (t odt896C) kth(begin, end, k int) int64 {	blocks := make(odt896C, end-begin)	copy(blocks, t[begin:end])	sort.Slice(blocks, func(i, j int) bool { return blocks[i].val < blocks[j].val })	k--	for _, b := range blocks {		if cnt := b.r - b.l + 1; k >= cnt {			k -= cnt		} else {			return b.val		}	}	panic(k)} func (odt896C) quickPow(x int64, n int, mod int64) int64 {	x %= mod	res := int64(1) % mod	for ; n > 0; n >>= 1 {		if n&1 == 1 {			res = res * x % mod		}		x = x * x % mod	}	return res} func (t odt896C) powSum(begin, end int, n int, mod int64) (res int64) {	for _, b := range t[begin:end] {		res += int64(b.r-b.l+1) * t.quickPow(b.val, n, mod)	}	return res % mod} // github.com/EndlessCheng/codeforces-gofunc Sol896C(reader io.Reader, writer io.Writer) {	in := bufio.NewReader(reader)	out := bufio.NewWriter(writer)	defer out.Flush() 	var n, m, vMax int	var seed int64	Fscan(in, &n, &m, &seed, &vMax)	rand := func(_n int) int {		const mod int64 = 1e9 + 7		ret := seed		seed = (seed*7 + 13) % mod		return int(ret) % _n	} 	t := make(odt896C, n)	for i := range t {		t[i] = odtBlock896C{i, i, int64(rand(vMax) + 1)}	}	for ; m > 0; m-- {		op := rand(4) + 1		l, r := rand(n), rand(n)		if l > r {			l, r = r, l		}		var x int		if op == 3 {			x = rand(r-l+1) + 1		} else {			x = rand(vMax) + 1		}		begin, end := t.prepare(l, r)		switch op {		case 1:			t.add(begin, end, int64(x))		case 2:			t.merge(begin, end, r, int64(x))		case 3:			Fprintln(out, t.kth(begin, end, x))		default:			y := int64(rand(vMax) + 1)			Fprintln(out, t.powSum(begin, end, x, y))		}	}} //func main() {//	Sol896C(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 ↗