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. 思路:昨天比赛出的一道类似 ...
随机推荐
- 817. Linked List Components - LeetCode
Question 817. Linked List Components Solution 题目大意:给一个链表和该链表元素组成的一个子数组,求子数组在链表中组成多少个片段,每个片段中可有多个连续的元 ...
- 好客租房24-react中的事件处理(事件绑定)
3.1事件绑定 React事件绑定语法和DOM事件语法相似 语法:on+事件名称={事件处理程序} 比如οnclick={()=>{}} //导入react import React f ...
- Python实现将csv文件转化为html文件
核心技术: Pandas 需要转化的csv文件(business.csv): 源代码: import pandas as pd f=pd.read_csv("business.csv&quo ...
- docker服务部署、迁移与备份、dockerfile、私有仓库
今日内容概要 服务部署 迁移与备份 dockerfile 私有仓库 内容详细 1.服务部署 # 装 mysql redis --->源码编译安装-->启 动 # 有了docker后,容器操 ...
- SQL中如何修改数据库名、表名、列名?
文章目录 1.SQL中如何修改数据库的名字? 2.SQL中如何修改表的名字? 3.SQL中如何修改列的名字? 4.SQL中如何修改列的数据类型?(未完成,待续) 1.SQL中如何修改数据库名? 语法 ...
- Asp.Net Core Identity 多数据库支持
Asp.Net Core Identity 是.Net自带的身份认证系统,支持用户界面 (UI) 登录功能,并且管理用户.密码.配置文件数据.角色.声明.令牌.电子邮件确认等等.使用Visual St ...
- ESXI系列问题整理以及记录——使用SSH为设备打VIB驱动包,同时提供一种对于ESXI不兼容螃蟹网卡(Realtek 瑞昱)的问题解决思路
对于ESXI不兼容螃蟹网卡的问题,这里建议购买一张博通的低端单口千兆网卡,先使用博通网卡完成系统部署,再按照下文方法添加螃蟹网卡的VIB驱动,最后拆除博通网卡. 螃蟹网卡VIB驱动包下载地址:http ...
- .NET 6.0.6 和 .NET Core 3.1.26、Visual Studio 2022 17.2 和 17.3 Preview 2 和 .NET 7.0 Preview 5 同时发布
Microsoft 昨天发布了适用于 .NET 6.0.6 和 .NET Core 3.1.26.NuGet.Visual Studio 2019 和 Visual Studio 2022 17.2 ...
- 龙芯发布 .NET 6 SDK 6.0.105-ea1 LoongArch64 版本
龙芯平台.NET,是龙芯公司基于开源社区.NET独立研发适配的龙芯版本,我们会长期进行安全更新和错误修复,并持续进行性能优化.社区.NET7版本开始已经原生支持LoongArch64架构源码.具备如下 ...
- SAP 实例- 下拉框
效果图 源代码 REPORT rsdemo_dropdown_listbox . DATA init. TABLES scarr. TABLES spfli. TABLES sflight. TABL ...