题目链接: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. BZOJ_1627_[Usaco2007_Dec]_穿越泥地_(bfs)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1627 网格图,给出起点,终点,障碍,求最短路. 分析 简单的宽搜. #include < ...

  2. 手动配置gradle

    最近从github倒入项目,运行的特别慢gradle配置有问题,解决方法: 1.C:\android\demo\hellocharts-android-master\gradle\wrapper 目录 ...

  3. c++ 完成端口资料

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

  4. LeetCode Find Minimum in Rotated Sorted Array 旋转序列找最小值(二分查找)

    题意:有一个有序序列A,其内部可能有部分被旋转了,比如A[1...n]被转成A[mid...n]+A[1...mid-1],如果被旋转,只有这种形式.问最小元素是?(假设没有重复元素) 思路:如果是序 ...

  5. hdu 2204 Eddy's爱好

    // 一个整数N,1<=N<=1000000000000000000(10^18).// 输出在在1到N之间形式如M^K的数的总数// 容斥原理// 枚举k=集合{2,3,5,7,11,1 ...

  6. Android中ListView嵌套进ScrollView时高度很小的解决方案

    package com.example.test.util; import android.view.View; import android.view.ViewGroup; import andro ...

  7. request的用法

    Request从几个集合取数据是有顺序的,从前到后的顺序依次是 QueryString,Form,最后是ServerVariables.Request对象按照这样的顺序依次搜索这几个集合中的变量,如果 ...

  8. 【转】Android开发学习笔记:5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

  9. jsp防盗链代码

    // 禁止缓存   response.setHeader("Cache-Control", "no-store");   response.setHeader( ...

  10. Mem Cgroup目录无法清理问题分析

    http://blogs.360.cn/360xitong/2013/05/02/mem-cgroup%E7%9B%AE%E5%BD%95%E6%97%A0%E6%B3%95%E6%B8%85%E7% ...