题目链接: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. LA 2218 (半平面交) Triathlon

    题意: 有n个选手,铁人三项有连续的三段,对于每段场地选手i分别以vi, ui 和 wi匀速通过. 对于每个选手,问能否通过调整每种赛道的长度使得他成为冠军(不能并列). 分析: 粗一看,这不像一道计 ...

  2. [Sciter系列] MFC下的Sciter–1.创建工程框架

    Sciter SDK中提供的Win32下例程很多,唯独使用很多(对我个人而言)的MFC框架下Sciter程序的构建讲的很少,虽然MFC有这样那样的诟病,但是不可否认的是编写一般的小项目,这仍然是大多数 ...

  3. LeetCode Triangle 三角形(最短路)

    题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [], [,4], [6,,7], [4,,8,3] 注意: ...

  4. Android 下压缩图片—微弱失真

    Android下压缩图片的方法: 大概能将3M左右的图片压缩到100K左右, 几乎不失真. 代码如下: import java.io.FileNotFoundException; import jav ...

  5. OracleBulkCopy的批量数据导入

    private void button1_Click(object sender, EventArgs e) { OpenFileDialog afd = new OpenFileDialog(); ...

  6. 给定一个函数rand()能产生0到n-1之间的等概率随机数,问如何产生0到m-1之间等概率的随机数?

    题目:给定一个函数rand()能产生1到n之间的等概率随机数,问如何产生1到m之间等概率的随机数? 先把问题特殊化,例如原题变为给定一个函数rand5(),该函数可以随机生成1-5的整数,且生成概率一 ...

  7. hdu1998 bjfu1272奇数阶幻方构造

    这题就是一个sb题,本来很水,硬是说得很含混.奇数阶幻方构造其实有好多方法,这题既不special judge,也不说清楚,以为这样能把水题变成难题似的,简直想骂出题人. /* * Author : ...

  8. cocos2d-x 详解之 CCLayer(触摸事件)

    CCLayer继承自CCNode,在CCLayer中可以实现单点触摸.多点触摸和重力感应回调3种不同形式的交互.这部分的难点在于,当存在多个层都要去接收触摸时它的响应机制是如何处理的.了解内部的处理机 ...

  9. 读pomelo的教程-2

    下面从头到尾记录chat demo的Login的过程 client:点击login按钮,取得username和rid两个值 $("#login").click(function() ...

  10. [CODEVS1697]⑨要写信

    题目描述 Description 琪露诺(冰之妖精)有操控冷气的能力.能瞬间冻结小东西,比普通的妖精更危险.一直在释放冷气的她周围总是非常寒冷. 由于以下三点原因…… 琪露诺的符卡 冰符“Icicle ...