結果

問題 No.1891 Static Xor Range Composite Query
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-03-18 19:59:52
言語 Go
(1.22.1)
結果
AC  
実行時間 1,015 ms / 5,000 ms
コード長 2,269 bytes
コンパイル時間 12,338 ms
コンパイル使用メモリ 219,540 KB
実行使用メモリ 98,096 KB
最終ジャッジ日時 2023-10-18 17:28:25
合計ジャッジ時間 29,041 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 6 ms
4,348 KB
testcase_12 AC 6 ms
4,348 KB
testcase_13 AC 6 ms
4,348 KB
testcase_14 AC 6 ms
4,348 KB
testcase_15 AC 6 ms
4,348 KB
testcase_16 AC 6 ms
4,348 KB
testcase_17 AC 6 ms
4,348 KB
testcase_18 AC 6 ms
4,348 KB
testcase_19 AC 7 ms
4,348 KB
testcase_20 AC 6 ms
4,348 KB
testcase_21 AC 1,014 ms
98,096 KB
testcase_22 AC 1,007 ms
98,096 KB
testcase_23 AC 1,008 ms
98,096 KB
testcase_24 AC 1,004 ms
98,096 KB
testcase_25 AC 1,006 ms
98,096 KB
testcase_26 AC 1,008 ms
98,096 KB
testcase_27 AC 1,008 ms
98,096 KB
testcase_28 AC 1,006 ms
98,096 KB
testcase_29 AC 1,006 ms
98,096 KB
testcase_30 AC 1,015 ms
98,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	// https://yukicoder.me/problems/no/1891
	// 给定一个长为2的幂次的数组
	// 区间仿射变换,区间查询时每个下标异或上给定的数
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n, q int
	fmt.Fscan(in, &n, &q)
	leaves := make([]S, n)
	for i := range leaves {
		fmt.Fscan(in, &leaves[i].mul, &leaves[i].add)
	}

	// RangeAffineRangeComposite
	seg := NewDisjointSparseTableXor(leaves)

	for i := 0; i < q; i++ {
		var start, end, indexXor, x int
		fmt.Fscan(in, &start, &end, &indexXor, &x)
		res := seg.Query(start, end, indexXor)
		fmt.Fprintln(out, (res.mul*x+res.add)%MOD)
	}
}

const MOD int = 998244353

type S = struct{ mul, add int }

func (*DisjointSparseTableXor) e() S { return S{1, 0} }
func (*DisjointSparseTableXor) op(a, b S) S {
	return S{a.mul * b.mul % MOD, (a.add*b.mul + b.add) % MOD}
}

type DisjointSparseTableXor struct {
	log  int
	data [][]S
}

// DisjointSparseTableXor 支持半群的区间静态查询.
//  op:只需要满足结合律 op(op(a,b),c) = op(a,op(b,c)).
//  区间查询时下标可以异或上 indexXor.
//  !nums 的长度必须要是2的幂.
func NewDisjointSparseTableXor(nums []S) *DisjointSparseTableXor {
	res := &DisjointSparseTableXor{}
	n := len(nums)
	log := 0
	for 1<<log < n {
		log++
	}
	if 1<<log != n {
		panic("len(nums) must be power of 2")
	}
	data := make([][]S, log+1)
	data[0] = make([]S, 1<<log)
	copy(data[0], nums)
	for k := 0; k < log; k++ {
		data[k+1] = make([]S, 1<<log)
		for i := 0; i < 1<<log; i++ {
			data[k+1][i] = res.op(data[k][i], data[k][i^(1<<k)])
		}
	}

	res.log = log
	res.data = data
	return res
}

// Calculate prod_{l<=i<r} A[x xor i], in O(log N) time.
func (st *DisjointSparseTableXor) Query(start, end, indexXor int) S {
	xl, xr := st.e(), st.e()
	for k := 0; k <= st.log; k++ {
		if start >= end {
			break
		}
		if start&(1<<k) != 0 {
			xl = st.op(xl, st.data[k][start^indexXor])
			start += 1 << k
		}
		if end&(1<<k) != 0 {
			end -= 1 << k
			xr = st.op(st.data[k][end^indexXor], xr)
		}
	}
	return st.op(xl, xr)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
0