이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sequence.h"
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
const ll INF = 1'000'000'000;
struct segment_tree_min {
vector<ll> tree, m;
ll size;
segment_tree_min(ll n) {
size = 1;
while (size < n) {
size *= 2;
}
tree.resize(size * 2, 0);
m.resize(size * 2, 0);
}
void add(ll v, ll x) {
tree[v] += x;
m[v] += x;
}
void push(ll v) {
add(v * 2, m[v]);
add(v * 2 + 1, m[v]);
m[v] = 0;
}
ll get(ll l, ll r) {
return get(1, 0, size, l, r);
}
ll get(ll v, ll l, ll r, ll ql, ll qr) {
if (ql <= l && r <= qr) {
return tree[v];
}
if (qr <= l || r <= ql) {
return INF;
}
push(v);
ll mid = (l + r) / 2;
return min(get(v * 2, l, mid, ql, qr), get(v * 2 + 1, mid, r, ql, qr));
}
void add(ll l, ll r, ll x) {
add(1, 0, size, l, r, x);
}
void add(ll v, ll l, ll r, ll ql, ll qr, ll x) {
if (ql <= l && r <= qr) {
add(v, x);
return;
}
if (qr <= l || r <= ql) {
return;
}
push(v);
ll mid = (l + r) / 2;
add(v * 2, l, mid, ql, qr, x);
add(v * 2 + 1, mid, r, ql, qr, x);
tree[v] = min(tree[v * 2], tree[v * 2 + 1]);
}
};
struct segment_tree_max {
vector<ll> tree, m;
ll size;
segment_tree_max(ll n) {
size = 1;
while (size < n) {
size *= 2;
}
tree.resize(size * 2, 0);
m.resize(size * 2, 0);
}
void add(ll v, ll x) {
tree[v] += x;
m[v] += x;
}
void push(ll v) {
add(v * 2, m[v]);
add(v * 2 + 1, m[v]);
m[v] = 0;
}
ll get(ll l, ll r) {
return get(1, 0, size, l, r);
}
ll get(ll v, ll l, ll r, ll ql, ll qr) {
if (ql <= l && r <= qr) {
return tree[v];
}
if (qr <= l || r <= ql) {
return -INF;
}
push(v);
ll mid = (l + r) / 2;
return max(get(v * 2, l, mid, ql, qr), get(v * 2 + 1, mid, r, ql, qr));
}
void add(ll l, ll r, ll x) {
add(1, 0, size, l, r, x);
}
void add(ll v, ll l, ll r, ll ql, ll qr, ll x) {
if (ql <= l && r <= qr) {
add(v, x);
return;
}
if (qr <= l || r <= ql) {
return;
}
push(v);
ll mid = (l + r) / 2;
add(v * 2, l, mid, ql, qr, x);
add(v * 2 + 1, mid, r, ql, qr, x);
tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
}
};
struct segment_tree_sum {
vector<ll> tree;
ll size;
segment_tree_sum(ll n) {
size = 1;
while (size < n) {
size *= 2;
}
tree.resize(size * 2, 0);
}
ll get(ll l, ll r) {
return get(1, 0, size, l, r);
}
ll get(ll v, ll l, ll r, ll ql, ll qr) {
if (ql <= l && r <= qr) {
return tree[v];
}
if (qr <= l || r <= ql) {
return 0;
}
ll mid = (l + r) / 2;
return get(v * 2, l, mid, ql, qr) + get(v * 2 + 1, mid, r, ql, qr);
}
void add(ll x, ll y) {
add(1, 0, size, x, y);
}
void add(ll v, ll l, ll r, ll x, ll y) {
if (x < l || r <= x) {
return;
}
if (r - l == 1) {
tree[v] += y;
return;
}
ll mid = (l + r) / 2;
add(v * 2, l, mid, x, y);
add(v * 2 + 1, mid, r, x, y);
tree[v] = tree[v * 2] + tree[v * 2 + 1];
}
};
ll sequence(ll n, vector<ll> a) {
segment_tree_min sti(n), stir(n);
segment_tree_max sta(n), star(n);
segment_tree_sum st(n);
vector<vector<ll>> all(n);
for (ll i = 0; i < n; i++) {
sti.add(i, n, 1);
sta.add(i, n, 1);
stir.add(0, i + 1, 1);
star.add(0, i + 1, 1);
st.add(i, 1);
all[a[i] - 1].push_back(i);
}
ll ans = 0;
for (ll i = 0; i < n; i++) {
for (auto j : all[i]) {
sti.add(j, n, -1);
sti.add(j, n, -1);
stir.add(0, j + 1, -1);
star.add(0, j + 1, -1);
st.add(j, -1);
}
ll lst = 0, la = 0;
for (ll j = 0; j < all[i].size(); j++) {
ll x = all[i][j];
sti.add(x, n, 1);
sta.add(x, n, -1);
ll m = st.get(0, all[i][j]), m2 = st.get(all[i][j], n);
ll mmi = stir.get(lst, all[i][j] + 1) - m2 - m, mma = star.get(lst, all[i][j] + 1) - m2 - m;
ll l = la, r = all[i].size();
while (r - l > 1) {
ll mid = (l + r) / 2;
ll mi = sti.get(all[i][mid], n) + mmi, ma = sta.get(all[i][mid], n) + mma;
if ((mi <= -1 && ma >= -1) || (mi <= 0 && ma >= 0) || (mi <= 1 && ma >= 1)) {
l = mid;
} else {
r = mid;
}
}
lst = all[i][j] + 1;
ans = max(ans, r - j);
la = r;
}
for (auto j : all[i]) {
sti.add(j, n, -1);
sta.add(j, n, -1);
stir.add(0, j + 1, -1);
star.add(0, j + 1, -1);
st.add(j, -1);
}
}
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'll sequence(ll, std::vector<int>)':
sequence.cpp:181:34: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
181 | for (ll j = 0; j < all[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |