# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
835590 | jmyszka2007 | Sequence (APIO23_sequence) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<class A, class B>
ostream& operator<<(ostream& o, const pair<A, B>& p) {return o << '(' << p.first << ", " << p.second << ')';}
template<size_t Index = 0, typename... Types>
ostream& printTupleElements(ostream& o, const tuple<Types...>& t) {if constexpr (Index < sizeof...(Types)){if(Index > 0){o << ", ";}o << get<Index>(t);printTupleElements<Index + 1>(o, t);}return o;}
template<typename... Types>
ostream& operator<<(ostream& o, const tuple<Types...>& t){o << "(";printTupleElements(o, t);return o << ")";}
template<class T>
auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o){o << '{';bool first = true;for (const auto& e : x){if (!first){o << ", ";}o << e;first = false;} return o << '}';}
//#define DEBUG
#ifdef DEBUG
#define fastio()
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#define check(x) if (!(x)) { cerr << "Check failed: " << #x << " in line " << __LINE__ << endl; exit(1); }
#else
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0);
#define debug(...)
#define check(x)
#endif
typedef long long ll;
#define pi pair<int, int>
#define pl pair<ll, ll>
#define st first
#define nd second
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
constexpr int base = (1 << 19);
int tri[2 * base][4];//tri[0] - minimum, tri[1] - maximum
int lazy[2 * base][4];//tri[0] - minimum, tri[1] - maximum
const int BUF_SZ = 1 << 15;
inline namespace Input {
char buf[BUF_SZ];
int pos;
int len;
char next_char() {
if (pos == len) {
pos = 0;
len = (int)fread(buf, 1, BUF_SZ, stdin);
if (!len) { return EOF; }
}
return buf[pos++];
}
int read_int() {
int x;
char ch;
int sgn = 1;
while (!isdigit(ch = next_char())) {
if (ch == '-') { sgn *= -1; }
}
x = ch - '0';
while (isdigit(ch = next_char())) { x = x * 10 + (ch - '0'); }
return x * sgn;
}
} // namespace Input
inline namespace Output {
char buf[BUF_SZ];
int pos;
void flush_out() {
fwrite(buf, 1, pos, stdout);
pos = 0;
}
void write_char(char c) {
if (pos == BUF_SZ) { flush_out(); }
buf[pos++] = c;
}
void write_int(int x) {
static char num_buf[100];
if (x < 0) {
write_char('-');
x *= -1;
}
int len = 0;
for (; x >= 10; x /= 10) { num_buf[len++] = (char)('0' + (x % 10)); }
write_char((char)('0' + x));
while (len) { write_char(num_buf[--len]); }
write_char('\n');
}
// auto-flush output when program exits
void init_output() { assert(atexit(flush_out) == 0); }
} // namespace Output
void spl(int v, int k) {
tri[2 * v][k] += lazy[v][k];
tri[2 * v + 1][k] += lazy[v][k];
lazy[2 * v][k] += lazy[v][k];
lazy[2 * v + 1][k] += lazy[v][k];
lazy[v][k] = 0;
}
void upd(int l, int r, int x, int k) {
l += base - 1;
r += base - 1;
while(l <= r) {
if(l & 1) {
tri[l][k] += x;
}
if(!(r & 1)) {
tri[r][k] += x;
}
if(l == r) {
l /= 2;
while(l > 0) {
tri[l][k] = min(tri[2 * l][k], tri[2 * l + 1][k]);
}
return;
}
l = (l + 1) / 2;
r = (r - 1) / 2;
}
}
int que(int v, int l, int r, int a, int b, int k) {
if(b < l || r < a) {
if(!k || k == 3) {
return 1e9;
}
else {
return -1e9;
}
}
if(a <= l && r <= b) {
return tri[v][k];
}
spl(v, k);
int mid = (l + r) / 2;
int ans;
if(!k || k == 3) {
ans = 1e9;
ans = min(ans, que(2 * v, l, mid, a, b, k));
ans = min(ans, que(2 * v + 1, mid + 1, r, a, b, k));
tri[v][k] = min(tri[2 * v][k], tri[2 * v + 1][k]);
}
else {
ans = -1e9;
ans = max(ans, que(2 * v, l, mid, a, b, k));
ans = max(ans, que(2 * v + 1, mid + 1, r, a, b, k));
tri[v][k] = max(tri[2 * v][k], tri[2 * v + 1][k]);
}
return ans;
}
void init() {
for(int i = 0; i < 2 * base; i++) {
tri[i][0] = 1e9;
tri[i][1] = -1e9;
tri[i][2] = -1e9;
tri[i][3] = 1e9;
}
}
int find(int v, int l, int r, int x, int k) {
if(l == r) {
return l;
}
spl(v, k);
int mid = (l + r) / 2;
if(!k) {
if(tri[2 * v][k] <= x) {
return find(2 * v, l, mid, x, k);
}
return find(2 * v + 1, mid +1 , r, x, k);
}
if(tri[2 * v][k] >= x) {
return find(2 * v, l, mid, x, k);
}
return find(2 * v + 1, mid +1 , r, x, k);
}
int sequence(int N, std::vector<int> A) {
init_output();
//ifstream cin("nazwa.in");
//ofstream cout("nazwa.out");
int n;
//n = read_int();
n = N;
vi tab(n + 1);
vector<vi> wys(n + 1);
int mx = 0;
init();
tri[base][0] = 0;
tri[base][1] = 0;
tri[base][2] = 0;
tri[base][3] = 0;
for(int i = 1; i <= n; i++) {
//tab[i] = read_int();
tab[i] = A[i];
wys[tab[i]].eb(i);
mx = max(mx, tab[i]);
tri[i + base][0] = -i;
tri[i + base][1] = -i;
tri[i + base][2] = -i;
tri[i + base][3] = -i;
}
for(int i = base - 1; i >= 1; i--) {
tri[i][0] = min(tri[2 * i][0], tri[2 * i + 1][0]);
tri[i][1] = max(tri[2 * i][1], tri[2 * i + 1][1]);
tri[i][2] = max(tri[2 * i][2], tri[2 * i + 1][2]);
tri[i][3] = min(tri[2 * i][3], tri[2 * i + 1][3]);
}
int ans = 0;
for(int i = 1; i <= mx; i++) {
for(auto x : wys[i]) {
upd(1, 0, base - 1, x, n, 2, 0);
upd(1, 0, base - 1, x, n, 2, 1);
}
//chce znalezc pierwszy <= que(1, 0, base - 1, r, n, 1) w tri[0]
//chce znalec pierwszy >= que(1, 0, base - 1, r, n, 3) w tri[2]
for(auto x : wys[i]) {
int ktmn = find(1, 1, base, que(1, 0, base - 1, x, n, 1), 0);
int ktmx = find(1, 1, base, que(1, 0, base - 1, x, n, 3), 2);
int kt = max(ktmn, ktmx);
auto it = lower_bound(all(wys[i]), kt);
auto it2 = lower_bound(all(wys[i]), x);
int tmp = (it2 - wys[i].begin());
tmp -= (it - wys[i].begin());
ans = max(ans, tmp + 1);
}
for(auto x : wys[i]) {
upd(1, 0, base - 1, x, n, 2, 2);
upd(1, 0, base - 1, x, n, 2, 3);
}
}
//write_int(ans);
return ans;
}
/*int main() {
fastio();
int t;
//cin >> t;
t = 1;
while(t--) {
solve();
}
}*/