题目
思路
本以为,核心在于减少判断次数(即,需要检查的三元组 ⟨ a , a + b 2 , b ⟩ \langle a,\frac{a+b}{2},b\rangle ⟨a,2a+b,b⟩ 较少)。
结果,原来是 暴力。永远不要忘记,同时判断多个等式,可能转化为字符串匹配从而 哈希。
本题中,对于某个 a i = v a_i=v ai=v,令 g ( a j ) = [ j < i ] g(a_j)=[j<i] g(aj)=[j<i],则 g ( a i + d ) ≠ g ( a i − d ) g(a_i{+}d)\ne g(a_i{-}d) g(ai+d)=g(ai−d) 时有解。
也就是 a i a_i ai 为中心时构成回文串。
哈希,线段树维护哈希值即可。时间复杂度 O ( n log n ) \mathcal O(n\log n) O(nlogn) 。
代码
#include <cstdio> // megalomaniac JZM yydJUNK!!!
#include <iostream> // Almighty XJX yyds!!!
#include <algorithm> // oracle: ZXY yydBUS!!!
#include <cstring> // DDG yyd Black Bridge!!!
#include <cctype> // decent XYX yydLONELY!!!
typedef long long llong;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
# define rep0(i,a,b) for(int i=(a); i!=(b); ++i)
inline int readint(){
int a = 0, c = getchar(), f = 1;
for(; !isdigit(c); c=getchar()) if(c == '-') f = -f;
for(; isdigit(c); c=getchar()) a = a*10+(c^48);
return a*f;
}
const int MAXN = 300005;
namespace zkw{
const int MOD = 1e9+7;
int v[MAXN*3][2], pow2[MAXN], ass;
void build(const int &n){
for(ass=1; ass<n+2; ass<<=1);
rep(i,pow2[0]=1,n) pow2[i] = (pow2[i-1]<<1)%MOD;
}
void modify(int i){
i ^= ass, v[i][0] = v[i][1] = 1;
for(int siz=1; i>>=1; siz<<=1){
v[i][0] = int((llong(v[i<<1][0])
*pow2[siz]+v[i<<1|1][0])%MOD);
v[i][1] = int((llong(v[i<<1|1][1])
*pow2[siz]+v[i<<1][1])%MOD);
}
}
int query0(int l, int r){
l = (l-1)^ass, r = (r+1)^ass; int lres = 0, rres = 0;
for(int siz=1,rsiz=0; true; l>>=1,r>>=1,siz<<=1){
if((l^r) == 1) return int((llong(lres)*pow2[rsiz]+rres)%MOD);
if(!(l&1)) lres = int((llong(lres)*pow2[siz]+v[l^1][0])%MOD);
if(!(r&1)) continue;
rres = int((llong(v[r^1][0])*pow2[rsiz]+rres)%MOD);
rsiz += siz; // current length
}
}
int query1(int l, int r){
l = (l-1)^ass, r = (r+1)^ass; int lres = 0, rres = 0;
for(int siz=1,lsiz=0; true; l>>=1,r>>=1,siz<<=1){
if((l^r) == 1) return int((llong(rres)*pow2[lsiz]+lres)%MOD);
if(r&1) rres = int((llong(rres)*pow2[siz]+v[r^1][1])%MOD);
if(l&1) continue;
lres = int((llong(v[l^1][1])*pow2[lsiz]+lres)%MOD);
lsiz += siz;
}
}
}
int main(){
int n = readint();
zkw::build(n);
for(int i=1,a; i<=n; ++i){
a = readint();
int len = std::min(n-a,a-1);
if(zkw::query0(a-len,a+len) != zkw::query1(a-len,a+len))
return void(puts("Yes")), 0;
zkw::modify(a);
}
puts("No");
return 0;
}