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 <bits/stdc++.h>
#define fi first
#define se second
#define ssize(x) int(x.size())
#define pn printf("\n")
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int inf = 2e09; ll infll = 2e18;
int base = 1;
struct seg{
struct Node{
int mn1, mn2, mx, p;
Node(){ mn1 = inf-1, mn2 = inf, mx = 0, p = 0; } // mn1 < mn2
Node(int m1, int m2, int mxx, int pp) { mn1 = m1, mn2 = m2, mx = mxx, p = pp; }
};
vector<Node> t;
void init(int n){
while(base < n) base <<= 1;
t.resize(base<<1);
}
void merge(int i){
t[i].mx = max(t[i<<1].mx, t[i<<1|1].mx);
if(t[i<<1].mn1 < t[i<<1|1].mn1) t[i].mn1 = t[i<<1].mn1, t[i].mn2 = min(t[i<<1].mn2, t[i<<1|1].mn1);
else if(t[i<<1].mn1 > t[i<<1|1].mn1) t[i].mn1 = t[i<<1|1].mn1, t[i].mn2 = min(t[i<<1].mn1, t[i<<1|1].mn2);
else t[i].mn1 = t[i<<1].mn1, t[i].mn2 = min(t[i<<1].mn2, t[i<<1|1].mn2);
}
void max_node(int i, int val){ t[i].mn1 = val, t[i].p = max(t[i].p, val); }
void push(int i){
if(t[i].p){
if(t[i<<1].mn1 < t[i].p) max_node(i<<1, t[i].p);
if(t[i<<1|1].mn1 < t[i].p)max_node(i<<1|1, t[i].p);
t[i].p = 0;
}
}
void upd_point(int i, int s, int e, int x, int val, int time){
if(s == e) return void(t[i] = Node(time, inf, val, 0));
int mid = (s+e)>>1; push(i);
if(x <= mid) upd_point(i<<1, s, mid, x, val, time);
else upd_point(i<<1|1, mid+1, e, x, val, time);
merge(i);
}
void update(int x, int val, int time){ upd_point(1, 1, base, x, val, time); }
void purge_point(int i, int &time){
if(base <= i) return void(t[i] = Node());
push(i);
if(t[i<<1].mn1 < time) purge_point(i<<1, time);
if(t[i<<1|1].mn1 < time) purge_point(i<<1|1, time);
merge(i);
}
void zero(int time){ purge_point(1, time); }
int qr(int i, int s, int e, int x, int y){
if(x <= s && e <= y) return t[i].mx;
int mid = (s+e)>>1, ret = 0; push(i);
if(x <= mid) ret = max(ret, qr(i<<1, s, mid, x, y));
if(mid < y) ret = max(ret, qr(i<<1|1, mid+1, e, x, y));
merge(i);
return ret;
}
int query(int x, int y){
if(x <= y) return qr(1, 1, base, max(1, x), min(base, y));
return 0;
}
void mx_time(int i, int s, int e, int x, int y, int time){
if(x <= s && e <= y && t[i].mn1 <= time && time < t[i].mn2) return void(max_node(i, time));
int mid = (s+e)>>1; push(i);
if(x <= mid && t[i<<1].mn1 <= time) mx_time(i<<1, s, mid, x, y, time);
if(mid < y && t[i<<1|1].mn1 <= time) mx_time(i<<1|1, mid+1, e, x, y, time);
merge(i);
}
void max_time(int l, int r, int time){ mx_time(1, 1, base, l, r, time); }
};
void answer(){
int n, d; scanf("%d%d", &n, &d);
set<int> sc;
vector<int> t(n+1);
for(int i = 1; i <= n; ++i) scanf("%d", &t[i]), sc.emplace(t[i]);
unordered_map<int, int> mp; int it = 0;
for(int u : sc) mp[u] = ++it;
for(int i = 1; i <= n; ++i) t[i] = mp[t[i]];
seg seg; seg.init(n);
int result = 0;
deque<int> dq;
for(int i = 1; i <= n; ++i){
if(ssize(dq) && dq.front() < i-d) dq.pop_front();
int mn = ssize(dq) ? t[dq.front()] : inf, mn_time = ssize(dq) ? dq.front() : 0;
while(ssize(dq) && t[i] <= t[dq.back()]) dq.pop_back();
dq.emplace_back(i);
//~ printf("%d %d\n", mn, mn_time);
if(mn != inf) seg.max_time(mn, base, mn_time), seg.zero(i-d);
if(i != n) seg.update(t[i], max(seg.query(1, t[i]-1)+1, seg.query(t[i], t[i])), i);
else result = max(seg.query(1, t[i]-1)+1, seg.query(t[i], base));
//~ for(int j = 1; j <= n; ++j) printf("%d %d, ", seg.query(j, j), seg.t[j+base-1].mn1);
//~ pn;
}
printf("%d\n", result);
}
int main(){
int T = 1;
for(++T; --T; ) answer();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void answer()':
Main.cpp:75:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | int n, d; scanf("%d%d", &n, &d);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:78:36: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | for(int i = 1; i <= n; ++i) scanf("%d", &t[i]), sc.emplace(t[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... |