题目链接: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. dp,px转换

    public static int dip2px(Context context, float dpValue) {        final float scale = context.getRes ...

  2. bzoj3747

    经典题,记录每个位置对应数下次出现的位置next[i] 每个位置维护当前左端点下到这个位置的和 随着左端点的右移一位到i+1,对[i+1,next[i]-1] 的影响是-a[i], [next[i], ...

  3. ASP.NET MVC 学习4、Controller中添加SearchIndex页面,实现简单的查询功能

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-method ...

  4. UVa 10892 (GCD) LCM Cardinality

    我一直相信这道题有十分巧妙的解法的,去搜了好多题解发现有的太过玄妙不能领会. 最简单的就是枚举n的所有约数,然后二重循环找lcm(a, b) = n的个数 #include <cstdio> ...

  5. UVa 10003 (可用四边形不等式优化) Cutting Sticks

    题意: 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用. 分析: d(i, j)表示切割第i个切点到第j个切点这段所需的最小费用.则有d(i, j) = ...

  6. UVa 699 The Falling Leaves

    题意:给出按先序输入的一颗二叉树,分别求出从左到右的相同横坐标上的节点的权值之和 递归建树,然后用sum数组分别统计每一个横坐标上的权值之和 感觉建树都在递归递归递归= =慢慢理解吧 #include ...

  7. VS2005中乱码问题

    VS2005打开某些文件(如.inc, js)的时候出现乱码: 解决方法: 工具 --> 选项 --> 文本编辑器 --> 将“自动检测不带签名的 UTF-8编码”选中保存即可. V ...

  8. UVALive 3661 Animal Run(最短路解最小割)

    题意:动物要逃跑,工作人员要截断从START(左上角)到END(右下角)的道路,每条边权表示拦截该条道路需要多少工作人员.问最少需要多少人才能完成拦截. 通俗地讲,就是把图一分为二所造成消耗的最小值. ...

  9. c++ 完成端口资料

    文章地址: http://blog.csdn.net/piggyxp/article/details/6922277 附件如下: word文档 PiggyIOCPServer_2008.rar Pig ...

  10. (function(){})()这个是什么?有不明觉厉的感觉么?

    今天在RunJs上看到一个人分享的一个jquery代码,写的是jquery弹性滑动效果.不过,看着看着,发现一句代码(function{})(),突然有种不明觉厉的感觉. 事实上,只是因为我们没有用过 ...