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. Android中获取网络数据时的分页加载

    //此实在Fragment中实现的,黄色部分为自动加载,红色部分是需要注意的和手动加载,    蓝色部分是睡眠时间,自我感觉不用写  ,还有就是手动加载时,不知道为什么进去后显示的就是最后一行,求大神 ...

  2. VIJOS P1081 野生动物园 SBT、划分树模板

    [描述] cjBBteam拥有一个很大的野生动物园.这个动物园坐落在一个狭长的山谷内,这个区域从南到北被划分成N个区域,每个区域都饲养着一头狮子.这些狮子从北到南编号为1,2,3,…,N.每头狮子都有 ...

  3. CF 675 div2C 数学 让环所有值变为0的最少操作数

    http://codeforces.com/contest/675/problem/C 题目大意: 给一个环,标号为1-n,然后能从n回到1.让这个环的值为0,最少需要的操作数是多少? 这道题目呀.. ...

  4. kafka configuration

    http://kafka.apache.org/08/configuration.html

  5. android 手势识别学习

    引自http://www.cnblogs.com/android100/p/android-hand.html    http://blog.csdn.net/jiangshide/article/d ...

  6. Android Studio调试功能使用总结---转

    Android Studio调试功能使用总结[转]   这段时间一直在使用Intellij IDEA, 今天把调试区工具的使用方法记录于此. 先编译好要调试的程序. 1.设置断点 选定要设置断点的代码 ...

  7. [转]iptables

    iptables ptables简介 iptables是基于内核的防火墙,功能非常强大,iptables内置了filter,nat和mangle三张表. filter负责过滤数据包,包括的规则链有,i ...

  8. 关于css的伪类和伪元素

    现在才发现自己一直没有分清楚css的伪类和伪元素啊,so,总结一下. CSS 伪类用于向某些选择器添加特殊的效果. CSS 伪元素用于将特殊的效果添加到某些选择器. 可以明确两点,第一两者都与选择器相 ...

  9. ecos的mvcl

    m 数据模型抽象层 v 视图 c 控制器 l 业务逻辑 mvc与mvcl区别 mvc中的m是mvcl中m+l

  10. iOS之Alcatraz常见插件

    转发:http://blog.csdn.net/pingchangtan367/article/details/26689497 对于Xcode是我们的开发环境,ok,怎样提高我们的效率呢,当然是使我 ...