제출 #521686

#제출 시각아이디문제언어결과실행 시간메모리
521686Soumya1Financial Report (JOI21_financial)C++17
0 / 100
4003 ms9680 KiB
#include <bits/stdc++.h> #ifdef __LOCAL__ #include <debug_local.h> #endif using namespace std; const int mxN = 3e5 + 5; int a[mxN]; int n, d; int ans[mxN]; template<typename T> struct SegmentTree { vector<T> t; int n; T identity = T(); SegmentTree() { } SegmentTree(int _n) : n(_n) { int lg = 31 - __builtin_clz(n); if ((1 << lg) < n) lg++; lg++; t.assign((1 << lg) + 5, identity); } void set(int x, int lx, int rx, int i) { if (lx == rx) { t[x] = 0; return; } int mx = (lx + rx) >> 1; if (i <= mx) set(x << 1, lx, mx, i); else set(x << 1 | 1, mx + 1, rx, i); t[x] = 0; } void set(int i) { set(1, 1, n, i); } void update(int x, int lx, int rx, int p, T v) { if (lx == rx) { t[x] = max(t[x], v); return; } int mx = (lx + rx) >> 1; if (p <= mx) update(x << 1, lx, mx, p, v); else update(x << 1 | 1, mx + 1, rx, p, v); t[x] = max(t[x << 1], t[x << 1 | 1]); } void update(int p, T v) { update(1, 1, n, p, v); } T query(int x, int lx, int rx, int l, int r) { if (lx > r || l > rx) return identity; if (l <= lx && r >= rx) return t[x]; int mx = (lx + rx) >> 1; return max(query(x << 1, lx, mx, l, r), query(x << 1 | 1, mx + 1, rx, l, r)); } T query(int l, int r) { return query(1, 1, n, l, r); } }; SegmentTree<int> st; void solve(int l, int r) { if (l == r) { ans[l] = max(ans[l], 1); return; } int m = (l + r) >> 1; solve(l, m); int cur = m; for (int i = r; i > m; i--) { while (cur >= i - d && cur > 0) { st.update(a[cur], ans[cur]); cur--; } ans[i] = st.query(1, a[i] - 1) + 1; } for (int i = cur + 1; i <= m; i++) { st.set(a[i]); } solve(m + 1, r); } void testCase() { cin >> n >> d; map<int, int> mp; for (int i = 1; i <= n; i++) cin >> a[i], mp[a[i]]; int id = 1; for (auto &[x, y] : mp) y = id++; for (int i = 1; i <= n; i++) a[i] = mp[a[i]]; st = SegmentTree<int> (n); solve(1, n); int mx = 0; for (int i = 1; i <= n; i++) mx = max(mx, ans[i]); cout << mx << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); testCase(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...