SPOJ-ANDROUND -线段树/与操作
ANDROUND - AND Rounds
You are given a cyclic array A having N numbers. In an AND round, each element of the array A is replaced by the bitwise AND of itself, the previous element, and the next element in the array. All operations take place simultaneously. Can you calculate A after K such AND rounds ?
Input
The first line contains the number of test cases T (T <= 50).
There follow 2T lines, 2 per test case. The first line contains two space seperated integers N (3 <= N <= 20000) and K (1 <= K <= 1000000000). The next line contains N space seperated integers Ai (0 <= Ai <= 1000000000), which are the initial values of the elements in array A.
Output
Output T lines, one per test case. For each test case, output a space seperated list of N integers, specifying the contents of array A after K AND rounds.
Example
Sample Input:
2
3 1
1 2 3
5 100
1 11 111 1111 11111 Sample Output:
0 0 0
1 1 1 1 1
#include<stdio.h>
#include<string.h>
#include<queue>
#include<bits/stdc++.h>
#include<algorithm>
#define MAX 400005
#define lc (id<<1)
#define rc ((id<<1)|1)
#define mid ((L+R)>>1)
using namespace std;
int res[(<<)+];
int a[];
void build(int id,int L,int R){
if(L==R){
res[id]=a[L];
return;
}
build(lc,L,mid);
build(rc,mid+,R);
res[id]=res[lc]&res[rc];
}
int query(int id,int L,int R,int l,int r){
if(R<=r&&L>=l){
return res[id];
}
if(r<=mid) return query(lc,L,mid,l,r);
else if(l>mid) return query(rc,mid+,R,l,r);
else return (query(lc,L,mid,l,r)&query(rc,mid+,R,l,r));
}
int main()
{
int n,m,c,t,i,j,k;
cin>>t;
while(t--){
cin>>n>>k;
cin>>a[];
int all=a[];
for(i=;i<=n;++i) scanf("%d",a+i),all&=a[i];
if(k*+>=n) {
for(i=;i<=n;++i)
printf("%d%c",all,i==n?'\n':' ');
continue;
}
build(,,n);
int l=n-k+,r=+k-;
for(i=;i<=n;++i){
printf("%d%c",l<=r?query(,,n,l,r):(query(,,n,,r)&query(,,n,l,n)),i==n?'\n':' ');
r++;
if(r==n+) r=;
l++;
if(l==n+) l=;
}
}
return ;
}
SPOJ-ANDROUND -线段树/与操作的更多相关文章
- SPOJ GSS3 线段树系列1
SPOJ GSS系列真是有毒啊! 立志刷完,把线段树搞完! 来自lydrainbowcat线段树上的一道例题.(所以解法参考了lyd老师) 题意翻译 n 个数, q 次操作 操作0 x y把 Ax 修 ...
- hdu 2871 线段树(各种操作)
Memory Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 线段树(区间操作) POJ 3325 Help with Intervals
题目传送门 题意:四种集合的操作,对应区间的01,问最后存在集合存在的区间. 分析:U T [l, r]填充1; I T [0, l), (r, N]填充0; D T [l, r]填充0; C T[0 ...
- POJ 3225 Help with Intervals --线段树区间操作
题意:给你一些区间操作,让你输出最后得出的区间. 解法:区间操作的经典题,借鉴了网上的倍增算法,每次将区间乘以2,然后根据区间开闭情况做微调,这样可以有效处理开闭区间问题. 线段树维护两个值: cov ...
- Tsinsen A1517. 动态树 树链剖分,线段树,子树操作
题目 : http://www.tsinsen.com/A1517 A1517. 动态树 时间限制:3.0s 内存限制:1.0GB 总提交次数:227 AC次数:67 平均分:49. ...
- hdu 4578 Transformation 线段树多种操作裸题
自己写了一个带结构体的WA了7.8次 但是测了几组小数据都对..感觉问题应该出在模运算那里.写完这波题解去对拍一下. 以后线段树绝不写struct!一般的struct都带上l,r 但是一条线段的长度确 ...
- HDU - 6315(2018 Multi-University Training Contest 2) Naive Operations (线段树区间操作)
http://acm.hdu.edu.cn/showproblem.php?pid=6315 题意 a数组初始全为0,b数组为1-n的一个排列.q次操作,一种操作add给a[l...r]加1,另一种操 ...
- HDU 4578——Transformation——————【线段树区间操作、确定操作顺序】
Transformation Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others)T ...
- HDU 3954 Level up(多颗线段树+lazy操作)
又是一开始觉得的水题,结果GG了好久的东西... 题意是给你n个英雄,每个英雄开始为1级经验为0,最多可以升到k级并且经验一直叠加,每一级都有一个经验值上限,达到就升级.接着给你两种操作:W li r ...
- SPOJ - GSS1 —— 线段树 (结点信息合并)
题目链接:https://vjudge.net/problem/SPOJ-GSS1 GSS1 - Can you answer these queries I #tree You are given ...
随机推荐
- HMM,MEMM,CRF模型的比较
本文参考自:http://blog.csdn.net/happyzhouxiaopei/article/details/7960876 这三个模型都可以用来做序列标注模型.但是其各自有自身的特点,HM ...
- nginx配置访问图片路径(windows)
简介 Nginx(("engine x")由俄罗斯的程序设计师Igor Sysoev所开发)是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.PO ...
- 跨域问题-jsonp
前端同源策略并不会拦截静态资源请求,那么就将接口伪装成资源,然后后端配合返回一个前端预定义的方法调用,将返回值放入调用该函数的形参即可 <!DOCTYPE html> <html l ...
- 145. Binary Tree Postorder Traversal(二叉树后序遍历)
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- Ubuntu离线安装Tensorflow keras
实验室的服务器木有连接外网,无法使用pip进行安装,真正意义上的离线安装需要很多安装依赖包,下载起来很麻烦,而且版本号还不一定对. 另一种方法是利用源代码编译安装,那样也需要安装依赖包,比较麻烦. 经 ...
- java中的重量级与轻量级概念
首先轻量级与重量级是一个相对的概念,主要是对应用框架使用方便性和所提供服务特性等方面做比较的. 比方说EJB就是一个重量级的框架,因为它对所编写的代码有限制,同时它也提供分布式等复杂的功能. 相比之下 ...
- EXTJS 下载
Extjs各版本的下载链接 Extjs的版本繁多,本文收集了Extjs各个版本的下载链接,包括官网和非官网的,以及各种汉化版api,欢迎大家下载分享. Extjs最新版下载链接:http://www. ...
- 《Java入门第三季》第一章 异常与异常处理
Java异常简介 1.Java异常的体系结构.万恶之源Throwable以及它的两个大儿子Mr.Error(程序终结者)和Mr.Exception(有大量儿子,包括不受查的RuntimeExcepti ...
- Codeforces679E. Bear and Bad Powers of 42
传送门 今天子帧的一套模拟题的T3. 考试的时候其实已经想到了正解了,但是一些地方没有想清楚,就没敢写,只打了个暴力. 首先考虑用线段树维护区间信息. 先把每个值拆成两个信息,一是距离他最近的且大于他 ...
- 【转】获取Windows系统明文密码神器
前序 电脑密码忘记了可以用本工具找回,前提是你能进入系统,例如本机保存了远程服务器登录的密码或借别人的电脑,而忘记了密码:mimikatz 2.0工具正好解决了你的问题. 工具下载 binaires ...