Submission #8391439
Source Code Expand
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
//#undef LOCAL
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
#ifdef LOCAL
struct PrettyOS {
ostream& os;
bool first;
template <class T> auto operator<<(T&& x) {
if (!first) os << ", ";
first = false;
os << x;
return *this;
}
};
template <class... T> void dbg0(T&&... t) {
(PrettyOS{cerr, true} << ... << t);
}
#define dbg(...) \
do { \
cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \
dbg0(__VA_ARGS__); \
cerr << endl; \
} while (false);
#else
#define dbg(...)
#endif
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
return os << "P(" << p.first << ", " << p.second << ")";
}
template <class T> ostream& operator<<(ostream& os, const V<T>& v) {
os << "[";
for (auto d : v) os << d << ", ";
return os << "]";
}
namespace fast {
// fast I/O by rsk0315 (update: 2019-08-08 07:02:47).
// This version supports only integer inputs/outputs, single character
// outputs, and string literal outputs.
static size_t constexpr buf_size = 1 << 17;
static size_t constexpr margin = 1;
static char inbuf[buf_size + margin] = {};
static __attribute__((aligned(8))) char minibuf[32];
static size_t constexpr int_digits = 20; // 18446744073709551615
static uintmax_t constexpr digit_mask = 0x3030303030303030;
static uintmax_t constexpr first_mask = 0x00FF00FF00FF00FF;
static uintmax_t constexpr second_mask = 0x0000FFFF0000FFFF;
static uintmax_t constexpr third_mask = 0x00000000FFFFFFFF;
static uintmax_t constexpr tenpow[] = {
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000
};
template <typename Tp>
using enable_if_integral = std::enable_if<std::is_integral<Tp>::value, Tp>;
class scanner {
char* pos = inbuf;
char* endpos = inbuf + buf_size;
void M_read_from_stdin() {
endpos = inbuf + fread(pos, 1, buf_size, stdin);
}
void M_reread_from_stdin() {
ptrdiff_t len = endpos - pos;
if (!(inbuf + len <= pos)) return;
memcpy(inbuf, pos, len);
char* tmp = inbuf + len;
endpos = tmp + fread(tmp, 1, buf_size-len, stdin);
*endpos = 0;
pos = inbuf;
}
public:
scanner() { M_read_from_stdin(); }
template <typename Integral,
typename enable_if_integral<Integral>::type* = nullptr>
void scan_parallel(Integral& x) {
// based on https://qiita.com/rsk0315_h4x/items/17a9cb12e0de5fd918f4
if (__builtin_expect(endpos <= pos + int_digits, 0))
M_reread_from_stdin();
bool ends = false;
typename std::make_unsigned<Integral>::type y = 0;
bool neg = false;
if (std::is_signed<Integral>::value && *pos == '-') {
neg = true;
++pos;
}
do {
memcpy(minibuf, pos, 8);
long c = *(long*)minibuf;
long d = (c & digit_mask) ^ digit_mask;
int skip = 8;
int shift = 8;
if (d) {
int ctz = __builtin_ctzl(d);
if (ctz == 4) break;
c &= (1L << (ctz-5)) - 1;
int discarded = (68-ctz) / 8;
shift -= discarded;
c <<= discarded * 8;
skip -= discarded;
ends = true;
}
c |= digit_mask;
c ^= digit_mask;
c = ((c >> 8) + c*10) & first_mask;
c = ((c >> 16) + c*100) & second_mask;
c = ((c >> 32) + c*10000) & third_mask;
y = y*tenpow[shift] + c;
pos += skip;
} while (!ends);
x = (neg? -y: y);
++pos;
}
template <typename Integral,
typename enable_if_integral<Integral>::type* = nullptr>
void scan_serial(Integral& x) {
if (__builtin_expect(endpos <= pos + int_digits, 0))
M_reread_from_stdin();
bool neg = false;
if (std::is_signed<Integral>::value && *pos == '-') {
neg = true;
++pos;
}
typename std::make_unsigned<Integral>::type y = *pos-'0';
while (*++pos >= '0') y = 10*y + *pos-'0';
x = (neg? -y: y);
++pos;
}
template <typename Integral,
typename enable_if_integral<Integral>::type* = nullptr>
void scan(Integral& x) { scan_parallel(x); }
};
static char outbuf[buf_size + margin] = {};
class printer {
char* pos = outbuf;
void M_flush_stdout() {
fwrite(outbuf, 1, pos-outbuf, stdout);
pos = outbuf;
}
public:
printer() = default;
~printer() { M_flush_stdout(); }
void print(char c) {
if (__builtin_expect(pos + 1 >= outbuf + buf_size, 0)) M_flush_stdout();
*pos++ = c;
}
template <size_t N>
void print(char const(&s)[N]) {
if (__builtin_expect(pos + N >= outbuf + buf_size, 0)) M_flush_stdout();
memcpy(pos, s, N-1);
pos += N-1;
}
template <typename Integral,
typename enable_if_integral<Integral>::type* = nullptr>
void print(Integral x) {
if (__builtin_expect(pos + int_digits >= outbuf + buf_size, 0))
M_flush_stdout();
if (x == 0) {
*pos++ = '0';
return;
}
if (x < 0) {
*pos++ = '-';
if (__builtin_expect(x == std::numeric_limits<Integral>::min(), 0)) {
switch (sizeof x) {
case 2: print("32768"); return;
case 4: print("2147483648"); return;
case 8: print("9223372036854775808"); return;
}
}
x = -x;
}
char* mp = minibuf + sizeof minibuf;
*--mp = x % 10 + '0';
size_t len = 1;
typename std::make_unsigned<Integral>::type y = x / 10;
while (y > 0) {
*--mp = y % 10 + '0';
y /= 10;
++len;
}
memcpy(pos, mp, len);
pos += len;
}
template <typename Tp>
void println(Tp x) { print(x), print('\n'); }
};
} // fast::
fast::scanner in;
//fast::printer out;
struct UnionFind {
V<int> p, r;
UnionFind(int n = 0) : p(n, -1) {}
void merge(int a, int b) {
int x = group(a), y = group(b);
if (x == y) return; // same
if (-p[x] < -p[y]) {
p[y] += p[x];
p[x] = y;
} else {
p[x] += p[y];
p[y] = x;
}
}
int group(int a) {
if (p[a] < 0) return a;
return p[a] = group(p[a]);
}
bool same(int a, int b) { return group(a) == group(b); }
};
struct StaticRangeUnion {
using P = pair<int, int>;
int n;
VV<P> ques;
StaticRangeUnion(int _n = 0) : n(_n), ques(n + 1) {}
void merge(int a, int b, int di) {
if (di == 0) return;
ques[min(di, n)].push_back({a, b});
}
UnionFind exec() {
UnionFind uf(n);
V<P> que, nque;
for (int di = n; di >= 1; di--) {
que.insert(que.end(), ques[di].begin(), ques[di].end());
nque.clear();
for (auto p: que) {
if (uf.same(p.first, p.second)) continue;
uf.merge(p.first, p.second);
nque.push_back({p.first + 1, p.second + 1});
}
swap(que, nque);
}
return uf;
}
};
int main() {
int n;
in.scan(n);
V<int> l(n);
for (int i = 0; i < n; i++) {
in.scan(l[i]);
}
StaticRangeUnion sru(n);
for (int i = 0; i < n; i++) {
sru.merge(0, n - 1 - i, l[i]);
}
auto uf = sru.exec();
for (int i = 0; i < n; i++) {
if (l[i] == i + 1) continue;
if (uf.same(l[i], n - 1 - i + l[i])) {
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
}
Submission Info
Submission Time |
|
Task |
D - LCP(prefix,suffix) |
User |
yosupo |
Language |
C++14 (GCC 5.4.1) |
Score |
1200 |
Code Size |
8338 Byte |
Status |
AC |
Exec Time |
42 ms |
Memory |
20076 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
1200 / 1200 |
Status |
|
|
Set Name |
Test Cases |
Sample |
00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 00_example_04.txt |
All |
00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 00_example_04.txt, 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt, 55.txt, 56.txt, 57.txt, 58.txt, 59.txt, 60.txt, 61.txt, 62.txt, 63.txt, 64.txt, 65.txt, 66.txt |
Case Name |
Status |
Exec Time |
Memory |
00_example_01.txt |
AC |
1 ms |
256 KB |
00_example_02.txt |
AC |
1 ms |
256 KB |
00_example_03.txt |
AC |
1 ms |
256 KB |
00_example_04.txt |
AC |
1 ms |
256 KB |
01.txt |
AC |
4 ms |
1280 KB |
02.txt |
AC |
10 ms |
3968 KB |
03.txt |
AC |
5 ms |
1920 KB |
04.txt |
AC |
42 ms |
13056 KB |
05.txt |
AC |
23 ms |
8960 KB |
06.txt |
AC |
33 ms |
12928 KB |
07.txt |
AC |
19 ms |
7680 KB |
08.txt |
AC |
41 ms |
12928 KB |
09.txt |
AC |
6 ms |
3756 KB |
10.txt |
AC |
15 ms |
10920 KB |
11.txt |
AC |
6 ms |
4220 KB |
12.txt |
AC |
13 ms |
10280 KB |
13.txt |
AC |
8 ms |
4940 KB |
14.txt |
AC |
14 ms |
11016 KB |
15.txt |
AC |
7 ms |
5292 KB |
16.txt |
AC |
18 ms |
13428 KB |
17.txt |
AC |
32 ms |
19072 KB |
18.txt |
AC |
16 ms |
11260 KB |
19.txt |
AC |
20 ms |
13812 KB |
20.txt |
AC |
17 ms |
11384 KB |
21.txt |
AC |
18 ms |
12268 KB |
22.txt |
AC |
17 ms |
12660 KB |
23.txt |
AC |
17 ms |
12624 KB |
24.txt |
AC |
21 ms |
13680 KB |
25.txt |
AC |
32 ms |
19072 KB |
26.txt |
AC |
14 ms |
11260 KB |
27.txt |
AC |
18 ms |
13812 KB |
28.txt |
AC |
16 ms |
11384 KB |
29.txt |
AC |
17 ms |
12268 KB |
30.txt |
AC |
16 ms |
12660 KB |
31.txt |
AC |
16 ms |
12624 KB |
32.txt |
AC |
19 ms |
13680 KB |
33.txt |
AC |
5 ms |
3756 KB |
34.txt |
AC |
13 ms |
10920 KB |
35.txt |
AC |
6 ms |
4220 KB |
36.txt |
AC |
12 ms |
10280 KB |
37.txt |
AC |
7 ms |
5068 KB |
38.txt |
AC |
12 ms |
11016 KB |
39.txt |
AC |
7 ms |
5292 KB |
40.txt |
AC |
16 ms |
13428 KB |
41.txt |
AC |
25 ms |
17008 KB |
42.txt |
AC |
24 ms |
15604 KB |
43.txt |
AC |
22 ms |
15216 KB |
44.txt |
AC |
24 ms |
20076 KB |
45.txt |
AC |
25 ms |
14456 KB |
46.txt |
AC |
22 ms |
17060 KB |
47.txt |
AC |
22 ms |
14324 KB |
48.txt |
AC |
28 ms |
13952 KB |
49.txt |
AC |
23 ms |
17008 KB |
50.txt |
AC |
24 ms |
15604 KB |
51.txt |
AC |
21 ms |
15216 KB |
52.txt |
AC |
23 ms |
18924 KB |
53.txt |
AC |
25 ms |
14456 KB |
54.txt |
AC |
21 ms |
17060 KB |
55.txt |
AC |
21 ms |
14324 KB |
56.txt |
AC |
28 ms |
13952 KB |
57.txt |
AC |
15 ms |
10236 KB |
58.txt |
AC |
14 ms |
9984 KB |
59.txt |
AC |
14 ms |
10108 KB |
60.txt |
AC |
14 ms |
9856 KB |
61.txt |
AC |
1 ms |
256 KB |
62.txt |
AC |
1 ms |
256 KB |
63.txt |
AC |
24 ms |
14456 KB |
64.txt |
AC |
21 ms |
17060 KB |
65.txt |
AC |
22 ms |
14324 KB |
66.txt |
AC |
28 ms |
13952 KB |