当前位置: 首页 > news >正文

P5670 秘籍-反复异或 Solution

Description

给定序列 a = ( a 1 , a 2 , ⋯ , a n ) a=(a_1,a_2,\cdots,a_n) a=(a1,a2,,an) 和常数 m m m,有 q q q 个操作分两种:

  • add ⁡ ( l , r , x ) \operatorname{add}(l,r,x) add(l,r,x):对每个 i ∈ [ l , r ] i\in[l,r] i[l,r] 执行 a i ← a i + x a_i\gets a_i+x aiai+x.
  • query ⁡ ( l , r ) \operatorname{query}(l,r) query(l,r):求 ( xor ⁡ i = l r a i ) m o d 2 m (\mathop{\operatorname{xor}}\limits_{i=l}^ra_i)\bmod 2^m (i=lxorrai)mod2m.

Limitations

1 ≤ n , q ≤ 1 0 5 1 \le n,q \le 10^5 1n,q105
1 ≤ m ≤ 10 \textcolor{red}{1\le m \le 10} 1m10
0 ≤ a i , x < 2 m 0\le a_i,x<2^m 0ai,x<2m
0.5 s , 256 MB \textcolor{red}{0.5\text{s}},256\text{MB} 0.5s,256MB

Solution

m m m 很小,考虑基于值域的做法.
由于异或性质,我们只用维护一个数出现次数奇偶性,这可以压在一个 1024 1024 1024 位的 bitset f f f 里,第 i i i 位表示 i i i 的出现次数奇偶性.
那么合并就是将两个 bitset xor ⁡ \operatorname{xor} xor 起来.
区间加 x x x 就是将 f f f 循环左移 x x x 次,可以写作 f = (f >> (1024 - x)) | (f << x).
显然可用线段树维护,记得卡常.

Code

4.63 KB , 2.79 s , 55.88 MB (in total, C++20 with O2) 4.63\text{KB},2.79\text{s},55.88\text{MB}\;\texttt{(in total, C++20 with O2)} 4.63KB,2.79s,55.88MB(in total, C++20 with O2)
fastiolazy_segment 删了.

#include <bits/stdc++.h>
using namespace std;using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;template<class T>
bool chmax(T &a, const T &b){if(a < b){ a = b; return true; }return false;
}template<class T>
bool chmin(T &a, const T &b){if(a > b){ a = b; return true; }return false;
}namespace fastio {}   // Removedusing fastio::read;
using fastio::write;template<class Info, class Tag>
struct lazy_segment{};   // Removedconstexpr int B = 1024, mask = 1023;struct Tag {int tag;inline Tag() : tag(0) {}inline Tag(int _tag) : tag(_tag) {}inline void apply(const Tag& t) { tag += t.tag; tag &= mask; }
};struct Info {bitset<B> bs;inline Info() {}inline Info(int x) { bs.reset(); bs[x] = 1; }inline void apply(const Tag& t) {bs = (bs >> (B - t.tag)) | (bs << t.tag);}
};inline Info operator+(const Info& lhs, const Info& rhs) {Info res;res.bs = lhs.bs ^ rhs.bs;return res;
}signed main() {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);const int n = read<int>(), m = read<int>(), q = read<int>();vector<int> a(n);for (int i = 0; i < n; i++) a[i] = read<int>();lazy_segment<Info, Tag> sgt(a);for (int i = 0, op, l, r; i < q; i++) {op = read<int>(), l = read<int>(), r = read<int>();l--, r--;if (op == 1) sgt.apply(l, r, read<int>());else {auto bs = sgt.query(l, r).bs;int ans = 0;for (int i = 0; i < B; i++) ans ^= bs[i] * i;write(ans & ((1 << m) - 1));putchar_unlocked('\n');}}return 0;
}

相关文章:

  • 日语学习-日语知识点小记-构建基础-JLPT-N4阶段(8): - (1)复习一些语法(2)「~ています」
  • C++中函数的实现写在头文件内
  • 第 6 篇:衡量预测好坏 - 评估指标
  • 机器视觉lcd屏增光片贴合应用
  • unity基础自学2.3:移动和抓握物品
  • Qt项目——汽车仪表盘
  • Git SSH 密钥多个 Git 来源
  • 研究夜间灯光数据在估计出行需求方面的潜力
  • MySQL 按照日期统计记录数量
  • python 练习
  • 基于LoRA的Llama 2二次预训练实践:高效低成本的大模型领域适配
  • 使用c++调用deepseek的api(附带源码)
  • AI律师匹配AI分析法律需求意图并匹配律师
  • 为什么在TCP层(即传输层)没有解决半包、粘包的问题
  • 基于SpringBoot的在线抽奖系统测试用例报告
  • unity之协程
  • 算法学习(二)
  • 【Leetcode 每日一题】2145. 统计隐藏数组数目
  • 爬虫学习——LinkEXtractor提取链接与Exporter导出数据
  • OpenCV 图形API(47)颜色空间转换-----将 I420(YUV 4:2:0) 格式的图像数据转换为 RGB 格式
  • 最新民调显示特朗普支持率降至上任以来新低
  • 分离19年后:陈杨梅首度露面,父亲亲手喂棉花糖给女儿吃
  • GDP增长6.0%,一季度浙江经济数据出炉
  • 世遗X时尚,七匹狼这场大秀秀出中国文化独特魅力
  • “这是本届政府的态度”,英国明确拒绝与中国脱钩
  • 一中国公民在日本滑雪场意外死亡,我领馆发布提醒