CF242E XOR on Segment

codeforces

洛谷

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

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

0 1与 0异或 数字不变

0 1与 1异或 数字翻转

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

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

Code
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int M = 20;
int n, now;
vector<vector<int>> a;
class segtree {
public:
struct node {
int tag = 0;
int64_t sum = 0;
void apply(int l, int r) {
// make v become node(tag,data) in modify
sum = r - l + 1 - sum;
tag ^= 1;
}
void init(int v) {
// in build_tree init
sum = v;
tag = 0;
}
}; node unite(const node &a, const node &b) const {
node res;
res.sum = a.sum + b.sum;
return res;
}
// about x: left son is x+1 , right son is x+((mid-l+1)<<1) ;
inline void push_down(int x, int l, int r) {
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
// push from x into (x + 1) and z
if (tree[x].tag) {
tree[x + 1].apply(l, y);
tree[z].apply(y + 1, r);
tree[x].tag = 0;
}
} int n;
vector<node> tree;
inline void push_up(int x, int z) { tree[x].sum = unite(tree[x + 1], tree[z]).sum; }
void build(int x, int l, int r) {
if (l == r) {
tree[x].init(a[l][now]);
return;
}
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
build(x + 1, l, y);
build(z, y + 1, r);
push_up(x, z);
} node get(int x, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) {
return tree[x];
}
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
push_down(x, l, r);
node res{};
if (rr <= y)
res = get(x + 1, l, y, ll, rr);
else {
if (ll > y)
res = get(z, y + 1, r, ll, rr);
else
res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));
}
push_up(x, z);
return res;
}
void modify(int x, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) {
tree[x].apply(l, r);
return;
}
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
push_down(x, l, r);
if (ll <= y) modify(x + 1, l, y, ll, rr);
if (rr > y) modify(z, y + 1, r, ll, rr);
push_up(x, z);
} segtree(int _n = ::n) : n(_n) {
assert(n > 0);
tree.resize(2 * n - 1);
} node get(int ll, int rr) {
assert(0 <= ll && ll <= rr && rr <= n - 1);
return get(0, 0, n - 1, ll, rr);
} void modify(int ll, int rr) {
assert(0 <= ll && ll <= rr && rr <= n - 1);
modify(0, 0, n - 1, ll, rr);
}
}; // root's idx is 0 and the begin of vector is also 0;
// don't forget idx is from 0 to n-1 (equal [--x,--y]) when ask;
signed main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr), std::cout.tie(nullptr);
int q;
cin >> n;
segtree t[M];
a.resize(n, vector<int>(M));
for (int i = 0, x; i < n; ++i) {
cin >> x;
for (int j = 0; j < M; ++j) a[i][j] = x >> j & 1;
}
for (int i = 0; i < M; ++i) now = i, t[i].build(0, 0, n - 1);
cin >> q;
while (q--) {
int op, l, r;
cin >> op >> l >> r;
--l, --r;
if (op == 1) {
int64_t ans = 0, p = 1;
for (int i = 0; i < M; ++i, p <<= 1) ans += p * t[i].get(l, r).sum;
cout << ans << endl;
} else {
int64_t k;
cin >> k;
for (int i = 0; i < M; ++i)
if (k >> i & 1) t[i].modify(l, r);
}
}
return 0;
}

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. 使用git提交和拉取gitee的代码

    使用git提交和拉取gitee的代码 1. 安装Git(自行摸索) 2. 在gitee新建仓库 名称和路径自己写 这两个二选一足矣 默认分支master就行 复制这个链接,待会要用 3. 新建项目目录 ...

  2. 论文解读(gCooL)《Graph Communal Contrastive Learning》

    论文信息 论文标题:Graph Communal Contrastive Learning论文作者:Bolian Li, Baoyu Jing, Hanghang Tong论文来源:2022, WWW ...

  3. Python数据分析--Numpy常用函数介绍(6)--Numpy中与股票成交量有关的计算

    成交量(volume)是投资中一个非常重要的变量,它是指在某一时段内具体的交易数,可以在分时图中绘制,包括日线图.周线图.月线图甚至是5分钟.30分钟.60分钟图中绘制. 股票市场成交量的变化反映了资 ...

  4. 【C++函数题目】重载完成Compare函数

    题目来源链接:https://www.dotcpp.com/oj/problem2008.html 题目讲解链接:http://6o2.cn/1yjJB2  题目描述 利用函数重载完成三个比较大小的C ...

  5. 开发工具-Redis Desktop Manager下载地址

    更新记录 2022年6月10日 完善标题. 官方: https://github.com/uglide/RedisDesktopManager 免费打包版: https://github.com/le ...

  6. 在公网服务器搭建CobaltStrike4.0

    因为工作需要使用cs,正好之前腾讯云薅了一把羊毛,就把VPS装起来cs. 选的环境是centos7.6 cs运行需要java环境 先使用yum -y list java* 查看yum存在的java库 ...

  7. MySQL-5-TCL,视图,变量,存储过程和函数,流程控制

    TCL:Transaction Control Language事务控制语言 TCL 事务的特点 acid: 原子性(Atomicity),一致性(Consistency),隔离性(isolation ...

  8. 深入理解 happens-before 原则

    在前面的文章中,我们深入了解了 Java 内存模型,知道了 Java 内存模型诞生的意义,以及其要解决的问题.最终我们知道:Java 内存模型就是定义了 8 个基本操作以及 8 个规则,只要遵守这些规 ...

  9. OpenSSF安全计划:SBOM将驱动软件供应链安全

    在 软件成分分析(SCA)一文中,我们简单提到软件物料清单(SBOM)在安全实践中的价值. 本期文章将带你深入了解 "SBOM 无处不在"计划是什么,以及 SBOM 对未来软件供应 ...

  10. Python selenium 实现大麦网自动购票过程

    一些无关紧要的哔哔: 大麦网是中国综合类现场娱乐票务营销平台,业务覆盖演唱会. 话剧.音乐剧.体育赛事等领域今天,我们要用代码来实现他的购票过程 开搞! 先来看看完成后的效果是怎么样的 开发环境 版 ...