From 99281d724c4c22b502ae0d966c8c4a7a47f56c2b Mon Sep 17 00:00:00 2001 From: li-chx Date: Tue, 29 Jul 2025 11:20:14 +0800 Subject: [PATCH] =?UTF-8?q?2411.=20=E6=8C=89=E4=BD=8D=E6=88=96=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E7=9A=84=E6=9C=80=E5=B0=8F=E5=AD=90=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 72 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 679815f..7f17257 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,79 @@ -struct Solution; -impl Solution { - pub fn count_max_or_subsets(nums: Vec) -> i32 { - let max_or = nums.iter().fold(0, |acc, &x| acc | x); - let mut dp = vec![0;1<, + count: usize, +} +impl XorVal { + pub fn new() -> Self { + XorVal { data: vec!(0; 32), count: 0 } + } + pub fn get_val(&self) -> i32 { + if self.count == 0 { + return -1; + } + let mut ans = 0; + for i in 0..32 { + if self.data[i] > 0 { + ans |= 1 << i; } } - dp.iter().filter(|x| **x == max_or ).count() as i32 + ans + } +} +impl AddAssign for XorVal { + fn add_assign(&mut self, mut x: i32) { + self.count += 1; + let mut i = 0; + while x > 0 { + if x & 1 == 1 { + self.data[i] += 1; + } + x >>= 1; + i += 1; + } + } +} +impl SubAssign for XorVal { + fn sub_assign(&mut self, mut x: i32) { + self.count -= 1; + let mut i = 0; + while x > 0 { + if x & 1 == 1 { + self.data[i] -= 1; + } + x >>= 1; + i += 1; + } + } +} +struct Solution; +impl Solution { + pub fn smallest_subarrays(nums: Vec) -> Vec { + let mut ans = vec![0; nums.len()]; + let mut xor = vec![0; nums.len()]; + xor[nums.len() - 1] = nums[nums.len() - 1]; + for i in (0..nums.len() - 1).rev() { + xor[i] = xor[i + 1] | nums[i]; + } + let mut xor_val = XorVal::new(); + let (mut l, mut r) = (0, 0); + for i in 0..nums.len() { + while l < i && l <= r { + xor_val -= nums[l]; + l += 1; + } + while xor_val.get_val() < xor[i] { + xor_val += nums[r]; + r += 1; + } + ans[i] = (r - l) as i32; + } + ans } } fn main() { - let sl = Solution::count_max_or_subsets(vec![3,2,1,5]); - println!("{}", sl); + let sl = Solution::smallest_subarrays(vec![0]); + println!("{:?}", sl); } -