http://codeforces.com/problemset/problem/242/E

题意:给出初始n个数,还有m个操作,操作一种是区间求和,一种是区间xor x。

思路:昨天比赛出的一道类似题目,对于一个数,把它变成二进制,那么做xor操作的时候,其实如果那一位xor 1,那么就是取反,否则不变。于是,可以对每一个二进制位开一棵线段树,由于数字最大有1e6,所以只需要开log(1e6) = 20棵线段树。对每一棵线段树统计区间内1的个数,那一位对答案的贡献就是那一位的权值*区间1的个数。在作xor操作的时候,如果是xor的数1,那么就是区间内所有的1变成0,0变成1,这一段的和就变成(长度 - 原本的值),否则不变。

pushdown操作对右子树操作少了一个|1,导致调试好久,一定要细心!

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
#define N 100010
#define INF 0x3f3f3f3f
#define lson rt<<1, l, m
#define rson rt<<1|1, m+1, r
int tree[][N<<], lazy[][N<<], bit[];
LL weight[]; void pushup(int id, int rt) { tree[id][rt] = tree[id][rt<<] + tree[id][rt<<|]; } void pushdown(int id, int rt, int len, int l, int r) {
if(lazy[id][rt]) {
tree[id][rt<<] = (len - len / ) - tree[id][rt<<];
tree[id][rt<<|] = (len / ) - tree[id][rt<<|];
lazy[id][rt<<] ^= ; lazy[id][rt<<|] ^= ;
lazy[id][rt] = ;
}
} void update(int id, int rt, int l, int r, int L, int R, int w) {
if(L <= l && r <= R) {
if(w) tree[id][rt] = (r - l + - tree[id][rt]);
lazy[id][rt] ^= w;
return ;
}
pushdown(id, rt, r - l + , l, r);
int m = (l + r) >> ;
if(L <= m) update(id, lson, L, R, w);
if(m < R) update(id, rson, L, R, w);
pushup(id, rt);
} int query(int id, int rt, int l, int r, int L, int R) {
if(L <= l && r <= R) return tree[id][rt];
pushdown(id, rt, r - l + , l, r);
int m = (l + r) >> ;
int ans = ;
if(L <= m) ans += query(id, lson, L, R);
if(m < R) ans += query(id, rson, L, R);
return ans;
} int main()
{
int n, num, q, maxn = ;
scanf("%d", &n);
for(int i = ; i <= ; i++) weight[i] = 1LL << (i - );
for(int i = ; i <= n; i++) {
scanf("%d", &num);
int tmp = num, cnt = ;
while(tmp) {
bit[++cnt] = tmp & ;
tmp >>= ;
}
if(cnt > maxn) maxn = cnt;
for(int j = cnt; j >= ; j--) update(j, , , n, i, i, bit[j]);
}
scanf("%d", &q);
while(q--) {
int type, l, r, w;
scanf("%d%d%d", &type, &l, &r);
if(type == ) {
LL ans = ;
for(int i = ; i <= maxn; i++) {
int num = query(i, , , n, l, r);
ans += weight[i] * num;
}
printf("%I64d\n", ans);
} else {
scanf("%d", &w);
int cnt = ;
while(w) {
bit[++cnt] = w & ;
w >>= ;
}
if(cnt > maxn) maxn = cnt;
for(int j = cnt; j >= ; j--) update(j, , , n, l, r, bit[j]);
}
}
return ;
}

Codeforces 242E:XOR on Segment(位上的线段树)的更多相关文章

  1. codeforces 242E - XOR on Segment (线段树 按位数建树)

    E. XOR on Segment time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...

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

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

  3. codeforces 242E. XOR on Segment 线段树

    题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...

  4. CodeForces 242E - XOR on Segment 二维线段树?

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

  5. [Codeforces 464E] The Classic Problem(可持久化线段树)

    [Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...

  6. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  7. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  8. Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

    D. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  9. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

随机推荐

  1. 基于Sublime Text搭建Python IDE

    http://loosky.net/2967.html(包括SublimeREPL插件的安装和设置快捷键) SublimeCodeIntel,智能提示功能,查找自定义函数引用的快捷键--Alt+鼠标左 ...

  2. JS定时器的使用--延时提示框

    <title>无标题文档</title> <style> div{float:left;margin:10px;} #div1{width:50px; height ...

  3. hdu 1535 Invitation Cards(SPFA)

    Invitation Cards Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) T ...

  4. Excel工作表 表名导出

    Technorati 标签: microsoft office,vbs,excel   1: Attribute VB_Name = "表名导出" 2: Sub test() 3: ...

  5. 转:Eclipse Debug 界面应用详解——Eclipse Debug不为人知的秘密

    今天浏览csdn,发现一文详细的描述了Eclipse Debug中的各个知识点,非常详尽!特此记录. Eclipse Debug不为人知的秘密 http://blog.csdn.net/mgoann/ ...

  6. Apache的安装

    Apache的安装: 注:本例只截取需要注意的截图,其它默认则不显示. 1.       服务器信息可以按照默认配置,如果服务器的80端口没被其他服务器程序占据.可选“for All Users,on ...

  7. java工程开发之图形化界面之(第五课)

    下面我们将: 一)更加完整的解释Graphics类 二)使用方法来更清晰的重新编写前面图形小应用程序之一 三)介绍一些其他的绘图的方法 四)介绍方法init,它是类似于paint但是用于不同用途的另一 ...

  8. 单线驱动74hc595(转)

    源:http://blog.chinaunix.net/uid-10701701-id-91938.html 这个电路是国外一个哥们2009年的时候发表的. http://www.romanblack ...

  9. Abandoned country

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  10. Struts2.3.16日志(中)

    Result Configuration --Result 配置 当一个操作类方法完成后,它将返回一个字符串.字符串的值是用来选择一个元素的结果.一个操作映射的结果往往会有一组代表不同的可能的结果.一 ...