LOJ6287 诗歌
题意
给定一个排列 \(p\),求是否存在三元组 \((i, j, k)\) 且 \(1 \le i < j < k \le n\) 使得 \(p_i = p_j = p_j - p_k\)。
\(n \le 3 \times 10 ^ 5\)。
Sol
注意到如果对于 \(j\),对她有贡献的点对 \((i, k)\) 出现在 \(j\) 的两边就直接判完了,不难想到将下标 \(< j\) 的点在值域上赋为 \(0\),下标 \(> j\) 的赋为 \(1\),然后在值域上用两个树状数组哈希一下就做完了。
复杂度:\(O(n \log n)\)。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#define ull unsigned long long
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
void write(int x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
bool _stmer;
const int N = 3e5 + 5, bas = 131;
array <ull, N> idx;
namespace Bit1 {
int lowbit(int x) { return x & -x; }
array <ull, N> edge;
void modify(int x, ull y, int n) {
while (x <= n)
edge[x] += y, x += lowbit(x);
}
ull query(int x) {
ull ans = 0;
while (x)
ans += edge[x], x -= lowbit(x);
return ans;
}
} //namespace Bit1
namespace Bit2 {
int lowbit(int x) { return x & -x; }
array <ull, N> edge;
void modify(int x, ull y) {
while (x)
edge[x] += y, x -= lowbit(x);
}
ull query(int x, int n) {
ull ans = 0;
while (x <= n)
ans += edge[x], x += lowbit(x);
return ans;
}
} //namespace Bit2
array <int, N> s;
bool _edmer;
int main() {
cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n";
int n = read();
for (int i = 1; i <= n; i++) s[i] = read();
idx[0] = 1;
for (int i = 1; i <= n; i++)
idx[i] = idx[i - 1] * 131ull;
for (int i = 1; i <= n; i++) {
Bit1::modify(s[i], idx[n - s[i] + 1], n);
Bit2::modify(s[i], idx[s[i]]);
}
bool ans = 1;
for (int i = 1; i <= n; i++) {
Bit1::modify(s[i], -idx[n - s[i] + 1], n);
Bit2::modify(s[i], -idx[s[i]]);
int len = min(s[i], n - s[i] + 1), ls = s[i] - len + 1, rs = s[i] + len - 1;
ans &= ((Bit1::query(s[i]) - Bit1::query(ls - 1)) * idx[ls - 1] ==
(Bit2::query(s[i], n) - Bit2::query(rs + 1, n)) * idx[n - rs]);
/* cerr << (Bit1::query(s[i]) - Bit1::query(ls - 1) * idx[len]) << "@" << endl; */
}
if (ans) puts("NO");
else puts("YES");
return 0;
}