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. 234. Palindrome Linked List - LeetCode

    Question 234. Palindrome Linked List Solution 题目大意:给一个链表,判断是该链表中的元素组成的串是否回文 思路:遍历链表添加到一个list中,再遍历lis ...

  2. 119_Power Pivot 长尾明细显示为【其他】

    博客:www.jiaopengzi.com 焦棚子的文章目录 请点击下载附件 一.背景 最近比较忙,太久不没有更新文章,确实没有好的素材,就写一个吧. 在关于产品数据分析的时候,我们经常关注的是主要的 ...

  3. 设计并实现大数类 BigNum

    学习任务:设计并实现大数类 BigNum 代码示例: import java.util.Scanner; public class BigNum { private double num; publi ...

  4. Redis快速度特性及为什么支持多线程及应用场景

    转载请注明出处: 目录 1.Redis 访问速度快特性 2.Redis 6.0 为什么支持多线程? 3.Redis可以做什么 3.1.缓存 3.2.排行榜系统 3.3.计数器应用 3.4.社交网络 3 ...

  5. ML第4周学习小结

    本周收获 总结一下本周学习内容: 1.学习了<深入浅出Pandas>的第五章:Pandas高级操作的两个内容 添加修改数据 高级过滤 我的博客链接: Pandas:添加修改.高级过滤 2. ...

  6. 『忘了再学』Shell基础 — 22、主要的环境变量配置文件说明

    目录 1.source命令 2.Linux系统中环境变量配置文件 (1)登录时生效的环境变量配置文件 (2)/etc/profile环境变量配置文件 (3)/etc/profile.d/*.sh环境变 ...

  7. .NET C#基础(6):命名空间 - 有名字的作用域

    0. 文章目的   面向C#新学者,介绍命名空间(namespace)的概念以及C#中的命名空间的相关内容. 1. 阅读基础   理解C与C#语言的基础语法.   理解作用域概念. 2. 名称冲突与命 ...

  8. Sublime text eslint windows 配置

    1. 下载安装eslint npm install -g eslint 2. 设置环境变量 C:\Users\<你的用户名>\AppData\Roaming\npm 3. sublime ...

  9. TDSQL|三篇论文入选国际顶会SIGMOD,厉害了腾讯云数据库

    好消息!6月13日,腾讯云数据库三篇论文再次入选数据库行业顶会SIGMOD,被SIGMOD 2022 Research Full Paper(研究类长文)收录. 本次被收录的研究成果中,新型数据结构设 ...

  10. Acwing785.快速排序

    Acwing785.快速排序 快排模板: y总教学大法好~: #include <iostream> using namespace std; const int N = 1000010; ...