题目链接

给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 线段树的更多相关文章

  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. CodeForces 242E - XOR on Segment 二维线段树?

    今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...

  4. CodeForces 242E "XOR on Segment"(线段树)

    传送门 •题意 给你一个包含 n 个数的序列 a,定义序列上的两个操作: (1)$1,l,r\ :\ ans=\sum_{i=l}^{r}a_i$; (2)$2,l,r,x\ :\ \forall\ ...

  5. 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. 这题 ...

  6. XOR on segment(线段树区间异或更新)

    原题传送门 本题大意:给定n个数字和m个操作,操作共有两种,第一种是求解区间l到r上元素的和,第二种是将区间l到r的元素都异或一个x,作为某个位置的新值. 很容易想到线段树维护区间和,但是我们发现,在 ...

  7. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  8. luogu P2574 XOR的艺术 (线段树)

    luogu P2574 XOR的艺术 (线段树) 算是比较简单的线段树. 当区间修改时.\(1 xor 1 = 0,0 xor 1 = 1\)所以就是区间元素个数减去以前的\(1\)的个数就是现在\( ...

  9. CodeForces 516C Drazil and Park 线段树

    原文链接http://www.cnblogs.com/zhouzhendong/p/8990745.html 题目传送门 - CodeForces 516C 题意 在一个环上,有$n$棵树. 给出每一 ...

随机推荐

  1. vs2015 不支持javascript的智能提示高亮

    有些人安装了vs2015后发现居然不支持javascrpt的高亮功能,连工具-选项-文本编辑器里面的javascript也没有了,楼主也碰到这么个情况了,估计是有与装了多个版本的原因,楼主电脑安装了V ...

  2. Sizzle一步步实现所有功能(一)

    前提: 1.HTML5自带querySelectAll可以完全替代Sizlle,所以我们下面写的Sizzle,是不考虑QSA的. 2.作者考虑了大量兼容情况,比如黑莓4.6系统这样几乎接触不到的bug ...

  3. SPRING 配置文件和类名

    今天写项目碰到一个很奇怪的问题,无论怎么改,还是一直包空指针 最终的问题出现在spring配置文件

  4. 关于新装ubuntu系统update失败和build-essential失败的解决办法

    我是12月4日在新电脑上的vmware-workstation 10 上安装的ubuntu14.04LTS,但安装后再校园环境下总是build-essential失败,上网一查,说是要先update, ...

  5. Win32应用程序的基本结构

    0 引言 Win32 API是用于创建 Windows 应用程序的应用程序编程接口.通常情况下,一个Win32应用包含一下几个部分: 1)    应用程序入口: 2)    注册窗口类: 3)    ...

  6. css负边距自适应布局

    单列定宽单列自适应布局: <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> ...

  7. [LeetCode]题解(python):146-LRU Cache

    题目来源: https://leetcode.com/problems/lru-cache/ 实现一个LRU缓存.直接上代码. 代码(python): class LRUCache(object): ...

  8. 在VHDL中,“传输延迟”和“惯性延迟”

    传输延迟就是最容易理解的从输入变化到输出变化之间的延迟.对应语法是transport例如 b <= transport a after 20ns 惯性延迟考虑了电容效应,即如果输入是(相对)窄的 ...

  9. sql查询数据库表中重复记录方法

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 代码如下: select * from people where peopleId in (select peopleId ...

  10. mysql timestamp 值不合法问题

    Create Table: CREATE TABLE `RecruitmentDesc` ( `sn` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号(自增字段 ...