codeforces242E XOR on Segment
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
题目链接:codeforces242E
正解:线段树
解题报告:
很快想出来之后写完就交,然后1A辣!
考虑这种和异或这类的位运算有关的题目,显然拆成位来考虑方便的多,需要资瓷区间修改、区间查询,那么就用线段树好了。
线段树上维护什么呢?
考虑把区间异或上$x$,如果$x$的第$i$位为$1$,相当于是把区间内所有的数的第$i$位从$1$变$0$,从$0$变$1$,显然我在线段树上维护区间内每一位的$1$、$0$出现次数就好了,修改的时候打个标记,把$0$、$1$的出现次数$swap$一下,查询的时候扫一遍算贡献。
//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
#include <bitset>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int MAXN = 100011;
int n,m,ql,qr,mo[45],CC,lin;
LL ans;
struct node{
int tag;
int s[21][2];
}a[MAXN*3]; inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void update(int root){
int lc=root<<1,rc=root<<1|1;
for(int i=20;i>=0;i--)
for(int j=0;j<2;j++)
a[root].s[i][j]=a[lc].s[i][j]+a[rc].s[i][j];
} inline void build(int root,int l,int r){
if(l==r) {
lin=getint();
for(int i=20;i>=0;i--) a[root].s[i][(lin>>i)&1]++;
return ;
}
int mid=(l+r)>>1,lc=root<<1,rc=root<<1|1;
build(lc,l,mid); build(rc,mid+1,r);
update(root);
} inline void pushdown(int root,int l,int r){
if(a[root].tag==0) return ; if(l==r) { a[root].tag=0; return ; }
lin=a[root].tag; a[root].tag=0; int lc=root<<1,rc=root<<1|1;
a[lc].tag^=lin; a[rc].tag^=lin;
for(int i=0;i<=20;i++)
if((lin>>i)&1) {
swap(a[lc].s[i][0],a[lc].s[i][1]);
swap(a[rc].s[i][0],a[rc].s[i][1]);
}
} inline void query(int root,int l,int r){
pushdown(root,l,r);
if(ql<=l && r<=qr) {
for(int i=0;i<=20;i++)
ans+=1LL*mo[i]*a[root].s[i][1];
return ;
}
int mid=(l+r)>>1,lc=root<<1,rc=root<<1|1;
if(ql<=mid) query(lc,l,mid); if(qr>mid) query(rc,mid+1,r);
} inline void modify(int root,int l,int r){
pushdown(root,l,r);
if(ql<=l && r<=qr) {
a[root].tag^=CC;
for(int i=0;i<=20;i++)
if((CC>>i)&1)
swap(a[root].s[i][0],a[root].s[i][1]);
return ;
}
int mid=(l+r)>>1,lc=root<<1,rc=root<<1|1;
if(ql<=mid) modify(lc,l,mid); if(qr>mid) modify(rc,mid+1,r);
update(root);
} inline void work(){
n=getint(); build(1,1,n);
for(int i=0;i<21;i++) mo[i]=(1<<i);
m=getint(); int type;
while(m--) {
type=getint(); ql=getint(); qr=getint();
if(type==1) {
ans=0;
query(1,1,n);
printf("%I64d\n",ans);
}
else {
CC=getint();
modify(1,1,n);
}
}
} int main()
{
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
codeforces242E XOR on Segment的更多相关文章
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- codeforces 242E - XOR on Segment (线段树 按位数建树)
E. XOR on Segment time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...
- CF242E XOR on Segment
CF242E XOR on Segment codeforces 洛谷 关于异或,无法运用懒标记实现区间异或: 可以像trie树一样拆位,将每个值拆成二进制数,对此建相应个数的线段树. 0 1与 0异 ...
- CodeForces 242E "XOR on Segment"(线段树)
传送门 •题意 给你一个包含 n 个数的序列 a,定义序列上的两个操作: (1)$1,l,r\ :\ ans=\sum_{i=l}^{r}a_i$; (2)$2,l,r,x\ :\ \forall\ ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- CodeForces 242E - XOR on Segment 二维线段树?
今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...
- codeforces 242E. XOR on Segment 线段树
题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...
- Codeforces 242E:XOR on Segment(位上的线段树)
http://codeforces.com/problemset/problem/242/E 题意:给出初始n个数,还有m个操作,操作一种是区间求和,一种是区间xor x. 思路:昨天比赛出的一道类似 ...
- XOR on segment(线段树区间异或更新)
原题传送门 本题大意:给定n个数字和m个操作,操作共有两种,第一种是求解区间l到r上元素的和,第二种是将区间l到r的元素都异或一个x,作为某个位置的新值. 很容易想到线段树维护区间和,但是我们发现,在 ...
随机推荐
- 关于DOM事件操作
事件的三要素: 事件源.事件.事件驱动程序. 事件源.: 引发后续事件的html标签 document.getElementById(“box”) document.getElementsByCl ...
- Python高级教程-多重继承
多重继承 继承是面向对象编程的一个重要的方式,因为通过继承,子类可以扩展父类的功能. Animal类的层次设计,假设要实现以下4中动物: Dog - 狗狗: Bat - 蝙蝠: Parrot - 鹦鹉 ...
- php实现异步的程序调用
浏览器和服务器之间的通信是基于HTTP协议进行链接通讯的,它是一种请求和相应的协议.浏览器通过URL向服务器发送请求,服务器接收到请求并执行请求,然后服务器将执行完成的数据返回到客户端. 这就存在一个 ...
- CCF 201312-4 有趣的数[dp][难]
问题描述 试题编号: 201312-4 试题名称: 有趣的数 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, ...
- 机器学习第2周---炼数成金-----线性回归与Logistic
重点归纳 回归分析就是利用样本(已知数据),产生拟合方程,从而(对未知数据)迚行预测用途:预测,判别合理性例子:利用身高预测体重:利用广告费用预测商品销售额:等等.线性回归分析:一元线性:多元线性:广 ...
- Git在Githib和Github上的使用
本文使用的环境是linux里 一.git的常用命令解释: 1.基础命令: git init #创建版本库 git add <file> #将文件修改添加到暂存区 git commit -m ...
- MySQL 温故知心(三)
MySQL锁概述 相对其他数据库而言,MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level loc ...
- web前端基础——初识JavaScript
1 JavaScript概述 JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚 ...
- java获得两个日期之间的所有月份
private static List<String> getMonthBetween(String minDate, String maxDate) throws ParseExcept ...
- 多媒体文件格式分析 MP3文件结构及编解码流程
多媒体文件格式分析 http://blog.csdn.net/taniya001/article/details/7962864 多媒体文件格式分析 MP3文件结构及编解码流程 http://www. ...