// ---------- begin BitSet ---------- #[derive(Clone)] struct BitSet { size: usize, a: Vec, } fn bit_size() -> usize { 8 * std::mem::size_of::() } fn quot_rem(n: usize) -> (usize, usize) { let w = bit_size(); (n / w, n % w) } #[allow(dead_code)] impl BitSet { fn new(size: usize) -> Self { let w = bit_size(); BitSet { size: size, a: vec![0; (size + w - 1) / w], } } fn set_at(&mut self, x: usize) { assert!(x < self.size); let (q, r) = quot_rem(x); self.a[q] |= 1 << r; } fn clear_at(&mut self, x: usize) { assert!(x < self.size); let (q, r) = quot_rem(x); self.a[q] &= !(1 << r); } fn get_at(&self, x: usize) -> bool { if x >= self.size { return false; } let (q, r) = quot_rem(x); (self.a[q] >> r) & 1 == 1 } fn any(&self) -> bool { self.a.iter().any(|a| *a != 0) } fn fix(&mut self) { let (q, r) = quot_rem(self.size); if r != 0 { self.a[q] &= (1 << r) - 1; } } fn clear(&mut self) { let len = self.a.len(); self.a.clear(); self.a.resize(len, 0); } fn truncate(&mut self, len: usize) { if len >= self.size { return; } let w = bit_size(); self.a.truncate((len + w - 1) / w); self.size = len; self.fix(); } fn popcnt(&self) -> usize { self.a.iter().fold(0, |s, a| s + a.count_ones() as usize) } fn shift_left(&self, rhs: usize) -> Self { let (q, r) = quot_rem(rhs); let mut ans = BitSet::new(self.size + rhs); if r == 0 { for (x, y) in ans.a[q..].iter_mut().zip(self.a.iter()) { *x = *y; } } else { let w = bit_size(); let mut prev = 0; for (x, y) in ans.a[q..].iter_mut().zip(self.a.iter()) { *x = (*y << r) | (prev >> (w - r)); prev = *y; } *ans.a.last_mut().unwrap() |= prev >> (w - r); } ans.fix(); ans } fn shift_right(&self, rhs: usize) -> Self { if rhs >= self.size { return BitSet::new(1); } let (q, r) = quot_rem(rhs); let mut ans = BitSet::new(self.size - rhs); if r == 0 { for (x, y) in ans.a.iter_mut().zip(self.a[q..].iter()) { *x = *y; } } else { let w = bit_size(); let mut prev = 0; for (x, y) in ans.a.iter_mut().zip(self.a[q..].iter()).rev() { *x |= (*y >> r) | (prev << (w - r)); prev = *y; } } ans.fix(); ans } fn bitwise_or(&self, rhs: &Self) -> Self { let (x, y) = if self.size >= rhs.size {(self, rhs)} else {(rhs, self)}; let mut a = x.a.clone(); for (a, y) in a.iter_mut().zip(y.a.iter()) { *a |= *y; } BitSet { size: x.size, a: a, } } fn bitwise_and(&self, rhs: &Self) -> Self { let (x, y) = if self.size <= rhs.size {(self, rhs)} else {(rhs, self)}; let mut a = x.a.clone(); for (a, y) in a.iter_mut().zip(y.a.iter()) { *a &= *y; } BitSet { size: x.size, a: a, } } fn bitwise_xor(&self, rhs: &Self) -> Self { let (x, y) = if self.size >= rhs.size {(self, rhs)} else {(rhs, self)}; let mut a = x.a.clone(); for (a, y) in a.iter_mut().zip(y.a.iter()) { *a ^= *y; } BitSet { size: x.size, a: a, } } fn bitwise_or_assign(&mut self, rhs: &Self) { if self.size < rhs.size { self.size = rhs.size; self.a.resize(rhs.a.len(), 0); } for (a, b) in self.a.iter_mut().zip(rhs.a.iter()) { *a |= *b; } } fn bitwise_and_assign(&mut self, rhs: &Self) { if self.size > rhs.size { self.size = rhs.size; self.a.resize(rhs.a.len(), 0); } for (a, b) in self.a.iter_mut().zip(rhs.a.iter()) { *a &= *b; } } fn bitwise_xor_assign(&mut self, rhs: &Self) { if self.size < rhs.size { self.size = rhs.size; self.a.resize(rhs.a.len(), 0); } for (a, b) in self.a.iter_mut().zip(rhs.a.iter()) { *a ^= *b; } } } // ---------- end BitSet ---------- use std::io::Read; use std::io::Write; fn run() { let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut it = s.trim().split_whitespace(); let n: usize = it.next().unwrap().parse().unwrap(); let m: usize = it.next().unwrap().parse().unwrap(); let mut a = vec![(0usize, 0); n]; for a in a.iter_mut() { a.0 = it.next().unwrap().parse().unwrap(); } let q: usize = it.next().unwrap().parse().unwrap(); let mut edge = vec![]; for i in 0..q { let x = it.next().unwrap().parse::().unwrap() - 1; let v: usize = it.next().unwrap().parse().unwrap(); if i > 0 { edge.push((a[x].1, i, a[x].0)); } a[x] = (v, i); } for (a, s) in a { edge.push((s, q, a)); } let mut ans = vec![false; q]; let mut bit = BitSet::new(1); bit.set_at(0); let mut stack = vec![(0, q, edge, bit)]; while let Some((l, r, e, mut bit)) = stack.pop() { let mid = (l + r) / 2; let mut left = vec![]; let mut right = vec![]; for (x, y, v) in e { if x <= l && r <= y { let mut shift = bit.shift_left(v); shift.truncate(m + 1); bit.bitwise_or_assign(&shift); continue; } if l < mid { left.push((x, y, v)); } if mid < r { right.push((x, y, v)); } } if l + 1 == r { ans[l] = bit.get_at(m); } else { stack.push((l, mid, left, bit.clone())); stack.push((mid, r, right, bit)); } } let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); for a in ans { writeln!(out, "{}", a as usize).ok(); } } fn main() { run(); }