codeforces 242E. XOR on Segment 线段树
给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x。
异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵线段树就可以。
每一次异或, 对于给定的x, 如果x的第i位是1, 那么就将第i棵线段树在给定的区间内0,1翻转, 这是很基础的操作。
对于区间求和操作, 我们可以求出给定的区间, 从高位到低位, 每一位依次有多少个1, 然后就可以直接求出来, 感觉不好表达....具体看代码。
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e5+;
int sum[maxn<<][], XOR[maxn<<][], a[];
void pushUp(int rt, int pos) {
sum[rt][pos] = sum[rt<<][pos] + sum[rt<<|][pos];
}
void pushDown(int rt, int m, int pos) {
if(XOR[rt][pos]) {
sum[rt<<][pos] = (m-(m>>)) - sum[rt<<][pos];
sum[rt<<|][pos] = (m>>)-sum[rt<<|][pos];
XOR[rt<<][pos] ^= ;
XOR[rt<<|][pos] ^= ;
XOR[rt][pos] = ;
}
}
void update(int L, int R, int l, int r, int rt, int pos) {
if(L<=l&&R>=r) {
XOR[rt][pos] ^= ;
sum[rt][pos] = r-l+-sum[rt][pos];
return ;
}
pushDown(rt, r-l+, pos);
int m = l+r>>;
if(L<=m)
update(L, R, lson, pos);
if(R>m)
update(L, R, rson, pos);
pushUp(rt, pos);
}
int query(int L, int R, int l, int r, int rt, int pos) {
if(L<=l&&R>=r) {
return sum[rt][pos];
}
pushDown(rt, r-l+, pos);
int m = l+r>>, ret = ;
if(L<=m)
ret += query(L, R, lson, pos);
if(R>m)
ret += query(L, R, rson, pos);
return ret;
}
int main()
{
int n, x, m, y, sign, z;
cin>>n;
mem(XOR);
for(int i = ; i<=n; i++) {
scanf("%d", &x);
for(int j = ; j<; j++) {
if((<<j)&x) {
update(i, i, , n, , j);
}
}
}
cin>>m;
while(m--) {
scanf("%d", &sign);
if(sign==) {
ll tmp = ;
scanf("%d%d", &x, &y);
for(int i = ; i>=; i--) {
tmp = 1ll*tmp*+query(x, y, , n, , i); //query是求这个区间里每一位有多少个1
}
cout<<tmp<<endl;
} else {
scanf("%d%d%d", &x, &y, &z);
for(int j = ; j<; j++) {
if(z&(<<j)) { //如果给出的x第j位是1, 那么就将区间内第j棵线段树翻转
update(x, y, , n, , j);
}
}
}
}
return ;
}
codeforces 242E. 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 ...
- CodeForces 242E - XOR on Segment 二维线段树?
今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...
- 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. 这题 ...
- XOR on segment(线段树区间异或更新)
原题传送门 本题大意:给定n个数字和m个操作,操作共有两种,第一种是求解区间l到r上元素的和,第二种是将区间l到r的元素都异或一个x,作为某个位置的新值. 很容易想到线段树维护区间和,但是我们发现,在 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- luogu P2574 XOR的艺术 (线段树)
luogu P2574 XOR的艺术 (线段树) 算是比较简单的线段树. 当区间修改时.\(1 xor 1 = 0,0 xor 1 = 1\)所以就是区间元素个数减去以前的\(1\)的个数就是现在\( ...
- CodeForces 516C Drazil and Park 线段树
原文链接http://www.cnblogs.com/zhouzhendong/p/8990745.html 题目传送门 - CodeForces 516C 题意 在一个环上,有$n$棵树. 给出每一 ...
随机推荐
- CSS3 border属性的妙用
.ribbon { background: #45c9c8; position: absolute; width: 75px; height: 25px; line-height: 25px; top ...
- MJExtension
MJExtension 长话短说下面我们通过一个列子来看下怎么使用 1. 先把框架拉进去你的项目 2. 首先我这次用到的json最外层是一个字典,根据数据的模型我们可以把这个归类为字典中有数组,数组中 ...
- Spring 整合hibernante 错误java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
1.将所需的jar包全部拷贝到WEB-INF/lib下,再重新启动tomcat便能顺利通过了.参考http://blessht.iteye.com/blog/1104450 最佳答案 spring ...
- jQuery中的.live()与die()
翻译原文地址:http://www.alfajango.com/blog/exploring-jquery-live-and-die/ 很多开发者都知道jQuery的.live()方法,他们大部分知道 ...
- nodeJs入门笔记(一)
node将"HTTP服务器"这一层抽离,直接面向浏览器用户 如PHP运行之前先要配置一个功能强大而复杂的HTTP 服务器,譬如Apache.IIS 或Nginx,还需要将PHP 配 ...
- 初识Sencha Touch:面板Panel
HTML代码: <!doctype html> <html> <head> <meta charset="utf-8"> <t ...
- the C programming language 阅读笔记2
1. 指针 1.1 自增符的使用 ++*p;//p指向的内容加一 (*p)++; //p指向的内容加一 *p++;//p本身自增 *++p; //p本身自增 因为诸如*和++这样的一元运算符在表达式求 ...
- Get请求出现乱码的解决方案
Get请求出现乱码,模拟一般出现的场景.场景一:超链接<a href=”url?name=张三&age=18”>场景二:window.opon(“url?name=张三&a ...
- android 环境搭建 windows, linux
android环境也搭建了很多次了,linux下window下.在这里记录下,以后再搭建设置变量啥的就直接看自己的博客就好了.电子挡笔记有时候也不方便 1.下载材料 概述:用的是比较简单的方式搭建环境 ...
- ARM Cortex M3(V7-M架构)硬件启动程序 二
解析 STM32 的启动过程 解析STM32的启动过程 当前的嵌入式应用程序开发过程里,并且C语言成为了绝大部分场合的最佳选择.如此一来main函数似乎成为了理所当然的起点——因为C程序往往从main ...