CF242E XOR on Segment

codeforces

洛谷

关于异或,无法运用懒标记实现区间异或;

可以像trie树一样拆位,将每个值拆成二进制数,对此建相应个数的线段树。

0 1与 0异或 数字不变

0 1与 1异或 数字翻转

由此,对于一个01串的每一个字符都与1异或 则 1的个数 = 串长 - 现在1的个数

查询:对所有的线段树[l,r]查询1的个数,在乘上对应的二进制位权,他们之和就是查询的答案。

Code
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define endl '\n'
  4. const int M = 20;
  5. int n, now;
  6. vector<vector<int>> a;
  7. class segtree {
  8. public:
  9. struct node {
  10. int tag = 0;
  11. int64_t sum = 0;
  12. void apply(int l, int r) {
  13. // make v become node(tag,data) in modify
  14. sum = r - l + 1 - sum;
  15. tag ^= 1;
  16. }
  17. void init(int v) {
  18. // in build_tree init
  19. sum = v;
  20. tag = 0;
  21. }
  22. };
  23. node unite(const node &a, const node &b) const {
  24. node res;
  25. res.sum = a.sum + b.sum;
  26. return res;
  27. }
  28. // about x: left son is x+1 , right son is x+((mid-l+1)<<1) ;
  29. inline void push_down(int x, int l, int r) {
  30. int y = (l + r) >> 1;
  31. int z = x + ((y - l + 1) << 1);
  32. // push from x into (x + 1) and z
  33. if (tree[x].tag) {
  34. tree[x + 1].apply(l, y);
  35. tree[z].apply(y + 1, r);
  36. tree[x].tag = 0;
  37. }
  38. }
  39. int n;
  40. vector<node> tree;
  41. inline void push_up(int x, int z) { tree[x].sum = unite(tree[x + 1], tree[z]).sum; }
  42. void build(int x, int l, int r) {
  43. if (l == r) {
  44. tree[x].init(a[l][now]);
  45. return;
  46. }
  47. int y = (l + r) >> 1;
  48. int z = x + ((y - l + 1) << 1);
  49. build(x + 1, l, y);
  50. build(z, y + 1, r);
  51. push_up(x, z);
  52. }
  53. node get(int x, int l, int r, int ll, int rr) {
  54. if (ll <= l && r <= rr) {
  55. return tree[x];
  56. }
  57. int y = (l + r) >> 1;
  58. int z = x + ((y - l + 1) << 1);
  59. push_down(x, l, r);
  60. node res{};
  61. if (rr <= y)
  62. res = get(x + 1, l, y, ll, rr);
  63. else {
  64. if (ll > y)
  65. res = get(z, y + 1, r, ll, rr);
  66. else
  67. res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));
  68. }
  69. push_up(x, z);
  70. return res;
  71. }
  72. void modify(int x, int l, int r, int ll, int rr) {
  73. if (ll <= l && r <= rr) {
  74. tree[x].apply(l, r);
  75. return;
  76. }
  77. int y = (l + r) >> 1;
  78. int z = x + ((y - l + 1) << 1);
  79. push_down(x, l, r);
  80. if (ll <= y) modify(x + 1, l, y, ll, rr);
  81. if (rr > y) modify(z, y + 1, r, ll, rr);
  82. push_up(x, z);
  83. }
  84. segtree(int _n = ::n) : n(_n) {
  85. assert(n > 0);
  86. tree.resize(2 * n - 1);
  87. }
  88. node get(int ll, int rr) {
  89. assert(0 <= ll && ll <= rr && rr <= n - 1);
  90. return get(0, 0, n - 1, ll, rr);
  91. }
  92. void modify(int ll, int rr) {
  93. assert(0 <= ll && ll <= rr && rr <= n - 1);
  94. modify(0, 0, n - 1, ll, rr);
  95. }
  96. }; // root's idx is 0 and the begin of vector is also 0;
  97. // don't forget idx is from 0 to n-1 (equal [--x,--y]) when ask;
  98. signed main() {
  99. std::ios_base::sync_with_stdio(false);
  100. std::cin.tie(nullptr), std::cout.tie(nullptr);
  101. int q;
  102. cin >> n;
  103. segtree t[M];
  104. a.resize(n, vector<int>(M));
  105. for (int i = 0, x; i < n; ++i) {
  106. cin >> x;
  107. for (int j = 0; j < M; ++j) a[i][j] = x >> j & 1;
  108. }
  109. for (int i = 0; i < M; ++i) now = i, t[i].build(0, 0, n - 1);
  110. cin >> q;
  111. while (q--) {
  112. int op, l, r;
  113. cin >> op >> l >> r;
  114. --l, --r;
  115. if (op == 1) {
  116. int64_t ans = 0, p = 1;
  117. for (int i = 0; i < M; ++i, p <<= 1) ans += p * t[i].get(l, r).sum;
  118. cout << ans << endl;
  119. } else {
  120. int64_t k;
  121. cin >> k;
  122. for (int i = 0; i < M; ++i)
  123. if (k >> i & 1) t[i].modify(l, r);
  124. }
  125. }
  126. return 0;
  127. }

