codeforces 339C Xenia and Bit Operations(线段树水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Xenia the beginner programmer has a sequence a, consisting of 2n non-negative integers: a1, a2, ..., a2n. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.
Namely, it takes several iterations to calculate value v. At the first iteration, Xenia writes a new sequence a1 or a2, a3 or a4, ..., a2n - 1 or a2n, consisting of 2n - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence a. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is v.
Let's consider an example. Suppose that sequence a = (1, 2, 3, 4). Then let's write down all the transformations (1, 2, 3, 4) → (1 or 2 = 3, 3 or 4 = 7) → (3 xor 7 = 4). The result is v = 4.
You are given Xenia's initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additional mqueries. Each query is a pair of integers p, b. Query p, b means that you need to perform the assignment ap = b. After each query, you need to print the new value v for the new sequence a.
The first line contains two integers n and m (1 ≤ n ≤ 17, 1 ≤ m ≤ 105). The next line contains 2n integers a1, a2, ..., a2n (0 ≤ ai < 230). Each of the next m lines contains queries. The i-th line contains integers pi, bi (1 ≤ pi ≤ 2n, 0 ≤ bi < 230) — the i-th query.
Print m integers — the i-th integer denotes value v for sequence a after the i-th query.
- 2 4
1 6 3 5
1 4
3 4
1 2
1 2
- 1
3
3
3
For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation
线段树的入门题难度,往上维护的时候或与异或轮流搞。
- //#####################
- //Author:fraud
- //Blog: http://www.cnblogs.com/fraud/
- //#####################
- #include <iostream>
- #include <sstream>
- #include <ios>
- #include <iomanip>
- #include <functional>
- #include <algorithm>
- #include <vector>
- #include <string>
- #include <list>
- #include <queue>
- #include <deque>
- #include <stack>
- #include <set>
- #include <map>
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- #include <cstring>
- #include <climits>
- #include <cctype>
- using namespace std;
- #define XINF INT_MAX
- #define INF 0x3FFFFFFF
- #define MP(X,Y) make_pair(X,Y)
- #define PB(X) push_back(X)
- #define REP(X,N) for(int X=0;X<N;X++)
- #define REP2(X,L,R) for(int X=L;X<=R;X++)
- #define DEP(X,R,L) for(int X=R;X>=L;X--)
- #define CLR(A,X) memset(A,X,sizeof(A))
- #define IT iterator
- typedef long long ll;
- typedef pair<int,int> PII;
- typedef vector<PII> VII;
- typedef vector<int> VI;
- #define MAXN 200100
- int A[MAXN*];
- int main()
- {
- ios::sync_with_stdio(false);
- int n,m;
- cin>>n>>m;
- int t=<<n;
- for(int i=;i<t;i++)cin>>A[i+t];
- for(int i=n;i>;i--){
- int l=<<(i-),r=<<(i);
- if((n-i)&){
- while(l<r){
- A[l]=A[l<<]^A[l<<|];
- l++;
- }
- }else{
- while(l<r){
- A[l]=A[l<<]|A[l<<|];
- l++;
- }
- }
- }
- int u,v,x;
- for(int i=;i<m;i++){
- cin>>u>>v;
- u+=(<<n)-;
- x=;
- A[u]=v;
- u>>=;
- while(u){
- if(x){
- A[u]=A[u<<]|A[u<<|];
- }else{
- A[u]=A[u<<]^A[u<<|];
- }
- u>>=;
- x^=;
- }
- cout<<A[]<<endl;
- }
- return ;
- }
代码君
codeforces 339C Xenia and Bit Operations(线段树水题)的更多相关文章
- CodeForces 339D Xenia and Bit Operations (线段树)
题意:给定 2的 n 次方个数,对这些数两个两个的进行或运算,然后会减少一半的数,然后再进行异或运算,又少了一半,然后再进行或运算,再进行异或,不断重复,到最后只剩下一个数,要输出这个数,然后有 m ...
- POJ 3468 A Simple Problem with Integers(线段树水题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 135904 ...
- hdu 1754 I Hate It(线段树水题)
>>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<a ...
- CF 197 DIV2 Xenia and Bit Operations 线段树
线段树!!1A 代码如下: #include<iostream> #include<cstdio> #define lson i<<1 #define rson i ...
- 【wikioi】1191 数轴染色(线段树+水题)
http://wikioi.com/problem/1191/ 太水的线段树了,敲了10分钟就敲完了,但是听说还有一种并查集的做法?不明觉厉. #include <cstdio> #inc ...
- P1198 最大数 线段树水题
这道题模拟一下可以过,但是我们发现线段树也可以安全水过...... 写的线段树只需要滋磁单点修改,区间求max即可 我一开始犯了一个很SB的错误:每次插入修改了t,然后疯狂爆0到怀疑人生... 而且我 ...
- hdu - 1394 Minimum Inversion Number(线段树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...
- codevs 1690 开关灯 线段树水题
没什么好说的,标记put表示开关是否开着. #include<cstdio> #include<cstring> #include<algorithm> using ...
- [ACM_数据结构] Color the ball [线段树水题][数组开大]
Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次 ...
随机推荐
- tttt
while(scanf("%d",&n)!=EOF) { res=-1; level(tmp,n,res,1); printf("%d/n",res); ...
- UVa10651(记忆化搜索)
题意: 给一串-和o组成的字符串,你可以把“-oo"变成”o--",可以把“oo-”变成“--o",问最后最少有多少个o. 状态压缩 ,记忆化搜索 code: #incl ...
- mac下升级ruby环境版本
在ios开发中会经常使用到cocoapods来管理第三方框架,在安装cocoapods的时候会涉及到ruby环境,有时候会因为版本过低会导致安装失败,本文主要讲一下如何升级ruby环境 安装rvm,r ...
- struts2 使用jsonplugin
配置中的参数含义: root参数用于指定要序列化的根对象,如果省去这一配置,表示要序列化action中的所有属性 ignoreHierarchy 为false时表示要序列化根对象的所有基类 exclu ...
- 织梦dedecms5.7后台进去就卡死解决方法
症状:进入dede后台点击菜单后,浏览器进入假死状态要等好久才能反应过来. 解决方式:1.打开后台目录dede/templets/ 2.找到index_body.htm文件中的第25行至第35行部分屏 ...
- Python网络编程踩的坑
错误:socket.error: [Errno 10013] 原因:端口号被占用 解决:换其他的端口号或者将其他应用的端口号关闭 错误:File "D:/pyworkspace/homewo ...
- 开发反模式 - SQL注入
一.目标:编写SQL动态查询 SQL常常和程序代码一起使用.我们通常所说的SQL动态查询,是指将程序中的变量和基本SQL语句拼接成一个完整的查询语句. string sql = SELECT * FR ...
- VS2010下测试程序性能瓶颈
之前看到Qt的有个BUGreport https://bugreports.qt-project.org/browse/QTBUG-13182 这个BUG是在windows下QFileDialog很慢 ...
- 试用mysql的infobright引擎
海量数据分析处理,向来比较头疼费劲,特别是项目资金不允许的情况下,都是优先考虑开源软件,2007使用mysql,2009年尝试greenplum(公司最终选用oracle),20 ...
- JAVA中的正则表达式--待续
1.关于“\”,在JAVA中的正则表达式中的不同: 在其他语言中"\\"表示为:我想要在正则表达式中插入一个普通的反斜杠: 在Java中“\\”表示为:我想要插入一个正则表达式反斜 ...