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
    给出一个循环数组,定义一轮ADD操作之后a[i]=a[i-1]&a[i]&a[i+1] ,所有元素同时进行这个操作,询问k轮ADD之后数组的值。
一开始没想到ST,以为是简单的xjb。。
  我们可以发现一个规律就是a[i]进行k轮ADD后得值==(a[i-k-1]&.....&a[i-1])&a[i]&(a[i+1]&.....&a[i+k]),当k超过一个值之后,数组内的所有值都相同,就是a[1]&...&a[n],不难得出是k*2+1>=n时;
  对于另一种情况我们可以计算出k轮之后第一个数对应原数组的左右边界,然后进行&操作,如果循环计算的话会T,想到用ST将复杂度降到log级别就好了。

 #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 -线段树/与操作的更多相关文章

  1. SPOJ GSS3 线段树系列1

    SPOJ GSS系列真是有毒啊! 立志刷完,把线段树搞完! 来自lydrainbowcat线段树上的一道例题.(所以解法参考了lyd老师) 题意翻译 n 个数, q 次操作 操作0 x y把 Ax 修 ...

  2. hdu 2871 线段树(各种操作)

    Memory Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. 线段树(区间操作) 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 ...

  4. POJ 3225 Help with Intervals --线段树区间操作

    题意:给你一些区间操作,让你输出最后得出的区间. 解法:区间操作的经典题,借鉴了网上的倍增算法,每次将区间乘以2,然后根据区间开闭情况做微调,这样可以有效处理开闭区间问题. 线段树维护两个值: cov ...

  5. Tsinsen A1517. 动态树 树链剖分,线段树,子树操作

    题目 : http://www.tsinsen.com/A1517 A1517. 动态树 时间限制:3.0s   内存限制:1.0GB    总提交次数:227   AC次数:67   平均分:49. ...

  6. hdu 4578 Transformation 线段树多种操作裸题

    自己写了一个带结构体的WA了7.8次 但是测了几组小数据都对..感觉问题应该出在模运算那里.写完这波题解去对拍一下. 以后线段树绝不写struct!一般的struct都带上l,r 但是一条线段的长度确 ...

  7. 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,另一种操 ...

  8. HDU 4578——Transformation——————【线段树区间操作、确定操作顺序】

    Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others)T ...

  9. HDU 3954 Level up(多颗线段树+lazy操作)

    又是一开始觉得的水题,结果GG了好久的东西... 题意是给你n个英雄,每个英雄开始为1级经验为0,最多可以升到k级并且经验一直叠加,每一级都有一个经验值上限,达到就升级.接着给你两种操作:W li r ...

  10. SPOJ - GSS1 —— 线段树 (结点信息合并)

    题目链接:https://vjudge.net/problem/SPOJ-GSS1 GSS1 - Can you answer these queries I #tree You are given ...

随机推荐

  1. 【多线程基础】- 多个线程顺序打印ABC

    题目:3个线程名字分别是A,B,C 现在在console上连续打印10次 ABC . public class Test { public static void main(String[] args ...

  2. 使用Webdriver执行JS

    首先,我们使用如下方式初始化driver: WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptEx ...

  3. yii2 中where条件查询

    在Yii的Model里进行查询的时候 where是必不可少的. Where方法声明为 static where( $condition ) 其中参数 $condition 类型为字符串或者数组1.字符 ...

  4. 在Idea中连接数据库并生成实体类(mybatis逆向生成实体类)

    1.连接数据库 (1)按下图 ,  点击view-----选择tool windows----------选择database并点击 (2)弹出Database窗口 点击加号------------选 ...

  5. 4. Median of Two Sorted Arrays(2个有序数组的中位数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  6. 2018秦皇岛ccpc-camp Steins;Gate (原根+FFT)

    因为给定的模数P保证是素数,所以P一定有原根. 根据原根的性质,若\(g\)是\(P\)的原根,则\(g^k\)能够生成\([1,P-1]\)中所有的数,这样的k一共有P-2个. 则\(a_i*a_j ...

  7. netty9---使用编码解码器

    客户端: package com.client; import java.net.InetSocketAddress; import java.util.Scanner; import java.ut ...

  8. 3.1、Ubuntu系统中jmeter的安装和目录解析

    ​以下内容亲测,如果不对的地方,欢迎留言指正,不甚感激.^_^祝工作愉快^_^ Jmeter是一个非常好用的压力测试工具.  Jmeter用来做轻量级的压力测试,非常合适,只需要十几分钟,就能把压力测 ...

  9. js 判断是ie浏览器

    if (!+[1,]) { //IE window.event.returnValue = false; } else { return false; }

  10. spring项目gitignore

    target/ ### STS ### .apt_generated .classpath .factorypath .project .settings .springBeans ### Intel ...