이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1'000'002;
const int K = 20;
int w[N], p[1002];
int lg[N];
int mx[N][K];
bool f = true;
vector<int> prv[1002];
vector<pair<int, int>> s;
struct el {
int l, r, k, ind, ans;
}query[N];
bool operator<(const el& a, const el& b) {
if (a.r == b.r)
return a.l < b.l;
return a.r < b.r;
}
bool het(const el& a, const el& b) {
return a.ind < b.ind;
}
void PoqrSolve(int n, int m) {
for (int q = 0; q < m; q++) {
int tmpmx = 0;
bool ok = true;
for (int i = query[q].l; i <= query[q].r; i++) {
if (w[i] < tmpmx && w[i] + tmpmx > query[q].k) {
query[q].ans = 0;
ok = false;
break;
}
tmpmx = max(tmpmx, w[i]);
}
if (ok)
query[q].ans = 1;
}
}
void SortedSolve(int n, int m) {
int l = 0, r = 0;
while (l < n)
{
while (r + 1 < n && w[r + 1] >= w[r])
r++;
s.push_back({ l,r });
l = r + 1;
}
sort(s.begin(), s.end());
for (int q = 0; q < m; q++) {
int ql = query[q].l, qr = query[q].r;
int l = 0, r = (int)s.size() - 1;
int ans = -1;
while (l <= r)
{
int m = l + (r - l) / 2;
if (s[m].first <= ql && s[m].second >= qr)
ans = m;
if (s[m].first <= ql)
l = m + 1;
else r = m - 1;
}
if (ans == -1)query[q].ans = 0;
else query[q].ans = 1;
}
}
void Build(int n, int curk)
{
lg[1] = 0;
for (int i = 2; i <= n; i++)
lg[i] = lg[i / 2] + 1;
int k = lg[n];
for (int i = 0; i < n; i++)
mx[i][0] = w[i + 1];
for (int j = 1; j <= k; j++)
for (int i = 0; i + (1 << j) - 1 <= n; i++)
mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
}
int Mx(int l, int r)
{
int j = lg[r - l + 1];
return max(mx[l][j], mx[r - (1 << j) + 1][j]);
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> w[i];
if (w[i] > 1000)f = false;
}
for (int i = 0; i < m; i++)
{
cin >> query[i].l >> query[i].r >> query[i].k;
query[i].l--; query[i].r--;
query[i].ind = i;
}
if (n <= 5000 && m <= 5000) {
PoqrSolve(n, m);
}
else if (f) {
for (int i = 0; i < n; i++)
prv[w[i]].push_back(i);
sort(query, query + m);
for (int q = 0; q < m; q++)
{
bool ok = true;
for (int i = 0; i <= 1000; i++)
{
while (p[i] < prv[i].size() && prv[i][p[i]] <= query[q].r)
p[i]++;
int tmp;
if (prv[i][p[i]] >= query[q].l && (tmp = Mx(query[q].l, prv[i][p[i]])) > i) {
if (tmp + i > query[q].k)ok = false;
}
}
if (ok)query[q].ans = 1;
}
sort(query, query + m, het);
}
else {
SortedSolve(n, m);
}
for (int i = 0; i < m; i++)
cout << query[i].ans << '\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:111:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | while (p[i] < prv[i].size() && prv[i][p[i]] <= query[q].r)
| ~~~~~^~~~~~~~~~~~~~~
# | 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... |