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 "sequence.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
struct node
{
int lb, rb;
node(int _lb = 1e9, int _rb = -1e9)
{
lb = _lb;
rb = _rb;
}
};
node merge_node(node lf, node rf)
{
node nd;
nd.lb = min(lf.lb, rf.lb);
nd.rb = max(lf.rb, rf.rb);
return nd;
}
node tree[4 * maxn];
int lazy_exp[4 * maxn], lazy_shift[4 * maxn];
void build_tree(int root, int left, int right)
{
if (left == right)
{
tree[root] = node(left, left);
return;
}
int mid = (left + right) / 2;
build_tree(root * 2, left, mid);
build_tree(root * 2 + 1, mid + 1, right);
tree[root] = merge_node(tree[root * 2], tree[root * 2 + 1]);
}
void push_lazy(int root, int left, int right)
{
tree[root].lb -= lazy_exp[root];
tree[root].rb += lazy_exp[root];
tree[root].lb += lazy_shift[root];
tree[root].rb += lazy_shift[root];
if (left != right)
{
lazy_exp[2 * root] += lazy_exp[root];
lazy_exp[2 * root + 1] += lazy_exp[root];
lazy_shift[2 * root] += lazy_shift[root];
lazy_shift[2 * root + 1] += lazy_shift[root];
}
lazy_exp[root] = lazy_shift[root] = 0;
}
void update_exp(int root, int left, int right, int qleft, int qright, int val)
{
push_lazy(root, left, right);
if (left > qright || right < qleft)
return;
if (left >= qleft && right <= qright)
{
lazy_exp[root] += val;
push_lazy(root, left, right);
return;
}
int mid = (left + right) / 2;
update_exp(root * 2, left, mid, qleft, qright, val);
update_exp(root * 2 + 1, mid + 1, right, qleft, qright, val);
tree[root] = merge_node(tree[root * 2], tree[root * 2 + 1]);
}
void update_shift(int root, int left, int right, int qleft, int qright, int val)
{
push_lazy(root, left, right);
if (left > qright || right < qleft)
return;
if (left >= qleft && right <= qright)
{
lazy_shift[root] += val;
push_lazy(root, left, right);
return;
}
int mid = (left + right) / 2;
update_shift(root * 2, left, mid, qleft, qright, val);
update_shift(root * 2 + 1, mid + 1, right, qleft, qright, val);
tree[root] = merge_node(tree[root * 2], tree[root * 2 + 1]);
}
node query(int root, int left, int right, int qleft, int qright)
{
push_lazy(root, left, right);
if (left > qright || right < qleft)
return node();
if (left >= qleft && right <= qright)
return tree[root];
int mid = (left + right) / 2;
return merge_node(query(root * 2, left, mid, qleft, qright),
query(root * 2 + 1, mid + 1, right, qleft, qright));
}
vector < int > occ[maxn];
int val[maxn], pref[maxn];
int sequence(int N, vector<int> A)
{
A.insert(A.begin(), 0);
for (int i = 1; i <= N; i ++)
occ[A[i]].push_back(i);
build_tree(1, 0, N);
int ans = 0;
for (int x = 1; x <= N; x ++)
{
for (int i : occ[x - 1])
{
update_shift(1, 0, N, i, N, -1);
}
for (int i : occ[x])
{
update_shift(1, 0, N, i, N, -1);
}
/**for (int i = 1; i <= N; i ++)
{
if (A[i] < x)
val[i] = -1;
else
if (A[i] == x)
val[i] = 0;
else
val[i] = 1;
pref[i] = pref[i - 1] + val[i];
}*/
int beg = ans + 1;
for (int i = ans + 1; i < occ[x].size(); i ++)
update_exp(1, 0, N, occ[x][i], N, 1);
int i = 0;
while(i < occ[x].size())
{
int j = i + ans;
if (j >= occ[x].size())
break;
///cout << pref[r] << endl;
int l = occ[x][i], r = occ[x][j];
int min_lf = 1e9, max_lf = -1e9;
int min_rf = 1e9, max_rf = -1e9;
/**int d = 0;
for (int p = l - 1; p >= 0; p --)
{
if (A[p] == x)
d ++;
min_lf = min(min_lf, pref[p] - d);
max_lf = max(max_lf, pref[p] + d);
}
d = 0;
for (int p = r; p <= N; p ++)
{
if (A[p] == x && p != r)
d ++;
min_rf = min(min_rf, pref[p] - d);
max_rf = max(max_rf, pref[p] + d);
}*/
node lf = query(1, 0, N, 0, l - 1);
min_lf = lf.lb;
max_lf = lf.rb;
node rf = query(1, 0, N, r, N);
min_rf = rf.lb;
max_rf = rf.rb;
max_rf += (j - i + 1);
min_rf -= (j - i + 1);
/**cout << "-----------" << endl;
cout << x << " " << i << " " << j << " " << ans << endl;
cout << min_lf << " " << max_lf << " " << min_rf << " " << max_rf << endl;
cout << rf.lb << " : " << rf.rb << endl;*/
if (max(min_rf, min_lf) <= min(max_rf, max_lf))
{
///cout << x << " " << i << " " << j << endl;
ans ++;
}
else
{
update_exp(1, 0, N, 0, l, 1);
i ++;
}
if (j + 1 != occ[x].size())
update_exp(1, 0, N, occ[x][j + 1], N, -1);
}
for (int p = 0; p < i; p ++)
update_exp(1, 0, N, 0, occ[x][p], -1);
}
return ans;
}
Compilation message (stderr)
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:121:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
121 | for (int i = 1; i <= N; i ++)
| ^~~
sequence.cpp:124:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
124 | build_tree(1, 0, N);
| ^~~~~~~~~~
sequence.cpp:151:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
151 | for (int i = ans + 1; i < occ[x].size(); i ++)
| ~~^~~~~~~~~~~~~~~
sequence.cpp:154:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
154 | while(i < occ[x].size())
| ~~^~~~~~~~~~~~~~~
sequence.cpp:157:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
157 | if (j >= occ[x].size())
| ~~^~~~~~~~~~~~~~~~
sequence.cpp:203:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
203 | if (j + 1 != occ[x].size())
| ~~~~~~^~~~~~~~~~~~~~~~
sequence.cpp:150:9: warning: unused variable 'beg' [-Wunused-variable]
150 | int beg = ans + 1;
| ^~~
# | 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... |