思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个,
所以可以转换成,x+k, x-k在到x所在位置的时候是否都出现,或者都不出现,即出现情况相等,我们可以
用线段树维护hash值的方式来判断所有x+k, x-k的出现情况是否都一样。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std; const int N = 3e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n;
ull Pow[N];
struct segmentTree {
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
struct info1 {
ull hs; int len;
info1 operator + (const info1 &rhs) {
return info1{hs+rhs.hs*Pow[len], len+rhs.len};
}
} a[N<<];
struct info2 {
ull hs; int len;
info2 operator + (const info2 &rhs) {
return info2{rhs.hs+hs*Pow[rhs.len], len+rhs.len};
}
} b[N<<];
void build(int l, int r, int rt) {
if(l == r) {
a[rt] = info1{, };
b[rt] = info2{, };
return;
}
int mid = l + r >> ;
build(lson); build(rson);
a[rt] = a[rt<<] + a[rt<<|];
b[rt] = b[rt<<] + b[rt<<|];
}
void update(int p, int l, int r, int rt) {
if(l == r) {
a[rt] = info1{, };
b[rt] = info2{, };
return ;
}
int mid = l + r >> ;
if(p <= mid) update(p, lson);
else update(p, rson);
a[rt] = a[rt<<] + a[rt<<|];
b[rt] = b[rt<<] + b[rt<<|];
}
info1 querya(int L, int R, int l, int r, int rt) {
if(l >= L && r <= R) return a[rt];
int mid = l + r >> ;
if(R <= mid) return querya(L, R, lson);
else if(L > mid) return querya(L, R, rson);
else return querya(L, R, lson) + querya(L, R, rson);
}
info2 queryb(int L, int R, int l, int r, int rt) {
if(l >= L && r <= R) return b[rt];
int mid = l + r >> ;
if(R <= mid) return queryb(L, R, lson);
else if(L > mid) return queryb(L, R, rson);
else return queryb(L, R, lson) + queryb(L, R, rson);
}
} seg; int main() {
for(int i=Pow[]=; i < N; i++) Pow[i]=Pow[i-]*;
scanf("%d", &n);
seg.build(, n, );
bool flag = false;
for(int i = ; i <= n; i++) {
int x; scanf("%d", &x);
int len = min(x-, n-x);
if(len && seg.querya(x-len, x-, , n, ).hs != seg.queryb(x+, x+len, , n, ).hs)
flag = true;
seg.update(x, , n, );
}
if(flag) puts("YES");
else puts("NO");
return ;
} /*
*/