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. loadrunner基本概念、安装及术语(一)

    一.初识loadrunner: LoadRunner,是一种预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问题,LoadRunner能够对整个企业架 ...

  2. java.lang.IllegalArgumentException: Can't convert argument: null

    出现这样的异常:: 这是由于eclipse在修改项目名的时候,eclipse自动更新部署了web.xml文件 并且重新生成了xml文件的头部声明. 新增了java的命名把这个javaee去掉就可以了. ...

  3. JNDI深入浅出

    1.什么是JNDI JNDI(The Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和 ...

  4. C# devExpress GridControl 统计行总数

    dev我不怎么会用,边学边记: 如果要在gridControl 页面底部统计记录总数只需两步: 1:设置显示gridControl页脚 2,.设置统计列: DevExpress.XtraGrid.Co ...

  5. linux常用命令 、查看日志、web排查

    linux常用命令 ps aux|grep xxx (比如 ps aux|grep tomcat ps aux|grep tomcat-portalvip ps aux|grep nginx 等) r ...

  6. ibdata1文件--缩小mysql数据库的ibdata1文件

    摘要 在MySQL数据库中,如果不指定innodb_file_per_table参数,单独存在每个表的数据,MySQL的数据都会存放在ibdata1文件. mysql ibdata1存放数据,索引等, ...

  7. acm的第一场比赛的总结

    6.4-6.5号很激动的去湖南湘潭打了一场邀请赛,这是第一次acm的旅程吧.毕竟大一上册刚开始接触c,然后现在就能抱着学长的大腿(拖着学长的后腿)打比赛,也是有一点小小的激动. 第一天很早就起床了,由 ...

  8. CGI接口原理及实现(转载)

    原文:http://blog.csdn.net/duola_rain/article/details/15812585 CGI接口原理及实现(2012-12-7 Over) 1.CGI定义: CGI( ...

  9. MyEclipse8.5 无法安装ADT解决办法

    打开MYECLIPSE.点击菜单栏的help ->my eclipse configure center .然后add site  指向 https://dl-ssl.google.com/an ...

  10. Android Fragment 真正的完全解析(下)---转

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和 ...