티스토리 뷰

https://www.acmicpc.net/problem/14245

 

14245번: XOR

첫 번째 줄에 수열의 크기 n (0 < n ≤ 500,000)이 주어진다. 두 번째 줄에 수열의 원소가 0번부터 n - 1번까지 차례대로 주어진다. 수열의 원소는 100,000보다 크지 않은 음이 아닌 정수이다. 세 번째 줄

www.acmicpc.net

//https://www.acmicpc.net/problem/14245
// XOR

// 느리게 갱신되는 세그먼트트리 재귀 구현
class LazySegmentTree {
	constructor(inputArray) {
		this.n = inputArray.length;
		this.lg = Math.ceil(Math.log2(this.n));
		this.sz = 1 << this.lg;
		this.tree = Array.from(new Array(this.sz << 1), () => 0);
		this.lazy = new Array(this.sz << 1).fill(0);
		this.init(inputArray, 1, 0, this.n - 1);
	}

	getMid(start, end) {
		return Math.floor((start + end) / 2);
	}

	init(array, node, start, end) {
		if (start == end) {
			this.tree[node] = array[start];
		} else {
			// console.log(node, node << 1, (node << 1) | 1);
			this.init(array, node * 2, start, this.getMid(start, end));
			this.init(array, node * 2 + 1, this.getMid(start, end) + 1, end);
			this.pull(node);
		}
	}

	merge(nodeA, nodeB) {
		return nodeA ^ nodeB;
	}

	pull(i) {
		this.tree[i] = this.merge(this.tree[i * 2], this.tree[i * 2 + 1]);
	}

	_update(node, start, end, index, value) {
		this.updateLazy(node, start, end);
		if (index < start || index > end) return;
		if (start == end) {
			this.tree[node] = value;
			return;
		}
		this._update(node * 2, start, Math.floor((start + end) / 2), index, value);
		this._update(node * 2 + 1, Math.floor((start + end) / 2) + 1, end, index, value);
		this.pull(node);
	}

	_query(node, start, end, left, right) {
		this.updateLazy(node, start, end);
		if (left > end || right < start) return 0;
		if (left <= start && end <= right) return this.tree[node];
		const leftQuery = this._query(node * 2, start, Math.floor((start + end) / 2), left, right);
		const rightQuery = this._query(node * 2 + 1, Math.floor((start + end) / 2) + 1, end, left, right);
		return this.merge(leftQuery, rightQuery);
	}

	updateLazy(node, start, end) {
		if (this.lazy[node] != 0) {
			if ((end - start + 1) % 2 == 1) this.tree[node] ^= this.lazy[node];
			if (start != end) {
				this.lazy[node * 2] ^= this.lazy[node];
				this.lazy[node * 2 + 1] ^= this.lazy[node];
			}
			this.lazy[node] = 0;
		}
	}

	_updateRange(node, start, end, left, right, value) {
		this.updateLazy(node, start, end);
		if (left > end || right < start) return;

		if (left <= start && end <= right) {
			if ((end - start + 1) % 2 == 1) this.tree[node] ^= value;

			if (start != end) {
				this.lazy[node * 2] ^= value;
				this.lazy[node * 2 + 1] ^= value;
			}
			return;
		}
		this._updateRange(node * 2, start, Math.floor((start + end) / 2), left, right, value);
		this._updateRange(node * 2 + 1, Math.floor((start + end) / 2) + 1, end, left, right, value);
		this.pull(node);
	}

	updatePoint(index, value) {
		return this._update(1, 0, this.n - 1, index, value);
	}

	updateRange(left, right, value) {
		return this._updateRange(1, 0, this.n - 1, left, right, value);
	}

	query(left, right) {
		return this._query(1, 0, this.n - 1, left, right);
	}
}

const input = require('fs').readFileSync('./dev/stdin').toString().trim().split('\n');
input.shift();
const nums = input.shift().split(' ').map(Number);
input.shift();
const segmentTree = new LazySegmentTree(nums);
const answer = [];

input.forEach((query) => {
	const [c, ...arg] = query.split(' ').map(Number);
	if (c == 1) {
		const [i, j, k] = arg;
		segmentTree.updateRange(i, j, k);
	} else {
		const [i] = arg;
		answer.push(segmentTree.query(i, i));
	}
});

console.log(answer.join('\n'));
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함