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. paddlehub安装使用
  2. gentoo使用unicon支持中文
  3. Web应用_Tomcat部署及优化
  4. Oracle内部错误:ORA-07445[kcflfi()+466] [INT_DIVIDE_BY_ZERO]一例
  5. 前端学习(2387):组件库使用说明
  6. android 环形时间显示_使用Arduino构建OLED显示屏与Android手机接口的智能手表
  7. Css内边距与外边距
  8. 退出出库复核是什么意思_细思极恐!为什么是黄晓明退出而不是李菲儿?因为女方是芒果艺人...
  9. 无法启动SQL Server 2005中的SQL Serve(MSSQLSERVER)服务
  10. 为什么用Ghost备份后会有两个文件?
  11. Linux服务器安全之 fail2ban的安装与配置
  12. 一些有价值的数值公式(游戏)
  13. java 解析xml saxreader_Java中使用DOM和SAX解析XML文件的方法示例
  14. Servlet工作原理
  15. 有源滤波器: 基于UAF42的50Hz陷波器设计
  16. ROC(AUC)的显著性检验
  17. 泰坦尼克号数据分析报告
  18. PySpark---SparkSQL中的DataFrame(一)
  19. Java注释的重要性
  20. 基于 BK 树的中文拼写纠错候选召回

热门文章

  1. windows如何修改远程桌面连接端口
  2. Python中单下划线、双下划线,头尾双下划线说明
  3. Lake Shore—霍尔(磁性)传感器
  4. ubuntu 查看 cpu使用率(转载)
  5. LVGL笔记(6)-电子相册使用手势切换图片(windows仿真)
  6. 表面富集季胺盐交联/多乙烯多胺接枝改性/甲基咪唑氯修饰聚苯乙烯微球的研究和制备
  7. 樊登36个问题建立亲密关系_亚瑟阿伦的36个问题:打造亲密关系
  8. python之基础摘抄题
  9. Direct和XNA基础
  10. Verdi 使用教程(持续更新中)