#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 2e5 + 7;
const int Mod = 1e6 + 3;///lon
const ll INF = 1e18 + 7;
const ll BASE = 137;
const int szBL = 250;
int n;
int a[N];
struct Segment_Tree {
int m;
pii st[N << 2];
void build (int id, int l, int r) {
if (l == r) {
st[id] = {a[l], 1};
return;
}
int mid = l + r >> 1;
build (id << 1, l, mid);
build (id << 1 | 1, mid + 1, r);
st[id].fs = max(st[id << 1].fs, st[id << 1 | 1].fs);
st[id].se = st[id << 1].se + st[id << 1 | 1].se;
}
void init (int n) {
m = n;
build (1, 1, m);
}
void update (int id, int l, int r, int pos) {
if (l > pos || r < pos) return;
if (l == r) {
st[id] = {0, 0};
return;
}
int mid = l + r >> 1;
update (id << 1, l, mid, pos);
update (id << 1 | 1, mid + 1, r, pos);
st[id].fs = max(st[id << 1].fs, st[id << 1 | 1].fs);
st[id].se = st[id << 1].se + st[id << 1 | 1].se;
}
int get (int id, int l, int r, int u, int v) {
if (l > v || r < u) return 0;
if (l >= u && r <= v) return st[id].fs;
int mid = l + r >> 1;
return max (get (id << 1, l, mid, u, v), get (id << 1 | 1, mid + 1, r, u, v));
}
int cnt (int id, int l, int r, int u, int v) {
if (l > v || r < u) return 0;
if (l >= u && r <= v) return st[id].se;
int mid = l + r >> 1;
return cnt (id << 1, l, mid, u, v) + cnt (id << 1 | 1, mid + 1, r, u, v);
}
void update (int pos) {
update (1, 1, m, pos);
}
int get (int u, int v) {
return get (1, 1, m, u, v);
}
int cnt (int u, int v) {
return cnt (1, 1, m, u, v);
}
}ST;
void solution () {
cin >> n;
rep (i, 1, n) cin >> a[i];
ST.init(n);
ll res = 0;
reb (i, n, 1) {
int pos; {
int lf = 1, rt = n;
while (lf < rt) {
int mid = lf + rt + 1>> 1;
if (ST.get(mid, n) >= i) lf = mid;
else rt = mid - 1;
}
if (ST.get(lf, n) < i) pos = -1;
else pos = lf;
}
if (pos == -1) {
cout << -1 <<"\n";
return;
}
res += ST.cnt(pos, n) - 1;
ST.update(pos);
}
cout << res <<"\n";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +12
*/
Compilation message
Main.cpp: In member function 'void Segment_Tree::build(int, int, int)':
Main.cpp:39:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'void Segment_Tree::update(int, int, int, int)':
Main.cpp:56:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'int Segment_Tree::get(int, int, int, int, int)':
Main.cpp:66:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
66 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'int Segment_Tree::cnt(int, int, int, int, int)':
Main.cpp:73:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
73 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In function 'void solution()':
Main.cpp:99:35: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
99 | int mid = lf + rt + 1>> 1;
| ~~~~~~~~^~~
Main.cpp: At global scope:
Main.cpp:118:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
118 | main () {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6736 KB |
Output is correct |
2 |
Correct |
5 ms |
6736 KB |
Output is correct |
3 |
Correct |
5 ms |
6736 KB |
Output is correct |
4 |
Correct |
5 ms |
6820 KB |
Output is correct |
5 |
Correct |
5 ms |
6736 KB |
Output is correct |
6 |
Correct |
5 ms |
6736 KB |
Output is correct |
7 |
Correct |
6 ms |
6736 KB |
Output is correct |
8 |
Correct |
6 ms |
6904 KB |
Output is correct |
9 |
Correct |
5 ms |
6736 KB |
Output is correct |
10 |
Correct |
5 ms |
6908 KB |
Output is correct |
11 |
Correct |
6 ms |
6736 KB |
Output is correct |
12 |
Correct |
5 ms |
6736 KB |
Output is correct |
13 |
Correct |
6 ms |
6736 KB |
Output is correct |
14 |
Correct |
6 ms |
6736 KB |
Output is correct |
15 |
Correct |
5 ms |
6736 KB |
Output is correct |
16 |
Correct |
5 ms |
6736 KB |
Output is correct |
17 |
Correct |
5 ms |
6736 KB |
Output is correct |
18 |
Correct |
6 ms |
6736 KB |
Output is correct |
19 |
Correct |
5 ms |
6736 KB |
Output is correct |
20 |
Correct |
6 ms |
6736 KB |
Output is correct |
21 |
Correct |
5 ms |
6736 KB |
Output is correct |
22 |
Correct |
5 ms |
6736 KB |
Output is correct |
23 |
Correct |
5 ms |
6736 KB |
Output is correct |
24 |
Correct |
5 ms |
6736 KB |
Output is correct |
25 |
Correct |
5 ms |
6736 KB |
Output is correct |
26 |
Correct |
5 ms |
6736 KB |
Output is correct |
27 |
Correct |
5 ms |
6904 KB |
Output is correct |
28 |
Correct |
7 ms |
6736 KB |
Output is correct |
29 |
Correct |
5 ms |
6736 KB |
Output is correct |
30 |
Correct |
4 ms |
6736 KB |
Output is correct |
31 |
Correct |
4 ms |
6736 KB |
Output is correct |
32 |
Correct |
5 ms |
6736 KB |
Output is correct |
33 |
Correct |
5 ms |
6736 KB |
Output is correct |
34 |
Correct |
5 ms |
6736 KB |
Output is correct |
35 |
Correct |
4 ms |
6736 KB |
Output is correct |
36 |
Correct |
5 ms |
6736 KB |
Output is correct |
37 |
Correct |
4 ms |
6992 KB |
Output is correct |
38 |
Correct |
5 ms |
6736 KB |
Output is correct |
39 |
Correct |
5 ms |
6816 KB |
Output is correct |
40 |
Correct |
8 ms |
6736 KB |
Output is correct |
41 |
Correct |
2 ms |
6736 KB |
Output is correct |
42 |
Correct |
5 ms |
6816 KB |
Output is correct |
43 |
Correct |
2 ms |
6736 KB |
Output is correct |
44 |
Correct |
2 ms |
6736 KB |
Output is correct |
45 |
Correct |
2 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6736 KB |
Output is correct |
2 |
Correct |
5 ms |
6736 KB |
Output is correct |
3 |
Correct |
5 ms |
6736 KB |
Output is correct |
4 |
Correct |
5 ms |
6820 KB |
Output is correct |
5 |
Correct |
5 ms |
6736 KB |
Output is correct |
6 |
Correct |
5 ms |
6736 KB |
Output is correct |
7 |
Correct |
6 ms |
6736 KB |
Output is correct |
8 |
Correct |
6 ms |
6904 KB |
Output is correct |
9 |
Correct |
5 ms |
6736 KB |
Output is correct |
10 |
Correct |
5 ms |
6908 KB |
Output is correct |
11 |
Correct |
6 ms |
6736 KB |
Output is correct |
12 |
Correct |
5 ms |
6736 KB |
Output is correct |
13 |
Correct |
6 ms |
6736 KB |
Output is correct |
14 |
Correct |
6 ms |
6736 KB |
Output is correct |
15 |
Correct |
5 ms |
6736 KB |
Output is correct |
16 |
Correct |
5 ms |
6736 KB |
Output is correct |
17 |
Correct |
5 ms |
6736 KB |
Output is correct |
18 |
Correct |
6 ms |
6736 KB |
Output is correct |
19 |
Correct |
5 ms |
6736 KB |
Output is correct |
20 |
Correct |
6 ms |
6736 KB |
Output is correct |
21 |
Correct |
5 ms |
6736 KB |
Output is correct |
22 |
Correct |
5 ms |
6736 KB |
Output is correct |
23 |
Correct |
5 ms |
6736 KB |
Output is correct |
24 |
Correct |
5 ms |
6736 KB |
Output is correct |
25 |
Correct |
5 ms |
6736 KB |
Output is correct |
26 |
Correct |
5 ms |
6736 KB |
Output is correct |
27 |
Correct |
5 ms |
6904 KB |
Output is correct |
28 |
Correct |
7 ms |
6736 KB |
Output is correct |
29 |
Correct |
5 ms |
6736 KB |
Output is correct |
30 |
Correct |
4 ms |
6736 KB |
Output is correct |
31 |
Correct |
4 ms |
6736 KB |
Output is correct |
32 |
Correct |
5 ms |
6736 KB |
Output is correct |
33 |
Correct |
5 ms |
6736 KB |
Output is correct |
34 |
Correct |
5 ms |
6736 KB |
Output is correct |
35 |
Correct |
4 ms |
6736 KB |
Output is correct |
36 |
Correct |
5 ms |
6736 KB |
Output is correct |
37 |
Correct |
4 ms |
6992 KB |
Output is correct |
38 |
Correct |
5 ms |
6736 KB |
Output is correct |
39 |
Correct |
5 ms |
6816 KB |
Output is correct |
40 |
Correct |
8 ms |
6736 KB |
Output is correct |
41 |
Correct |
2 ms |
6736 KB |
Output is correct |
42 |
Correct |
5 ms |
6816 KB |
Output is correct |
43 |
Correct |
2 ms |
6736 KB |
Output is correct |
44 |
Correct |
2 ms |
6736 KB |
Output is correct |
45 |
Correct |
2 ms |
6736 KB |
Output is correct |
46 |
Correct |
262 ms |
8956 KB |
Output is correct |
47 |
Correct |
278 ms |
8832 KB |
Output is correct |
48 |
Correct |
206 ms |
8952 KB |
Output is correct |
49 |
Correct |
251 ms |
8776 KB |
Output is correct |
50 |
Correct |
237 ms |
8784 KB |
Output is correct |
51 |
Correct |
212 ms |
8848 KB |
Output is correct |
52 |
Correct |
334 ms |
8592 KB |
Output is correct |
53 |
Correct |
288 ms |
8528 KB |
Output is correct |
54 |
Correct |
273 ms |
8772 KB |
Output is correct |
55 |
Correct |
208 ms |
8784 KB |
Output is correct |
56 |
Correct |
261 ms |
8756 KB |
Output is correct |
57 |
Correct |
233 ms |
8760 KB |
Output is correct |
58 |
Correct |
335 ms |
8528 KB |
Output is correct |
59 |
Correct |
305 ms |
8528 KB |
Output is correct |
60 |
Correct |
350 ms |
8540 KB |
Output is correct |
61 |
Correct |
268 ms |
8528 KB |
Output is correct |
62 |
Correct |
211 ms |
8776 KB |
Output is correct |
63 |
Correct |
227 ms |
8528 KB |
Output is correct |
64 |
Correct |
317 ms |
8528 KB |
Output is correct |
65 |
Correct |
367 ms |
8760 KB |
Output is correct |
66 |
Correct |
290 ms |
8528 KB |
Output is correct |
67 |
Correct |
294 ms |
8528 KB |
Output is correct |
68 |
Correct |
283 ms |
8528 KB |
Output is correct |
69 |
Correct |
360 ms |
8772 KB |
Output is correct |
70 |
Correct |
358 ms |
8792 KB |
Output is correct |
71 |
Correct |
324 ms |
8796 KB |
Output is correct |
72 |
Correct |
329 ms |
8792 KB |
Output is correct |
73 |
Correct |
296 ms |
8780 KB |
Output is correct |
74 |
Correct |
293 ms |
8692 KB |
Output is correct |
75 |
Correct |
298 ms |
8792 KB |
Output is correct |
76 |
Correct |
112 ms |
8784 KB |
Output is correct |
77 |
Correct |
200 ms |
8868 KB |
Output is correct |
78 |
Correct |
265 ms |
8528 KB |
Output is correct |
79 |
Correct |
182 ms |
8528 KB |
Output is correct |
80 |
Correct |
138 ms |
8784 KB |
Output is correct |
81 |
Correct |
171 ms |
8784 KB |
Output is correct |
82 |
Correct |
188 ms |
8528 KB |
Output is correct |
83 |
Correct |
174 ms |
8764 KB |
Output is correct |
84 |
Correct |
269 ms |
8784 KB |
Output is correct |
85 |
Correct |
292 ms |
8632 KB |
Output is correct |
86 |
Correct |
285 ms |
8652 KB |
Output is correct |
87 |
Correct |
16 ms |
8784 KB |
Output is correct |
88 |
Correct |
310 ms |
8708 KB |
Output is correct |