#ifndef LOCAL #pragma GCC optimize("O3") #endif #include using namespace std; #define sim template < class c #define ris return * this #define mor > muu & operator << ( #define R22(r) sim > typename \ enable_if<1 r sizeof dud(0), muu&>::type operator<<( c g) { sim > struct rge { c b, e; }; sim > rge range(c h, c n) { return {h, n}; } sim > auto dud(c * r) -> decltype(cerr << *r); sim > char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() { cerr << a.str() << endl; } R22(<) a << boolalpha << g; ris; } R22(==) ris << range(begin(g), end(g)); } sim, class b mor pair < b, c> r) { ris << "(" << r.first << ", " << r.second << ")"; } sim mor rge u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } #else sim mor const c&) { ris; } #endif muu & operator()() { ris; } }; #define imie(r...) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ") #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define REP(i,a) FOR(i,0,(a)-1) #define SIZE(a) (int)(a).size() #define ALL(a) (a).begin(),(a).end() #define ST first #define ND second #define PB push_back typedef vector VI; typedef pair PII; const int M = 1 << 18; PII dp[2 * M + 5]; void wstaw(int pos, int ile) { pos += M; dp[pos] = {ile, ile}; pos /= 2; while(pos) { dp[pos].ST = min(dp[2*pos].ST, dp[2 * pos + 1].ST); dp[pos].ND = max(dp[2*pos].ND, dp[2 * pos + 1].ND); pos /= 2; } } PII zapytaj(int pocz, int kon) { pocz += M; kon += M; PII res = dp[pocz]; if (dp[kon].ST < res.ST) res.ST = dp[kon].ST; if (dp[kon].ND > res.ND) res.ND = dp[kon].ND; while (pocz / 2 != kon / 2) { if (pocz % 2 == 0) { res.ST = min(res.ST, dp[pocz + 1].ST); res.ND = max(res.ND, dp[pocz + 1].ND); } if (kon % 2 == 1) { res.ST = min(res.ST, dp[kon - 1].ST); res.ND = max(res.ND, dp[kon - 1].ND); } pocz /= 2; kon /= 2; } return res; } vector> daj_sety(int n, const VI& v) { set secik; secik.insert(v[0]); vector> sety(n); FOR(i, 1, n - 1) { auto it = secik.begin(); int mini = *it; it = prev(secik.end()); int maksi = *it; it = secik.lower_bound(v[i] + 1); int first_larger = -1; while (it != secik.end() && *it <= v[i]) it++; if (it != secik.end()) first_larger = *it; if (it == secik.end()) it--; int first_smaller = -1; while (it != secik.begin() && *it >= v[i]) it--; if (*it < v[i]) first_smaller = *it; sety[i] = {mini, maksi, first_smaller, first_larger}; secik.insert(v[i]); } return sety; } int X, Y, Z; int ansy[500]; void update(int a, int b, int c) { X = 1; Y = 1; Z = 1; if (a > b) X++; if (a > c) X++; if (b > a) Y++; if (b > c) Y++; if (c > b) Z++; if (c > a) Z++; if (X + Y + Z == 5 && X == 3) X = 2; if (X + Y + Z == 5 && Y == 3) Y = 2; if (X + Y + Z == 5 && Z == 3) Z = 2; ansy[100 * X + 10 * Y + Z] = 1; } void cisnij_na_drzewie(int pocz, int kon, int val, int n) { if (pocz > 0) { auto p = zapytaj(0, pocz - 1); update(p.ST, val, val); update(p.ND, val, val); } if (kon < n - 1) { auto p = zapytaj(kon + 1, n - 1); update(val, val, p.ST); update(val, val, p.ND); } if (kon > pocz + 1) { auto p = zapytaj(pocz + 1, kon - 1); update(val, p.ST, val); update(val, p.ND, val); } } void jebaj_sety(int n, const VI& v, const vector>& x, const vector>& y) { debug << "jebaj sety"; FOR(i, 1, n - 2) { for (int a : x[i]) for (int b : y[i]) if (a > 0 && b > 0) { update(a, v[i], b); } } } void solve() { int n; cin >> n; VI v(n); map mapka; REP(i, n) { cin >> v[i]; mapka[v[i]].PB(i); wstaw(i, v[i]); } auto sety_left = daj_sety(n, v); reverse(ALL(v)); auto sety_right = daj_sety(n, v); reverse(ALL(v)); reverse(ALL(sety_right)); for (const auto& p : mapka) { if (SIZE(p.ND) < 2) continue; if (SIZE(p.ND) > 2) update(1, 1, 1); cisnij_na_drzewie(p.ND[0], p.ND[1], p.ST, n); cisnij_na_drzewie(p.ND[SIZE(p.ND) - 2], p.ND[SIZE(p.ND) - 1], p.ST, n); cisnij_na_drzewie(p.ND.front(), p.ND.back(), p.ST, n); } jebaj_sety(n, v, sety_left, sety_right); REP(j, 500) { int i = j; if (ansy[i]) { int a = i/100; i -= a * 100; int b = i/10; i -= b * 10; cout << a << b << i << "\n"; } } /* 5 1 2 3 4 5 jebaj_sety#153: 0 0 jebaj_sety#153: 1 4 jebaj_sety#154: 1 jebaj_sety#154: 1 jebaj_sety#154: -1 jebaj_sety#154: 1 jebaj_sety#153: 2 4 jebaj_sety#154: 1 jebaj_sety#154: 2 jebaj_sety#154: -1 jebaj_sety#154: 2 jebaj_sety#153: 3 4 jebaj_sety#154: 1 jebaj_sety#154: 3 jebaj_sety#154: -1 jebaj_sety#154: 3 jebaj_sety#153: 4 4 jebaj_sety#154: 1 jebaj_sety#154: 4 jebaj_sety#154: -1 jebaj_sety#154: 4 jebaj_sety#158: 0 4 jebaj_sety#159: 2 jebaj_sety#159: 5 jebaj_sety#159: -1 jebaj_sety#159: 2 jebaj_sety#158: 1 4 jebaj_sety#159: 3 jebaj_sety#159: 5 jebaj_sety#159: -1 jebaj_sety#159: 3 jebaj_sety#158: 2 4 jebaj_sety#159: 4 jebaj_sety#159: 5 jebaj_sety#159: -1 jebaj_sety#159: 2 jebaj_sety#158: 3 4 jebaj_sety#159: 5 jebaj_sety#159: 5 jebaj_sety#159: -1 jebaj_sety#159: 5 jebaj_sety#158: 4 0 jebaj_sety#162: jebaj sety update#114: 1 2 3 update#114: 1 2 5 update#114: 1 2 3 update#114: 1 2 3 update#114: 1 2 5 update#114: 1 2 3 update#114: 1 2 3 update#114: 1 2 5 update#114: 1 2 3 update#114: 1 3 4 update#114: 1 3 5 update#114: 1 3 2 update#114: 2 3 4 update#114: 2 3 5 update#114: 2 3 2 update#114: 2 3 4 update#114: 2 3 5 update#114: 2 3 2 update#114: 1 4 5 update#114: 1 4 5 update#114: 1 4 5 update#114: 3 4 5 update#114: 3 4 5 update#114: 3 4 5 update#114: 3 4 5 update#114: 3 4 5 update#114: 3 4 5 121 123 132 */ } int main() { ios_base::sync_with_stdio(0); solve(); }