CF242E XOR on Segment的更多相关文章

  1. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  2. codeforces 242E - XOR on Segment (线段树 按位数建树)

    E. XOR on Segment time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...

  3. 「CF242E」XOR on Segment 解题报告

    题面 长度为\(n\)的数列,现有两种操作: 1.区间异或操作 2.区间求和操作 对于每个查询,输出答案 思路: 线段树+二进制拆位 线段树区间修改一般使用的都是懒标记的方法,但是对于异或,懒标记的方 ...

  4. 线段树+二进制位拆分【CF242E】XOR on Segment

    Description 给定一个长为\(n(n<=10^5)\)的数组 数组里的数不超过\(10^6\) 有两种操作: 1:求\(sum[l,r]\); 2:对\([l,r]\)中的所有数和\( ...

  5. CodeForces 242E "XOR on Segment"(线段树)

    传送门 •题意 给你一个包含 n 个数的序列 a,定义序列上的两个操作: (1)$1,l,r\ :\ ans=\sum_{i=l}^{r}a_i$; (2)$2,l,r,x\ :\ \forall\ ...

  6. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  7. CodeForces 242E - XOR on Segment 二维线段树?

    今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...

  8. codeforces 242E. XOR on Segment 线段树

    题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...

  9. Codeforces 242E:XOR on Segment(位上的线段树)

    http://codeforces.com/problemset/problem/242/E 题意:给出初始n个数,还有m个操作,操作一种是区间求和,一种是区间xor x. 思路:昨天比赛出的一道类似 ...

随机推荐

  1. unity---UI管理模块

    UI管理器 任务: 1.所有面板的父类,2.UIMgr 所有UI控件都继承UIBehaviour 面板基类 找到相应空间 简化后 也存在问题:一个物体可以同时挂载两个组件 导致键相同,而值不同, 将值 ...

  2. CoaXPress 时间戳 Time Stamping

    背景 在CXP2.0之前,CXP没有定义Time Stamping时间戳的概念,但是用户对Time Stamping是有实际需求的,比如我们要对比多台设备拍摄同一个物体不同角度的照片,或者记录触发完成 ...

  3. STM32 CubeMx使用教程

    一.STM32CubeMX 简介 STM32CubeMX 是 ST 意法半导体近几年来大力推荐的STM32 芯片图形化配置工具,目的就是为了方便开发者, 允许用户使用图形化向导生成C 初始化代码,可以 ...

  4. einsum函数介绍-张量常用操作

    einsum函数说明 pytorch文档说明:\(torch.einsum(equation, **operands)\) 使用基于爱因斯坦求和约定的符号,将输入operands的元素沿指定的维数求和 ...

  5. CF1625D - Binary Spiders[trie树优化dp]

    官方题解 题意:给数列a[],选择尽量多的数满足任意两个异或起来<=k 1625D - Binary Spiders 思路:首先,将数列排序得到,然后升序取得的值的任意两个最小值为相邻两个异或的 ...

  6. nazo.io 通关记录

    游戏网址 说在前面 答案错误页面 nazo.io/wrong 攻略 第0关 谜.io 纯粹是欢迎你来游戏. 所以他给你的start就是答案. 第1关 欢迎 它用灰体字写了key: welcome 直接 ...

  7. Spring Authorization Server(AS)从 Mysql 中读取客户端、用户

    Spring AS 持久化 jdk version: 17 spring boot version: 2.7.0 spring authorization server:0.3.0 mysql ver ...

  8. JS:String

    String数据类型:字符串 字符串是存储字符的变量. 字符串可以是引号中(可以使用单引号或双引号)的任意文本. var a = "abc"; var b = "123& ...

  9. SAP 实例 12 List Box with Value List from PBO Module

    REPORT demo_dynpro_dropdown_listbox. DATA: name TYPE vrm_id, list TYPE vrm_values, value LIKE LINE O ...

  10. RocketMQ消息的顺序与重复

    1.如何保证消息的顺序 原因:生产者将消息发给topic,topic分发给不同的队列再给多个消费者并发消费,难以保证顺序. 方案:topic和队列之间加入MessageQueueSelector.将一 ...