CF242E XOR on Segment
CF242E XOR on Segment
关于异或,无法运用懒标记实现区间异或;
可以像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的更多相关文章
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- codeforces 242E - XOR on Segment (线段树 按位数建树)
E. XOR on Segment time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...
- 「CF242E」XOR on Segment 解题报告
题面 长度为\(n\)的数列,现有两种操作: 1.区间异或操作 2.区间求和操作 对于每个查询,输出答案 思路: 线段树+二进制拆位 线段树区间修改一般使用的都是懒标记的方法,但是对于异或,懒标记的方 ...
- 线段树+二进制位拆分【CF242E】XOR on Segment
Description 给定一个长为\(n(n<=10^5)\)的数组 数组里的数不超过\(10^6\) 有两种操作: 1:求\(sum[l,r]\); 2:对\([l,r]\)中的所有数和\( ...
- CodeForces 242E "XOR on Segment"(线段树)
传送门 •题意 给你一个包含 n 个数的序列 a,定义序列上的两个操作: (1)$1,l,r\ :\ ans=\sum_{i=l}^{r}a_i$; (2)$2,l,r,x\ :\ \forall\ ...
- 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. 这题 ...
- CodeForces 242E - XOR on Segment 二维线段树?
今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...
- codeforces 242E. XOR on Segment 线段树
题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...
- Codeforces 242E:XOR on Segment(位上的线段树)
http://codeforces.com/problemset/problem/242/E 题意:给出初始n个数,还有m个操作,操作一种是区间求和,一种是区间xor x. 思路:昨天比赛出的一道类似 ...
随机推荐
- layui数据表格导入数据
作为一个后端程序员,前端做的确实很丑,所以就学习了一下layui框架的使用.数据表格主要的问题就是传输数据的问题,这里我用我的前后端代码来做一个实际的分解. 前端部分 可以到layui官网示例中找到数 ...
- 用python实现输入三边判断能否组成三角形
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'sanjiaoxing.py'## Creat ...
- SpringBoot 2.X 快速掌握
0.重写博文的原因 当初我的SpringBoot系列的知识是采用分节来写的,即:每一个知识点为一篇博文,但是:最近我霉到家了,我发现有些博文神奇般地打不开了,害我去找当初的markdown笔记,但是方 ...
- django框架2
内容概要 django小白必会三板斧 静态文件及相关配置 登录功能 静态文件 request对象方法 pycharm链接MySQL django链接MySQL django orm操作 django ...
- Ubuntu安装python固定版本
一. 安装python3.7 本篇文章使用python3.7安装步骤为例 1.直接使用apt-get安装python3.7 apt-get install python3.7 该方法经常会出现unab ...
- 解决maven依赖冲突,这篇就够了!
一.前言 什么是依赖冲突 依赖冲突是指项目依赖的某一个jar包,有多个不同的版本,因而造成了包版本冲突. 依赖冲突的原因 我们在maven项目的pom中 一般会引用许许多多的dependency.例如 ...
- 关于vue项目中搜索节流的实现
我们经常会遇到这种需求,现在我们在使用百度搜索的时候他们的思想也是根据防抖节流而实现的,至于用防抖还是节流根据自己需求. <template> <input type="t ...
- 记一次 .NET 某物管后台服务 卡死分析
一:背景 1. 讲故事 这几个月经常被朋友问,为什么不更新这个系列了,哈哈,确实停了好久,主要还是打基础去了,分析 dump 的能力不在于会灵活使用 windbg,而是对底层知识有一个深厚的理解,比如 ...
- UiPath保存图片操作的介绍和使用
一.保存图像 (Save Image)的介绍 可以将图像保存到磁盘的一种活动 二.保存图像 (Save Image)在UiPath中的使用 1. 打开设计器,在设计库中新建一个Sequence,为序列 ...
- linux下怎样在某个文件里面查找一个字符串?
方法一: grep命令 举个栗子:我想要在redis.conf中查询我设置的redis密码,执行下面代码 grep "require" redis.conf #grep " ...