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。
这题是线段树成段更新,但是不能直接更新,不然只能一个数一个数更新。这样只能把每个数存到一个数组中,长度大概是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 (线段树成段更新+二进制)的更多相关文章
- 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. ...
- 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 ...
- 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 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- 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 ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- 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所 ...
- 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 ...
- 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 ...
随机推荐
- UVa 10285 Longest Run on a Snowboard【记忆化搜索】
题意:和最长滑雪路径一样, #include<iostream> #include<cstdio> #include<cstring> #include <c ...
- Java [Leetcode 338]Counting Bits
题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...
- Java [Leetcode 191]Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...
- <摘录>TS,PS,PES包格式
PES是打包过的ES,已经插入PTS和DTS,一般是一个pes包为一帧图像 PES包格式: PES再打包成TS流或PS流,往往一个PES会分存到多个ts包中, start_code: 0x00 00 ...
- Oracle中获取执行计划的几种方法分析
以下是对Oracle中获取执行计划的几种方法进行了详细的分析介绍,需要的朋友可以参考下 1. 预估执行计划 - Explain PlanExplain plan以SQL语句作为输入,得到这条S ...
- ORACLE CLIENT客户端安装步骤详解
下载地址: http://download.oracle.com/otn/nt/oracle11g/112010/win32_11gR2_client.zip 先将下载下来的ZIP文件解压,并运行se ...
- 一个响应式框架——agera
Google在上周开源了一个响应式框架——agera,相信它会慢慢地被广大程序员所熟知.我个人对这样的技术是很感兴趣的,在这之前也研究过RxJava,所以在得知Google开源了这样的框架之后第一时间 ...
- 新闻类App使用的组件
UI SlidingMenu:com.jeremyfeinstein.slidingmenu:滑动菜单 ActionBarSherlock:com.actionbarsherlock:Action B ...
- hadoop中日志聚集问题
遇到的问题: 当点击上面的logs时,会出现下面问题: 这个解决方案为: By default, Hadoop stores the logs of each container in the nod ...
- C++ primer里的template用法
来源:http://c.chinaitlab.com/cc/ccjq/200806/752604_2.html -- template 的用法 在程序设计当中经常会出现使用同种数据结构的不同实 ...