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. 思路:昨天比赛出的一道类似 ...
随机推荐
- 好客租房56-props深入(3props校验-约束规则)
1常见类型:Array,bool,func,number,object,string 2React:element 3必填项:isRequred 4特定结构的想:shape({}) //导入react ...
- Javabean使用实例
1.login.jsp <%@ page language="java" contentType="text/html; charset=utf-8" p ...
- 安装vsFTP到CentOS(YUM)
运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:vsftpd-3.0.2 硬件要求:无 安装过程 1.安装YUM-EPEL存储库 YUM-EPE ...
- vue大型电商项目尚品汇(前台篇)day04
这几天一直都在做项目,只是没有上传上来,即将把前台项目完结了.现在开始更新整个前台的部分 一.面包屑处理 1.分类操作 点击三级联动进入搜索产生面包屑,直接取参数中的name即可 点击x怎么干掉这个面 ...
- 零成本搭建个人博客之图床和cdn加速
本文属于零成本搭建个人博客指南系列 为什么要使用图床 博客文章中的图片资源文件一般采用本地相对/绝对路径引用,或者使用图床通过外链进行引用展示.本地引用的弊端我认为在于: 图片和博客放在同一个代码托管 ...
- CabloyJS 4.12震撼发布,及新版教程尝鲜
引言 凡是可以用 JavaScript 来写的应用,最终都会用 JavaScript 来写 | Atwood 定律 目前市面上出现的大多数与 NodeJS 相关的框架,基本都将 NodeJS 定位在工 ...
- 七牛云创建存储空间并绑定自定义域名-https协议
七牛云创建存储空间并绑定自定义域名-https协议 一.准备 0.绑定自定义域名的前提:你起码拥有过一个备案过的域名[一级域名] 1.在七牛云创建一个存储空间 2.存储空间绑定自定义域名(cdn加速) ...
- BUUCTF-刷新过的图片
刷新过的图片 刷新在MISC中比较特殊,一般是指F5隐写方式 直接使用工具提取出来,发现生成的是Pk开头的,应该是zip格式 使用16进制确认了是ZIP,将生成的output.txt改为output. ...
- MySql查看索引以及各字段含义
查看表的索引: show index from userInfo(表名) show index from 数据库名.表名 查看某表某一列上的索引使用下面的SQL语句: show index from ...
- Tapdata 在线研讨会:实时数据同步应用场景及实现方案探讨
数字化时代的到来,企业业务敏捷度的提升,对传统的数据处理和可用性带来更高的要求,实时数据同步技术的发展,给基于数据的业务创新带来了更多的可能性.9月8日晚,Tapdata 联合MongoDB 中文社区 ...