Little A has come to college and majored in Computer and Science.

Today he has learned bit-operations in Algorithm Lessons, and he got a problem as homework.

Here is the problem:

You are giving n non-negative integers  a1,a2,⋯,an a1,a2,⋯,an, and some queries.

A query only contains a positive integer p, which means you 
are asked to answer the result of bit-operations (and, or, xor) of all the integers except  ap ap.

Input

There are no more than 15 test cases.

Each test case begins with two positive integers n and p 
in a line, indicate the number of positive integers and the number of queries.

2≤n,q≤105 2≤n,q≤105

Then n non-negative integers  a1,a2,⋯,an a1,a2,⋯,an follows in a line,  0≤ai≤109 0≤ai≤109 for each i in range[1,n].

After that there are q positive integers  p1,p2,⋯,pq p1,p2,⋯,pqin q lines,  1≤pi≤n 1≤pi≤n for each i in range[1,q].

Output

For each query p, output three non-negative integers indicates the result of bit-operations(and, or, xor) of all non-negative integers except  ap ap in a line. 

Sample Input

3 3
1 1 1
1
2
3

Sample Output

1 1 0
1 1 0
1 1 0

题解:

题意:

给一堆数字,每次询问一个x,让你分别求除了下标为x的数字其他数字的与,或,异或运算的结果

思路:

如果x为1或者n,直接用线段树求[2,n]的结果或者[1,n-1]的结果

否则将区间分成两段,[1,x-1]和[x+1,n]然后这两段的结果进行要求的运算就好了

代码:

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
#include<algorithm>
using namespace std;
#define INF 100861111
#define ll long long
#define eps 1e-7
#define maxn 1e5+5
#define lson k*2
#define rson k*2+1
#define M (t[k].l+t[k].r)/2
struct node
{int l,r;int v1,v2,v3;
}t[100005*4];
int d1,d2,d3;//这三个为询问的结果,因为函数返回值只能有一个,所以直接定义成全局就好了
void pushup(int k)
{t[k].v1=t[lson].v1&t[rson].v1;t[k].v2=t[lson].v2|t[rson].v2;t[k].v3=t[lson].v3^t[rson].v3;
}
void Build(int l,int r,int k)
{t[k].l=l;t[k].r=r;if(l==r){scanf("%d",&t[k].v1);t[k].v2=t[k].v3=t[k].v1;return;}int mid=M;Build(l,mid,lson);Build(mid+1,r,rson);pushup(k);
}
void query(int l,int r,int k)
{if(t[k].l==l&&t[k].r==r){d1=t[k].v1;d2=t[k].v2;d3=t[k].v3;return;}int mid=M;if(r<=mid)query(l,r,lson);else if(l>mid)query(l,r,rson);else{int t1,t2,t3;query(l,mid,lson);t1=d1,t2=d2,t3=d3;//暂存一下query(mid+1,r,rson);d1&=t1;d2|=t2;d3^=t3;}
}
int main()
{int i,j,x,n,m,t1,t2,t3;while(scanf("%d%d",&n,&m)!=EOF){Build(1,n,1);for(i=0;i<m;i++){scanf("%d",&x);if(x==1||x==n)//假如x是端点的情况{if(x==1)query(2,n,1);elsequery(1,n-1,1);printf("%d %d %d\n",d1,d2,d3);continue;}query(1,x-1,1);//询问两边t1=d1;t2=d2;t3=d3;query(x+1,n,1);printf("%d %d %d\n",d1&t1,d2|t2,d3^t3);}}return 0;
}

HDU 6186 CS Course(线段树区间操作)相关推荐

  1. HDU - 3397 Sequence operation(线段树+区间合并)

    题目链接:点击查看 题目大意:给定一个初始的数列,然后依次进行m次操作: 0 a b:将闭区间[a,b]内的点都变为0 1 a b:将闭区间[a,b]内的点都变为1 2 a b:将闭区间[a,b]内的 ...

  2. HDU - 2871 Memory Control(线段树+区间合并)好题!

    题目链接:点击查看 题目大意:给定n个内存和m个操作,分别是: New x:从内存编号1开始向右查找,分配一个长度为x的空间,若找到输出区间的首地址,否则输出Reject New: Free x:释放 ...

  3. HDU - 1540 Tunnel Warfare(线段树+区间合并)

    题目链接:点击查看 题目大意:给定n个村庄,初始化全部连接为一条直线,需要依次执行m个操作,D表示摧毁第i个村庄的连接,R表示恢复最后一 个被摧毁的村庄的连接,Q表示询问包括本身在内,与第i个村庄相连 ...

  4. 吉首大学校赛 K 白山茶与红玫瑰 (线段树区间操作)

    链接:https://ac.nowcoder.com/acm/contest/925/K 来源:牛客网 题目描述 公元2019年6月22日,白山茶王国与红玫瑰王国展开大战,在世外仙境--天空花园处,双 ...

  5. HDU 1540 Tunnel Warfare 线段树区间合并

    Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...

  6. HDU - 3333 Turing Tree 线段树区间不同值和+详解+思想

    传送门 首先第一次做这种求不同元素和的线段树题,猜想是个裸题.但是题目中有一句话显然给题目降低了很大的难度,就是 想想其实它就是在暗示你这道题你要结合多次询问来处理,也就是所谓的离线,而不是一次一次的 ...

  7. hdu 1540 Tunnel Warfare(线段树区间合并)

    hdu 1540 Tunnel Warfare 记录每个节点的最大左连续值.最大右连续值.最大连续值,向上更新的是常规的区间合并处理方式 关键是想到如何去查询,如果查询节点落在左儿子节点的右连续段中, ...

  8. hdu 3966(树链剖分+线段树区间更新)

    传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...

  9. hdu 1698 Just a Hook 线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 Let us number the consecutive metallic sticks of ...

最新文章

  1. Python表白代码:“ 星光月夜烟花 皆归你,我也归你”
  2. 波士顿咨询:2018最具创新力企业50强
  3. AWS re-Invent最新发布AI产品解析:场景为王
  4. Transformer-XL解读(论文 + PyTorch源码)
  5. 06004_Redis的启动、使用和停止
  6. Ajax结合json在web中的应用
  7. workflow initialization in webclient ui - Remote call case
  8. ct报告就一定准确吗_晋州市人民医院检验报告出具时间提速啦!
  9. Redis:03---Redis的启动与配置参数大全
  10. 5个常用的htaccess技巧
  11. “Lephone.Data.DbEntry”的类型初始值设定项引发异常。(DbEntry.net3.9)
  12. python编写hadoop代码
  13. 漫话:如何给女朋友解释什么是 3PC?
  14. hls和modelsim进行联合仿真
  15. AVIator -- Bypass AV tool
  16. deb,命令行安装与软件中心安装有差异
  17. python数值类型和序列类型_数值类型和序列类型(python)
  18. 一个著名防外挂软件,下面转载一遍关于nProtect的破解
  19. Dictionary:MAC字典最简解决方案
  20. MQ 是什么?为什么使用?

热门文章

  1. Android app漏洞挖掘初探(0)
  2. 三步教会你旋转动画的制作
  3. 使用Sort 对文件排序详解
  4. 开发一个中小学生学习数学的软件,应该找谁调研
  5. 给行驶中的飞行器换引擎?应用现代化是怎么做到的
  6. MongoDB4.2.5安装
  7. Docker——容器虚拟化技术
  8. 12333提交显示服务器异常,ora-600[12333]错误小结及跟踪方法
  9. 新手小白怎么开快递驿站?
  10. mysql keyring file_详解MySQL 整表加密解决方案 keyring_file