이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 3e5 + 2;
const int M = 5e5 + 2;
int mod = 1000000007;
int link[N], l[N], r[N];
int Find(int x) {
if (x == link[x]) return x;
return x = Find(link[x]);
}
void Unite(int u, int v) {
u = Find(u);
v = Find(v);
if (u == v) return;
link[u] = v;
smin(l[v], l[u]);
smax(r[v], r[u]);
}
int st[4 * N];
void Add(int node, int l, int r, int x, int y) {
if (l == r) {st[node] = y; return;}
int mid = l + r >> 1;
if (x <= mid) Add(2 * node, l, mid, x, y);
else Add(2 * node + 1, mid + 1, r, x, y);
st[node] = max(st[2 * node], st[2 * node + 1]);
}
int Get(int node, int l, int r, int ql, int qr) {
if (r < ql || qr < l) return 0;
if (ql <= l && r <= qr) return st[node];
int mid = l + r >> 1;
return max(Get(2 * node, l, mid, ql, qr), Get(2 * node + 1, mid + 1, r, ql, qr));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, d;
cin >> n >> d;
vector<int> a(n), b(n);
iota(b.begin(), b.end(), 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
link[i] = i;
l[i] = max(0, i - d);
r[i] = min(n - 1, i + d);
}
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&] (int i, int j) {
if (a[i] == a[j]) return b[i] > b[j];
return a[i] < a[j];
});
set<int> s;
for (int i : ord) {
auto it = s.lower_bound(b[i]);
if (it != s.end() && r[b[i]] >= *it) Unite(b[i], *it);
if (it != s.begin() && l[b[i]] <= *prev(it)) Unite(b[i], *prev(it));
s.insert(b[i]);
Add(1, 0, n - 1, b[i], Get(1, 0, n - 1, l[Find(b[i])], b[i]) + 1);
}
cout << st[1] << en;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'void Add(int, int, int, int, int)':
Main.cpp:28:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
28 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In function 'int Get(int, int, int, int, int)':
Main.cpp:36:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | int mid = l + r >> 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... |