제출 #841190

#제출 시각아이디문제언어결과실행 시간메모리
841190HakiersFinancial Report (JOI21_financial)C++17
100 / 100
2082 ms21692 KiB
#include <iostream> #include <queue> using namespace std; const int BASE = 1 << 20; const int BASE2 = 1 << 19; int TREE[BASE << 1]; int LAZY[BASE << 1]; pair<int, int> TREE2[BASE2 << 1]; priority_queue<pair<int, int>> Q; void push(int v, int l, int r){ TREE[l] += LAZY[v]; TREE[r] += LAZY[v]; LAZY[l] += LAZY[v]; LAZY[r] += LAZY[v]; LAZY[v] = 0; } void add(int v, int l, int r, int a, int b, int val){ if(r < a || b < l) return; else if(a <= l && r <= b){ TREE[v] += val; LAZY[v] += val; } else{ int mid = (l+r)/2; push(v, 2*v, 2*v + 1); add(2*v, l, mid, a, b, val); add(2*v + 1, mid+1, r, a, b, val); TREE[v] = min(TREE[2*v], TREE[2*v+1]); } } int querymin(int v, int l, int r, int a, int b){ if(r < a || b < l) return 1e9; else if(a <= l && r <= b) return TREE[v]; else{ int mid = (l+r)/2; push(v, 2*v, 2*v + 1); return min(querymin(2*v, l, mid, a, b), querymin(2*v + 1, mid+1, r, a, b)); } } void update(int v, pair<int, int> val){ v+= BASE2; TREE2[v] = val; v/=2; while (v > 0){ int l = 2*v, r = 2*v + 1; if(TREE2[l].first == TREE2[r].first){ if(TREE2[l].second < TREE2[r].second) TREE2[v] = TREE2[l]; else TREE2[v] = TREE2[r]; } else TREE2[v] = max(TREE2[l], TREE2[r]); v/=2; } } pair<int, int> query(int a, int b){ a += BASE2 - 1; b += BASE2 + 1; pair<int, int> res = {0, -1}; while(a/2 != b/2){ if(a % 2 == 0){ if(res.first == TREE2[a+1].first){ if(TREE2[a+1].second < res.second) res = TREE2[a+1]; } else res = max(res, TREE2[a+1]); } if(b % 2 == 1){ if(res.first == TREE2[b-1].first){ if(TREE2[b-1].second < res.second) res = TREE2[b-1]; } else res = max(res, TREE2[b-1]); } a/=2; b/=2; } return res; } int BS(int idx){ int l = 1, r = idx, mid; while(l < r){ mid = (l + r)/2; if(querymin(1, 0, BASE-1, mid, idx)) r = mid; else l = mid + 1; } return l; } int solve(int d){ int res = 0; while(Q.size()){ auto [a, i] = Q.top(); Q.pop(); a = -a; i = -i; int idx = BS(i-1); pair<int, int> quer = query(idx, i); pair<int, int> val; //cout << "a :: " << a << " bsidx" << idx << endl; val.first = quer.first; val.second = a; if(quer.second != a) val.first += 1; //cout << "a: " << a << " val" << val.first << endl; res = max(res, val.first); update(i, val); add(1, 0, BASE-1, i, i+d-1, 1); } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, d; cin >> n >> d; for(int i = 0; i < BASE2; i++) TREE2[i] = {0, -1}; for(int i = 1; i <= n; i++){ int a; cin >> a; Q.push({-a, -i}); } cout << solve(d) << "\n"; }
#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...