题目链接:http://codeforces.com/problemset/problem/242/E

给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x。

这题是线段树成段更新,但是不能直接更新,不然只能一个数一个数更新。这样只能把每个数存到一个数组中,长度大概是20吧,然后模拟二进制的位操作。仔细一点就行了。

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int MAXN = 1e5 + ;
typedef __int64 LL;
struct segtree {
int l , r , lazy , bit[];
}T[MAXN << ];
LL Res; void init(int p , int l , int r) {
int mid = (l + r) >> ;
T[p].r = r , T[p].l = l , T[p].lazy = ;
if(l == r) {
int num;
scanf("%d" , &num);
for(int i = ; i < ; i++) {
T[p].bit[i] = ((num & ) ? : );
num >>= ;
}
return ;
}
init(p << , l , mid);
init((p << )| , mid + , r);
for(int i = ; i < ; i++) {
T[p].bit[i] = T[p << ].bit[i] + T[(p << )|].bit[i];
}
} void updata(int p , int l , int r , int x) {
int mid = (T[p].l + T[p].r) >> ;
if(l == T[p].l && T[p].r == r) {
T[p].lazy ^= x;
for(int i = ; i < ; i++) {
if(x % )
T[p].bit[i] = (T[p].r - T[p].l + ) - T[p].bit[i];
x >>= ;
}
return ;
}
if(T[p].lazy) {
T[p << ].lazy ^= T[p].lazy;
T[(p << )|].lazy ^= T[p].lazy;
for(int i = ; i < ; i++) {
if(T[p].lazy & ) {
T[p << ].bit[i] = (T[p << ].r - T[p << ].l + ) - T[p << ].bit[i];
T[(p << )|].bit[i] = (T[(p << )|].r - T[(p << )|].l + ) - T[(p << )|].bit[i];
}
T[p].lazy >>= ;
}
}
if(r <= mid) {
updata(p << , l , r , x);
}
else if(l > mid) {
updata((p << )| , l , r , x);
}
else {
updata(p << , l , mid , x);
updata((p << )| , mid + , r , x);
}
for(int i = ; i < ; i++) {
T[p].bit[i] = T[p << ].bit[i] + T[(p << )|].bit[i];
}
} void query(int p , int l , int r) {
int mid = (T[p].l + T[p].r) >> ;
if(l == T[p].l && T[p].r == r) {
for(int i = ; i < ; i++) {
if(T[p].bit[i])
Res += (LL)T[p].bit[i] * (LL)( << i);
}
return ;
}
if(T[p].lazy) {
T[p << ].lazy ^= T[p].lazy;
T[(p << )|].lazy ^= T[p].lazy;
for(int i = ; i < ; i++) {
if(T[p].lazy & ) {
T[p << ].bit[i] = (T[p << ].r - T[p << ].l + ) - T[p << ].bit[i];
T[(p << )|].bit[i] = (T[(p << )|].r - T[(p << )|].l + ) - T[(p << )|].bit[i];
}
T[p].lazy >>= ;
}
}
if(r <= mid) {
query(p << , l , r);
}
else if(l > mid) {
query((p << )| , l , r);
}
else {
query(p << , l , mid);
query((p << )| , mid + , r);
}
for(int i = ; i < ; i++) {
T[p].bit[i] = T[p << ].bit[i] + T[(p << )|].bit[i];
}
} int main()
{
int n , m , c , x , l , r;
scanf("%d" , &n);
init( , , n);
scanf("%d" , &m);
while(m--) {
scanf("%d" , &c);
if(c == ) {
scanf("%d %d" , &l , &r);
Res = ;
query( , l , r);
printf("%I64d\n" , Res);
}
else {
scanf("%d %d %d" , &l , &r , &x);
updata( , l , r , x);
}
}
}

Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)的更多相关文章

  1. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  2. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  3. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  4. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  5. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  6. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  7. Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)

    题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...

  8. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树

    E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...

  9. Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并

    题目链接:http://codeforces.com/contest/381/problem/E  E. Sereja and Brackets time limit per test 1 secon ...

随机推荐

  1. source insight快捷键及使用技巧

      source insight快捷键及使用技巧 退出程序                             : Alt+F4 重画屏幕                             ...

  2. UVALive 4287 Proving Equivalences(缩点)

    等价性问题,给出的样例为 a->b的形式,问要实现全部等价(即任意两个可以互相推出),至少要加多少个形如 a->b的条件. 容易想到用强连通缩点,把已经实现等价的子图缩掉,最后剩余DAG. ...

  3. uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)

    题目真心分析不出来.看了白书才明白,不过有点绕脑. 容易想到,把题目给的不相邻的关系,利用矩阵,反过来建图.既然是全部可行的关系,那么就应该能画出含奇数个点的环.求环即是求双连通分量:找出所有的双连通 ...

  4. apache开源项目 -- Tuscany

    tuscany是Apache组织关于SOA实现的一个开放源码的工程项目,目前处于孵化期阶段. 该项目主要基于SCA,SDO,DAS等技术上实现的. SCA 的基本概念以及 SCA 规范的具体内容并不在 ...

  5. 2013.11.15 初学ant构建

    该做的事情都差不多做完了,今天开始用ant构建,所以学了下ant,其实要不是因为ubuntu时不时的抽风我应该早就可以开始构建了,但重写的时候也想清楚了一些逻辑,优化了一些地方.下面是我这辈子写的第一 ...

  6. 【转】开始iOS 7中自动布局教程(一)

    原文网址:http://www.cocoachina.com/industry/20131203/7462.html 原文:Beginning Auto Layout Tutorial in iOS ...

  7. 树-哈夫曼树(Huffman Tree)

    概述 哈夫曼树:树的带权路径长度达到最小. 构造规则 1. 将w1.w2.-,wn看成是有n 棵树的森林(每棵树仅有一个结点): 2. 在森林中选出根结点的权值最小的两棵树进行合并,作为一棵新树的左. ...

  8. 关于如果修改 ie 浏览器 文本模式

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/html4/stric ...

  9. php上传文件时出现错误:failed to open stream: Permission denied

    尝试使用php写了一段小的上传程序,但是在使用的时候,在上传文件时出现这个错误,由于之前在写程序要读文件,曾经出现过这个问题,当时是因为要读的文件的权限不够,于是使用chmod 775 1.txt把文 ...

  10. 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇07:全屏炸弹》

    7.全屏炸弹 全屏炸弹概述: 为了增设游戏的趣味性,我们制作一个游戏的基本框架以外.还会增设一些其他的额外的功能.比如5秒无敌状态.冰冻效果等.下面咱们以消灭屏幕中所有炸弹为例,看除了碰撞可以触发事件 ...