Problem solution · Go

Codeforces 339D — Xenia and Bit Operations

Codeforces 339D — Xenia and Bit Operations: a Go solution using segment tree or range structure. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Segment tree or range structure
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

Segment tree or range structure

For Codeforces 339D — Xenia and Bit Operations, the implementation stores interval information in a range-query data structure so updates and queries avoid rescanning the full input.

  1. Choose the aggregate stored for each interval or prefix.
  2. Build or initialize the structure from the input.
  3. Apply updates and combine the affected nodes to answer each query.

Code notes

  • 81 lines of Go from the credited upstream file 339D.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the build once, then multiply the logarithmic update or query path by the number of operations.

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 339D — Xenia and Bit Operations · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") type node339D struct {	l, r   int	val    int	needOR bool}type segmentTree339D []node339D func (t segmentTree339D) _pushUp(o int) {	lo, ro := t[o<<1], t[o<<1|1]	if lo.needOR {		t[o].val = lo.val | ro.val	} else {		t[o].val = lo.val ^ ro.val	}} func (t segmentTree339D) _build(arr []int, o, l, r int) {	t[o].l, t[o].r = l, r	if l == r {		t[o].val = arr[l]		t[o].needOR = true		return	}	mid := (l + r) >> 1	t._build(arr, o<<1, l, mid)	t._build(arr, o<<1|1, mid+1, r)	t[o].needOR = !t[o<<1].needOR	t._pushUp(o)} func (t segmentTree339D) _update(o, idx int, val int) {	if t[o].l == t[o].r {		t[o].val = val		return	}	if idx <= (t[o].l+t[o].r)>>1 {		t._update(o<<1, idx, val)	} else {		t._update(o<<1|1, idx, val)	}	t._pushUp(o)} func (t segmentTree339D) init(arr []int)          { t._build(arr, 1, 1, len(arr)-1) }func (t segmentTree339D) update(idx int, val int) { t._update(1, idx, val) } func Sol339D(reader io.Reader, writer io.Writer) {	in := bufio.NewReader(reader)	out := bufio.NewWriter(writer)	defer out.Flush() 	var n, m int	Fscan(in, &n, &m)	n = 1 << uint(n)	arr := make([]int, n+1)	for i := 1; i <= n; i++ {		Fscan(in, &arr[i])	} 	t := make(segmentTree339D, 2*n)	t.init(arr)	for ; m > 0; m-- {		var idx, val int		Fscan(in, &idx, &val)		t.update(idx, val)		Fprintln(out, t[1].val)	}} //func main() {//	Sol339D(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 ↗