이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sequence.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5 + 5, INF = 1e18;
pair<int, int> seg[N * 4];
int lazy[N * 4];
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 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 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)};
}
int32_t sequence(int32_t n, vector<int32_t> a)
{
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++)
{
if(occ[i].size() == 0) continue;
if(occ[i].size() == 1)
{
Update(1, n, 1, occ[i][0], 2);
continue;
}
// cout << "occ[i] = {" << occ[i][0] << ", " << occ[i][1] << "}\n";
// cout << "Updating: [" << occ[i][0] << ", " << n << "]\n";
Update(1, n, 1, occ[i][0], 1);
// cout << "Updating: [" << occ[i][1] << ", " << n << "]\n";
Update(1, n, 1, occ[i][1], 1);
pair<int, int> p1 = Query(1, n, 1, 1, occ[i][0] - 1);
if(occ[i][0] == 1)
{
p1 = {0, 0};
}
pair<int, int> p2 = Query(1, n, 1, occ[i][1], n);
// cout << p1.first << " " << p1.second << " " << p2.first << " " << p2.second << "\n";
if(p2.first + 2 >= p1.first && p2.first - 2 <= p1.second)
{
ans = 2;
}
if(p2.second + 2 >= p1.first && p2.second - 2 <= p1.second)
{
ans = 2;
}
// cout << "ans = " << ans << "\n";
Update(1, n, 1, occ[i][0], 1);
Update(1, n, 1, occ[i][1], 1);
}
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int32_t sequence(int32_t, std::vector<int>)':
sequence.cpp:94:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int i = 0; i < a.size(); i++)
| ~~^~~~~~~~~~
# | 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... |