Iteraive Code for Basic Segment Tree { RANGE XOR AND POINT UPDATE }

Правка en3, от fluxgamer999, 2024-04-16 16:47:41
Your code here...
void solve() {
	int n , q ;
	cin >> n >> q ;
	vector<int> arr(n);
	for (int i  = 0 ; i < n ; i++) {
		cin >> arr[i];
	}
	while (n & (n - 1)) {
		n++;
		arr.pb(0);
	}
	vector<ll> seg(2 * n, 0 );
	for (int i  = 0 ; i  < n ; i++) {
		seg[n + i] = arr[i];
	}
	for (int i = n - 1 ; i >= 1 ; i--) {
		seg[i] = (seg[2 * i] ^ seg[2 * i + 1]);
	}
	auto query = [&](int l , int r) {
		l += n;
		r += n;
		ll ans = 0 ;
		while (l < r) {
			if (l & 1) ans = (ans ^ seg[l++]);
			if (r & 1) ans = (ans ^ seg[--r]);
			l /= 2; r /= 2;
		}
		return ans ;
	};
	auto update = [&](int pos , int val) {
		pos += n;
		seg[pos] = val ;	
		while (pos > 1) {
			pos /= 2;
			seg[pos] = (seg[2 * pos] ^ seg[2 * pos + 1]);
		}
	};
	while (q--) {
		int  type = 2  , l, r ;
		cin >> l >> r ;
		if (type == 2) {
			l--; r;
			cout << query(l, r) << endl;
		}
		else {
			update(--l, r);
		}

	}
}

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en3 Английский fluxgamer999 2024-04-16 16:47:41 38
en2 Английский fluxgamer999 2024-04-16 16:45:44 37 Tiny change: 'void solve' -> '\n~~~~~\nYour code here...\n~~~~~\n\nvoid solve'
en1 Английский fluxgamer999 2024-04-16 16:45:23 996 Initial revision (published)