#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
//#define endl '\n'
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 2e5 + 30;
template <typename T>
struct SegmentTree{
struct node{
T val;
T add_lazy, assign_lazy;
bool isAdded, isAssigned;
node(){
add_lazy = assign_lazy = 0;
isAdded = isAssigned = false;
val = 0;
};
node(T x){
add_lazy = assign_lazy = 0;
isAdded = isAssigned = false;
val = x;
}
};
vector<node> tree;
node neutral_element = node(1e6); // e.g. 0 for sum, INF for min ...;
inline void combine(node &par, node lf, node rg){
par.val = min(lf.val, rg.val);
}
int _begin, _end;
inline void init(int n){
_begin = 0;
_end = n - 1;
tree.assign(n * 4 + 5, neutral_element);
}
inline void upd(int v, char op, T lazy){
if (op == '='){
tree[v].val = lazy;
tree[v].assign_lazy = lazy;
tree[v].isAssigned = true;
tree[v].add_lazy = 0;
tree[v].isAdded = false;
}
if (op == '+'){
tree[v].val += lazy;
tree[v].add_lazy += lazy;
tree[v].isAdded = true;
}
}
inline void push(int v, int tl, int tr){
if (tl == tr) return;
if (tree[v].isAssigned){
upd(v << 1, '=', tree[v].assign_lazy);
upd(v << 1 | 1, '=', tree[v].assign_lazy);
tree[v].isAssigned = false;
tree[v].assign_lazy = 0;
}
if (tree[v].isAdded){
upd(v << 1, '+', tree[v].add_lazy);
upd(v << 1 | 1, '+', tree[v].add_lazy);
tree[v].isAdded = false;
tree[v].add_lazy = 0;
}
}
inline void update(int v, int tl, int tr, int l, int r, char op, T val){
push(v, tl, tr);
if (l <= tl && tr <= r){
upd(v, op, val);
return;
}
if (tl > r || tr < l) return;
int tm = (tl + tr) >> 1;
update(v << 1, tl, tm, l, r, op, val);
update(v << 1 | 1, tm + 1, tr, l, r, op, val);
combine(tree[v], tree[v << 1], tree[v << 1 | 1]);
}
inline void update(int l, int r, char op, T val){
update(1, _begin, _end, l, r, op, val);
}
inline void update(int pos, char op, T val){
update(1, _begin, _end, pos, pos, op, val);
}
inline node get(int v, int tl, int tr, int l, int r){
push(v, tl, tr);
if (l <= tl && tr <= r) return tree[v];
if (tl > r || tr < l) return neutral_element;
int tm = (tl + tr) >> 1;
node res;
combine(res, get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r));
return res;
}
inline node get(int l, int r){
return get(1, _begin, _end, l, r);
}
int find(int v, int tl, int tr, int k){
push(v, tl, tr);
if (tl == tr) return tl;
int tm = (tl + tr) >> 1;
if (tree[v << 1].val > k){
return find(v << 1, tl, tm, k);
} else {
return find(v << 1 | 1, tm + 1, tr, k);
}
}
int find(int k){
if (tree[1].val <= k) return -1;
return find(1, _begin, _end, k);
}
};
int a[N];
int ord[N];
void solve(){
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
vector<int> p(n + 1);
iota(all(p), 0);
sort(all(p), [&](int i, int j){
return MP(a[i] - 1LL * i * m, -i) < MP(a[j] - 1LL * j * m, -j);
});
for (int i = 0; i <= n; i++) ord[p[i]] = i;
SegmentTree<int> st;
st.init(n + 1);
st.update(ord[0], '=', 0);
for (int i = 1; i <= n; i++){
int j = ord[i];
int new_dp = st.get(j, n).val;
st.update(0, n, '+', 1);
if (new_dp <= n){
int pos = st.find(new_dp);
if (pos != -1 && pos <= j) st.update(pos, j, '=', new_dp);
}
// for (int t = 0; t <= n; t++){cout << st.get(t, t).val << ' ';} cout << endl;
}
cout << st.get(0, n).val << endl;
}
int main(){
clock_t startTime = clock();
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test_cases = 1;
// cin >> test_cases;
for (int test = 1; test <= test_cases; test++){
//cout << (solve() ? "YES" : "NO") << endl;
solve();
}
#ifdef LOCAL
cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif
return 0;
}
Compilation message
triusis.cpp: In function 'int main()':
triusis.cpp:188:13: warning: unused variable 'startTime' [-Wunused-variable]
188 | clock_t startTime = clock();
| ^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |