2124: 等差子序列

Description

给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列。

Input

输入的第一行包含一个整数T,表示组数。下接T组数据,每组第一行一个整数N,每组第二行为一个1到N的排列,数字两两之间用空格隔开。

Output

对于每组数据,如果存在一个等差子序列,则输出一行“Y”,否则输出一行“N”。

Sample Input

2
3
1 3 2
3
3 2 1

Sample Output

N
Y

HINT

对于100%的数据,N<=10000,T<=7

题解:

  题目说了是1到n的排列

  我们将1到n逐渐插入线段树的中,且用01表示出现状态

  假设当前x,我们只要找出与x相距距离相等的存在状态不同的就说明存在答案

  这就是线段树维护hash

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 10010
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define p 3
#define mod 1000000007
using namespace std;
typedef long long ll;
ll hash[N<<],hash2[N<<];
ll pow[N];
int a[N];
int n,t;
void pushup(int rt,int m)
{
ll tmp=m>>;
hash[rt]=(hash[rt<<]*pow[tmp]+hash[rt<<|])%mod;
hash2[rt]=(hash2[rt<<|]*pow[m-tmp]+hash2[rt<<])%mod;
}
void update(int pt,int l,int r,int rt)
{
if(l==r)
{
hash[rt]=hash2[rt]=;
return;
}
int mid=(l+r)>>;
if(pt<=mid)update(pt,lson);
else update(pt,rson);
pushup(rt,r-l+);
}
ll query(int L,int R,int l,int r,int rt)
{
if(L>R)return ;
int ans=;
if(L==l&&r==R)
{
return hash[rt];
}
int mid=(l+r)>>;
if(R<=mid)return query(L,R,lson);
else if(L>mid)return query(L,R,rson);
else return (query(L,mid,lson)*pow[R-mid]+query(mid+,R,rson))%mod;
}
ll query2(int L,int R,int l,int r,int rt)
{
if(L>R)return ;
if(L==l&&r==R)
{
return hash2[rt];
}
int mid=(l+r)>>;
if(R<=mid)return query2(L,R,lson);
else if(L>mid)return query2(L,R,rson);
else return (query2(L,mid,lson)+query2(mid+,R,rson)*pow[mid-L+])%mod;
}
int main()
{
scanf("%d",&t);
pow[]=p;
for(int i=;i<=;i++)pow[i]=(pow[i-]*p)%mod;
while(t--)
{
memset(hash,,sizeof(hash));
memset(hash2,,sizeof(hash2));
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
int flag=;
for(int i=;i<=n;i++)
{
ll len=min(a[i]-,n-a[i]);
ll tmp1=query(a[i]-len,a[i]-,,n,);
ll tmp2=query2(a[i]+,a[i]+len,,n,);
if(tmp1!=tmp2)
{
flag=;break;
}
update(a[i],,n,);
}
if(flag)printf("Y\n");
else printf("N\n");
}
}

BZOJ 2124: 等差子序列 线段树维护hash的更多相关文章

  1. bzoj 2124 等差子序列 (线段树维护hash)

    2124: 等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 1922  Solved: 714[Submit][Status][Discuss ...

  2. BZOJ 2124等差子序列 线段树&&hash

    [题目描述 Description] 给一个 1 到 N 的排列{Ai},询问是否存在 1<=p1<p2<p3<p4<p5<…<pLen<=N(Len& ...

  3. bzoj2124: 等差子序列线段树+hash

    bzoj2124: 等差子序列线段树+hash 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2124 思路 找大于3的等差数列其实就是找等于 ...

  4. BZOJ 2124: 等差子序列

    Sol 线段树+Hash. 首先暴力 等差子序列至少3项就可以了,就枚举中项,枚举公差就可以了,只需要一个数在中项前出现,另一个数在中项前没出现过就可以了.复杂度 \(O(n^2)\) 然后我想了一个 ...

  5. cf213E 线段树维护hash

    链接 https://codeforces.com/contest/213/problem/E 题目大意 给出两个排列a.b,长度分别为n.m,你需要计算有多少个x,使 得\(a_1 + x; a_2 ...

  6. MemSQL Start[c]UP 2.0 - Round 1 F - Permutation 思维+线段树维护hash值

    F - Permutation 思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个, 所以可以转换成,x+k, x-k在到x所在位置的时候是否 ...

  7. BZOJ2124:等差子序列(线段树,hash)

    Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pLen<=N (Len>=3), 使得A ...

  8. [bzoj2124]等差子序列——线段树+字符串哈希

    题目大意 给一个1到N的排列\(A_i\),询问是否存在\(p_i\),\(i>=3\),使得\(A_{p_1}, A_{p_2}, ... ,A_{p_len}\)是一个等差序列. 题解 显然 ...

  9. bzoj 4184: shallot (线段树维护线性基)

    题面 \(solution:\) 这一题绝对算的上是一道经典的例题,它向我们诠释了一种新的线段树维护方式(神犇可以跳过了).像这一类需要加入又需要维护删除的问题,我们曾经是遇到过的像莫对,线段树... ...

随机推荐

  1. vue插件 vue-seamless-scroll 无缝滚动插件ES6使用总结

    最近因为需求需要写一个项目信息无缝向上滚动组件,在网上搜了一下,看到大家的一致好评就果断的使用了vue-seamless-scroll组件.下面就简单的介绍它的使用,具体详细的使用推荐大家去看下开发者 ...

  2. Spring《八》AOP/代理类定义

    Spring通知 Interception Around通知 MethodInterceptor类(方法执行前后通知) Before通知 MethodBeforeAdvice类(方法执行前通知) Af ...

  3. Surround the Trees[HDU1392]

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. java MD5加密的工具类

    import java.security.MessageDigest; /** * MD5加密工具类 * @author zwq */ public class MD5Util { /** * MD5 ...

  5. JavaScript实现网页换肤

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  6. how does Array.prototype.slice.call() work?

    763 down vote accepted +50 What happens under the hood is that when .slice() is called normally, thi ...

  7. [转]Git入门与实践(一)

    git入门与实践(一) ·        March 10th, 2010 ·        Posted in UNIX环境编程 ·        By ghosTM55 Write comment ...

  8. js 把json字符串转为json对象

       <input type="hidden" name="data" id="data" value='[{"name&q ...

  9. linu问题集锦

    问题1 系统卡 慢 执行命令延迟/var/spool/mail下root文件过大导致/var磁盘空间92% cd / && du | sort -n | tail -n 10 查看排名 ...

  10. TensorFlow+实战Google深度学习框架学习笔记(11)-----Mnist识别【采用滑动平均,双层神经网络】

    模型:双层神经网络 [一层隐藏层.一层输出层]隐藏层输出用relu函数,输出层输出用softmax函数 过程: 设置参数 滑动平均的辅助函数 训练函数 x,y的占位,w1,b1,w2,b2的初始化 前 ...