Sol

线段树+Hash.

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

然后我想了一个虽然复杂度没变(因为我不会设计这个数据结构...) 但是好像有点用的算法,就是枚举中项,考虑从一个中项转移到另一个中项,那就是 \(\pm \Delta\) 就可以了...如果能够用数据结构维护这个操作,那就灰常好了.变换中项也就是变换折叠的位置吧.

标算呢...就是用线段树维护Hash,一个数字出现过,那就为1,没出现过就为0,维护一个正反序的01序列,如果一个数的 小于它正序01序列 和 大于它的反序01序列 异或值不为0那么就存在满足条件的等差序列.

01串太长了就可以用Hash来维护了...

PS:100000007不是质数...没开Long Long 直接见祖宗.

PS:我常数好大啊QAQ

Code

/**************************************************************
Problem: 2124
User: BeiYu
Language: C++
Result: Accepted
Time:2348 ms
Memory:8720 kb
****************************************************************/ #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; typedef long long LL;
const int N = 100005;
const LL p = 100000007;
#define debug(a) cout<<#a<<"="<<a<<" "
#define mid ((l+r)>>1)
#define lc (o<<1)
#define rc (o<<1|1) int T,n;
int a[N];
LL h1[N<<2],h2[N<<2],pow[N]; inline int in(int x=0,char ch=getchar()){ while(ch>'9'||ch<'0') ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x; } inline void Update(int x,int o,int l,int r){
if(l==r){ h1[o]=h2[o]=1;return; }
if(x<=mid) Update(x,lc,l,mid);
else Update(x,rc,mid+1,r);
h1[o]=(h1[lc]+h1[rc]*pow[mid-l+1])%p;
h2[o]=(h2[lc]*pow[r-mid]+h2[rc])%p;
}
inline LL Query1(int L,int R,int o,int l,int r){
if(L>R) return -1;
// debug(L),debug(R),debug(l),debug(r)<<endl;
if(L==l&&r==R) return h1[o];
if(L<=mid&&R>mid) return (Query1(L,mid,lc,l,mid)+Query1(mid+1,R,rc,mid+1,r)*pow[mid-L+1])%p;
if(L<=mid) return Query1(L,R,lc,l,mid)%p;
else return Query1(L,R,rc,mid+1,r)%p;
// debug(res)<<endl;
// return res;
}
inline LL Query2(int L,int R,int o,int l,int r){
if(L>R) return -1;
if(L==l&&r==R) return h2[o];
if(L<=mid&&R>mid) return (Query2(L,mid,lc,l,mid)*pow[R-mid]+Query2(mid+1,R,rc,mid+1,r))%p;
if(L<=mid) return Query2(L,R,lc,l,mid)%p;
else return Query2(L,R,rc,mid+1,r)%p;
// return res;
}
int main(){
pow[0]=1;for(int i=1;i<N;i++) pow[i]=(pow[i-1]<<1)%p;
for(T=in();T--;){
n=in();for(int i=1;i<=n;i++) a[i]=in();
int f=0;
memset(h1,0,sizeof(h1)),memset(h2,0,sizeof(h2));
for(int i=1;i<=n;i++){
int l=min(n-a[i],a[i]-1);
int u=Query2(a[i]-l,a[i]-1,1,1,n);
int v=Query1(a[i]+1,a[i]+l,1,1,n);
// cout<<"*********"<<endl;
// debug(i)<<endl;
// debug(l),debug(a[i])<<endl;
// debug(a[i]-l),debug(a[i]-1)<<endl;
// debug(a[i]+1),debug(a[i]+l)<<endl;
// cout<<u<<" "<<v<<endl;
if(u!=v){ f=1;break; }
Update(a[i],1,1,n);
}if(f) puts("Y");else puts("N");
}
return 0;
}

  

BZOJ 2124: 等差子序列的更多相关文章

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

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

  2. BZOJ 2124: 等差子序列 线段树维护hash

    2124: 等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一 ...

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

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

  4. bzoj 2124 等差子序列 树状数组维护hash+回文串

    等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 1919  Solved: 713[Submit][Status][Discuss] Desc ...

  5. 2124: 等差子序列 - BZOJ

    Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一个整数T,表示组数.下接 ...

  6. BZOJ [P2124] 等差子序列

    线段树维护哈希值 要求出现长度大于三的等差子序列,我们只要找到长度等于三的就可以了 初看本题没有思路,只能暴力枚举,O(n^4) 后来发现,这个序列是n的一个排列,那么每个数字都只会出现一次 我们可以 ...

  7. [bzoj2124]等差子序列(hash+树状数组)

    我又来更博啦     2124: 等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 941  Solved: 348[Submit][Statu ...

  8. bzoj2124 等差子序列(hash+线段树)

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

  9. BZOJ2124: 等差子序列(树状数组&hash -> bitset 求是否存在长度为3的等差数列)

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

随机推荐

  1. Ubuntu打开终端的方法三种

    这个不应该称得上是一个问题,但是,发现对于新手,确实是个难题因为少有人能够提到这一点,基本都是上来就直接讲用到的命令.我开始的时候也曾经被这个问题困扰着,后来,搜了一会儿才弄明白.1.在菜单内的附件上 ...

  2. 10月21日下午PHP常用函数

    函数四要素:返回类型  函数名  参数列表  函数体 //最简单的函数定义方式 function Show() { echo "hello"; } Show();//输出结果为he ...

  3. Java递归算法——阶乘

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...

  4. Java——文件选择框:JFileChooser

    import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import j ...

  5. php瀑布流,把一个数组分4个数组,按照时间排序

    简单介绍:把一个数组分成4个数组,取其中1的倍数 <?php $arr = array( ', ', ', ', ', ', ', ', ', ', ', ', ', ); foreach($a ...

  6. js随机生成N位数

    function RondomPass(number){ var arr = new Array; "); ;i<number;i++){ ); arr[i] =arr1[n] ; / ...

  7. Autofac.Integration.Web分析

    using System; using System.Web; using Autofac.Core.Lifetime; namespace Autofac.Integration.Web { /// ...

  8. Class.forName()用法详解

    Class.forName()用法详解 标签: classjvmjdbc数据库documentationjava 2012-03-29 09:39 40414人阅读 评论(8) 收藏 举报  分类: ...

  9. array_flip() array_merge() array+array的使用总结

    array_flip(array); //传递一个数组参数,对该数组的键.值进行翻转 例如: $a = array( 'a', 'b', 'c' ); print_r(array_flip($a)); ...

  10. python字符类型的一些方法

    python 字符串和字节互转换.bytes(s, encoding = "utf8") str(b, encoding = "utf-8") i.isspac ...