#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 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 = j + 1;
int l = 0, r = j;
while (l <= r){
int mid = (l + r) >> 1;
if (st.get(mid, j).val >= new_dp){
pos = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
if (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:184:13: warning: unused variable 'startTime' [-Wunused-variable]
184 | clock_t startTime = clock();
| ^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
13 ms |
660 KB |
Output is correct |
21 |
Correct |
17 ms |
736 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
17 ms |
748 KB |
Output is correct |
24 |
Correct |
17 ms |
728 KB |
Output is correct |
25 |
Correct |
17 ms |
724 KB |
Output is correct |
26 |
Correct |
17 ms |
724 KB |
Output is correct |
27 |
Correct |
14 ms |
748 KB |
Output is correct |
28 |
Correct |
18 ms |
788 KB |
Output is correct |
29 |
Correct |
17 ms |
724 KB |
Output is correct |
30 |
Correct |
17 ms |
744 KB |
Output is correct |
31 |
Correct |
19 ms |
724 KB |
Output is correct |
32 |
Correct |
17 ms |
740 KB |
Output is correct |
33 |
Correct |
17 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
13 ms |
660 KB |
Output is correct |
5 |
Correct |
17 ms |
736 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
17 ms |
748 KB |
Output is correct |
8 |
Correct |
17 ms |
728 KB |
Output is correct |
9 |
Correct |
17 ms |
724 KB |
Output is correct |
10 |
Correct |
17 ms |
724 KB |
Output is correct |
11 |
Correct |
14 ms |
748 KB |
Output is correct |
12 |
Correct |
18 ms |
788 KB |
Output is correct |
13 |
Correct |
17 ms |
724 KB |
Output is correct |
14 |
Correct |
17 ms |
744 KB |
Output is correct |
15 |
Correct |
19 ms |
724 KB |
Output is correct |
16 |
Correct |
17 ms |
740 KB |
Output is correct |
17 |
Correct |
17 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
336 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
328 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
3 ms |
856 KB |
Output is correct |
35 |
Correct |
3 ms |
724 KB |
Output is correct |
36 |
Correct |
3 ms |
724 KB |
Output is correct |
37 |
Correct |
17 ms |
752 KB |
Output is correct |
38 |
Correct |
14 ms |
768 KB |
Output is correct |
39 |
Correct |
18 ms |
724 KB |
Output is correct |
40 |
Correct |
19 ms |
724 KB |
Output is correct |
41 |
Correct |
2 ms |
724 KB |
Output is correct |
42 |
Correct |
20 ms |
744 KB |
Output is correct |
43 |
Correct |
17 ms |
752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
856 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
3 |
Correct |
3 ms |
724 KB |
Output is correct |
4 |
Correct |
17 ms |
752 KB |
Output is correct |
5 |
Correct |
14 ms |
768 KB |
Output is correct |
6 |
Correct |
18 ms |
724 KB |
Output is correct |
7 |
Correct |
19 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
20 ms |
744 KB |
Output is correct |
10 |
Correct |
17 ms |
752 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
13 ms |
660 KB |
Output is correct |
15 |
Correct |
17 ms |
736 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
17 ms |
748 KB |
Output is correct |
18 |
Correct |
17 ms |
728 KB |
Output is correct |
19 |
Correct |
17 ms |
724 KB |
Output is correct |
20 |
Correct |
17 ms |
724 KB |
Output is correct |
21 |
Correct |
14 ms |
748 KB |
Output is correct |
22 |
Correct |
18 ms |
788 KB |
Output is correct |
23 |
Correct |
17 ms |
724 KB |
Output is correct |
24 |
Correct |
17 ms |
744 KB |
Output is correct |
25 |
Correct |
19 ms |
724 KB |
Output is correct |
26 |
Correct |
17 ms |
740 KB |
Output is correct |
27 |
Correct |
17 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
336 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
328 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
0 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Execution timed out |
1077 ms |
15572 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |