Problem solution · Go

Codeforces 242E — XOR on Segment

Codeforces 242E — XOR on Segment: 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
122 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 242E — XOR on Segment, 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

  • 122 lines of Go from the credited upstream file 242E.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 242E — XOR on Segment · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") type node42 struct {	l, r        int32	sum         int32	revChildren bool}type seg42 []node42 func (t seg42) _pushUp(o int) {	lo, ro := t[o<<1], t[o<<1|1]	t[o].sum = lo.sum + ro.sum} func (t seg42) _build(arr []int32, pos uint8, o int, l, r int32) {	t[o].l, t[o].r = l, r	if l == r {		t[o].sum = arr[l-1] >> pos & 1		return	}	mid := (l + r) >> 1	t._build(arr, pos, o<<1, l, mid)	t._build(arr, pos, o<<1|1, mid+1, r)	t._pushUp(o)} func (t seg42) _spread(o int) {	if t[o].revChildren {		lo, ro := &t[o<<1], &t[o<<1|1]		lo.sum = lo.r - lo.l + 1 - lo.sum		ro.sum = ro.r - ro.l + 1 - ro.sum		lo.revChildren = !lo.revChildren		ro.revChildren = !ro.revChildren		t[o].revChildren = false	}} func (t seg42) _rev(o int, l, r int32) {	ol, or := t[o].l, t[o].r	if l <= ol && or <= r {		t[o].sum = or - ol + 1 - t[o].sum		t[o].revChildren = !t[o].revChildren		return	}	t._spread(o)	mid := (ol + or) >> 1	if l <= mid {		t._rev(o<<1, l, r)	}	if mid < r {		t._rev(o<<1|1, l, r)	}	t._pushUp(o)} func (t seg42) _query(o int, l, r int32) (res int32) {	if l <= t[o].l && t[o].r <= r {		return t[o].sum	}	t._spread(o)	mid := (t[o].l + t[o].r) >> 1	if l <= mid {		res += t._query(o<<1, l, r)	}	if mid < r {		res += t._query(o<<1|1, l, r)	}	return} func (t seg42) init(arr []int32, pos uint8) { t._build(arr, pos, 1, 1, int32(len(arr))) }func (t seg42) rev(l, r int32)              { t._rev(1, l, r) }func (t seg42) query(l, r int32) int32      { return t._query(1, l, r) } // github.com/EndlessCheng/codeforces-gofunc Sol242E(reader io.Reader, writer io.Writer) {	in := bufio.NewReader(reader)	out := bufio.NewWriter(writer)	defer out.Flush() 	var n, q int	Fscan(in, &n)	arr := make([]int32, n)	for i := range arr {		Fscan(in, &arr[i])	}	trees := make([]seg42, 20)	for i := range trees {		trees[i] = make(seg42, 4*n)		trees[i].init(arr, uint8(i))	} 	for Fscan(in, &q); q > 0; q-- {		var op, l, r, x int32		Fscan(in, &op, &l, &r)		if op == 1 {			sum := 0			for i, t := range trees {				sum += 1 << uint(i) * int(t.query(l, r))			}			Fprintln(out, sum)		} else {			Fscan(in, &x)			for i, t := range trees {				if x>>uint(i)&1 == 1 {					t.rev(l, r)				}			}		}	}} //func main() {//	Sol242E(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 ↗