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;
#define int long long
const int N = 5e5 + 5, INF = 1e18;
struct segtree
{
pair<int, int> seg[N * 4];
int lazy[N * 4];
void Build(int l, int r, int pos)
{
seg[pos] = {-r, -l};
if(l == r) return;
int mid = (l + r) / 2;
Build(l, mid, pos * 2);
Build(mid + 1, r, pos * 2 + 1);
}
void UpdateLazy(int l, int r, int pos)
{
if(l == r)
{
seg[pos].first += lazy[pos];
seg[pos].second += lazy[pos];
lazy[pos] = 0;
return;
}
seg[pos].first += lazy[pos];
seg[pos].second += lazy[pos];
lazy[pos * 2] += lazy[pos];
lazy[pos * 2 + 1] += lazy[pos];
lazy[pos] = 0;
}
void Update(int l, int r, int pos, int ql, int qval)
{
if(lazy[pos] != 0)
{
UpdateLazy(l, r, pos);
}
if(ql > r)
{
// cout << "Update(" << l << ", " << r << ") out of bounds\n";
return;
}
if(l == r)
{
// cout << "Update(" << l << ", " << r << ") total in [1]\n";
seg[pos].first += qval;
seg[pos].second += qval;
return;
}
if(l >= ql)
{
// cout << "Update(" << l << ", " << r << ") total in [2]\n";
seg[pos].first += qval;
seg[pos].second += qval;
lazy[pos * 2] += qval;
lazy[pos * 2 + 1] += qval;
return;
}
int mid = (l + r) / 2;
Update(l, mid, pos * 2, ql, qval);
Update(mid + 1, r, pos * 2 + 1, ql, qval);
// cout << "Update(" << l << ", " << r << ") partial\n";
seg[pos].first = min(seg[pos * 2].first, seg[pos * 2 + 1].first);
seg[pos].second = max(seg[pos * 2].second, seg[pos * 2 + 1].second);
}
pair<int, int> Query(int l, int r, int pos, int ql, int qr)
{
if(lazy[pos] != 0)
{
UpdateLazy(l, r, pos);
}
if(ql <= l && r <= qr)
{
return seg[pos];
}
if(ql > r || l > qr)
{
return {INF, -INF};
}
int mid = (l + r) / 2;
pair<int, int> p1 = Query(l, mid, pos * 2, ql, qr);
pair<int, int> p2 = Query(mid + 1, r, pos * 2 + 1, ql, qr);
return {min(p1.first, p2.first), max(p1.second, p2.second)};
}
} before, after;
int32_t sequence(int32_t n, vector<int32_t> a)
{
before.Build(1, n, 1);
after.Build(1, n, 1);
vector<int> occ[n + 1];
for(int i = 0; i < a.size(); i++)
{
occ[a[i]].push_back(i + 1);
}
int ans = 1;
for(int i = 1; i <= n; i++)
{
// cout << "i = " << i << endl;
for(int j: occ[i])
{
// cout << "hello" << endl;
before.Update(1, n, 1, j, 2);
}
for(int l = 0; l < occ[i].size(); l++)
{
int r = l + ans;
if(r >= occ[i].size())
{
break;
}
int afterRightMin = after.Query(1, n, 1, occ[i][r], n).first;
int afterLeftMax = after.Query(1, n, 1, 1, occ[i][l] - 1).second;
afterLeftMax = max(afterLeftMax, 0LL);
int beforeRightMax = before.Query(1, n, 1, occ[i][r], n).second;
int beforeLeftMin = before.Query(1, n, 1, 1, occ[i][l] - 1).first;
beforeLeftMin = min(beforeLeftMin, 0LL);
int afterAnswer = afterRightMin - afterLeftMax;
int beforeAnswer = beforeRightMax - beforeLeftMin;
// cout << beforeLeftMin << " " << afterLeftMax << " " << afterRightMin << " " << beforeRightMax << "\n";
if(afterAnswer <= 0 && beforeAnswer >= 0)
{
ans += 1;
l -= 1;
}
}
for(int j: occ[i])
{
after.Update(1, n, 1, j, 2);
}
}
return ans;
}
Compilation message (stderr)
sequence.cpp: In function 'int32_t sequence(int32_t, std::vector<int>)':
sequence.cpp:100:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int i = 0; i < a.size(); i++)
| ~~^~~~~~~~~~
sequence.cpp:113:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(int l = 0; l < occ[i].size(); l++)
| ~~^~~~~~~~~~~~~~~
sequence.cpp:116:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | if(r >= occ[i].size())
| ~~^~~~~~~~~~~~~~~~
# | 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... |