#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;
const int mod = (int)1e6 + 3;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n), srt(n);
for(int i = 0; i < n; ++i){
cin >> a[i];
srt[i] = a[i];
}
sort(srt.begin(), srt.end());
srt.erase(unique(srt.begin(), srt.end()), srt.end());
int ans = 0;
for(int i = 0; i + 1 < (int)srt.size(); ++i){
vector<int> lmn(n, -1), rmn(n, -1);
set<int> st;
for(int j = 0; j < n; ++j){
auto p = st.lower_bound(a[j] + 1);
if(p != st.end()){
lmn[j] = *p;
}
st.insert(a[j]);
}
st.clear();
for(int j = n - 1; j >= 0; --j){
auto p = st.lower_bound(a[j] + 1);
if(p != st.end()){
rmn[j] = *p;
}
st.insert(a[j]);
}
int cnt = 0;
int mn = -1, mx = n;
for(int j = 0; j < n; ++j){
if(lmn[j] != -1 && rmn[j] != -1 && a[j] == srt[i]){
if(mn == -1) mn = j;
mx = j;
++cnt;
a[j] = srt[i + 1];
}
}
if(cnt){
if(cnt == 1){
(ans += (lmn[mn] + rmn[mn]) * (srt[i + 1] - srt[i]) % mod) %= mod;
assert(ans >= 0 && ans < mod);}
else{
(ans += (lmn[mn] + rmn[mx] + min(rmn[mn], lmn[mx])) * (srt[i + 1] - srt[i]) % mod + (srt[i] + srt[i + 1] + 1) * (srt[i + 1] - srt[i]) / 2 % mod * (cnt * 2 - 3) % mod) %= mod;
assert(ans >= 0 && ans < mod); }
assert(ans >= 0 && ans < mod);
(ans += cnt * ((srt[i] + srt[i + 1] - 1) * (srt[i + 1] - srt[i]) / 2 % mod) % mod) %= mod;
// cout << fir << ' ' << lst << ' ' << cnt << ' ' << ans << endl;
}
assert(ans >= 0 && ans < mod);
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
17 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
400 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
24 ms |
480 KB |
Output is correct |
10 |
Correct |
20 ms |
484 KB |
Output is correct |
11 |
Correct |
18 ms |
476 KB |
Output is correct |
12 |
Correct |
19 ms |
468 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 |
212 KB |
Output is correct |
4 |
Correct |
17 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
400 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
24 ms |
480 KB |
Output is correct |
10 |
Correct |
20 ms |
484 KB |
Output is correct |
11 |
Correct |
18 ms |
476 KB |
Output is correct |
12 |
Correct |
19 ms |
468 KB |
Output is correct |
13 |
Correct |
33 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
34 ms |
468 KB |
Output is correct |
17 |
Correct |
33 ms |
480 KB |
Output is correct |
18 |
Correct |
34 ms |
468 KB |
Output is correct |
19 |
Correct |
33 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
17 ms |
468 KB |
Output is correct |
24 |
Correct |
17 ms |
468 KB |
Output is correct |
25 |
Correct |
17 ms |
484 KB |
Output is correct |
26 |
Correct |
22 ms |
468 KB |
Output is correct |
27 |
Correct |
22 ms |
468 KB |
Output is correct |
28 |
Correct |
22 ms |
476 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
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 |
212 KB |
Output is correct |
4 |
Correct |
17 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
400 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
24 ms |
480 KB |
Output is correct |
10 |
Correct |
20 ms |
484 KB |
Output is correct |
11 |
Correct |
18 ms |
476 KB |
Output is correct |
12 |
Correct |
19 ms |
468 KB |
Output is correct |
13 |
Correct |
33 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
34 ms |
468 KB |
Output is correct |
17 |
Correct |
33 ms |
480 KB |
Output is correct |
18 |
Correct |
34 ms |
468 KB |
Output is correct |
19 |
Correct |
33 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
17 ms |
468 KB |
Output is correct |
24 |
Correct |
17 ms |
468 KB |
Output is correct |
25 |
Correct |
17 ms |
484 KB |
Output is correct |
26 |
Correct |
22 ms |
468 KB |
Output is correct |
27 |
Correct |
22 ms |
468 KB |
Output is correct |
28 |
Correct |
22 ms |
476 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
4382 ms |
688 KB |
Output is correct |
36 |
Correct |
4403 ms |
596 KB |
Output is correct |
37 |
Correct |
4465 ms |
692 KB |
Output is correct |
38 |
Correct |
4427 ms |
692 KB |
Output is correct |
39 |
Correct |
4611 ms |
692 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
3 ms |
468 KB |
Output is correct |
42 |
Correct |
3 ms |
468 KB |
Output is correct |
43 |
Correct |
3693 ms |
708 KB |
Output is correct |
44 |
Correct |
3724 ms |
692 KB |
Output is correct |
45 |
Correct |
3672 ms |
684 KB |
Output is correct |
46 |
Correct |
4030 ms |
688 KB |
Output is correct |
47 |
Correct |
4034 ms |
688 KB |
Output is correct |
48 |
Correct |
3971 ms |
596 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 |
212 KB |
Output is correct |
4 |
Correct |
17 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
400 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
24 ms |
480 KB |
Output is correct |
10 |
Correct |
20 ms |
484 KB |
Output is correct |
11 |
Correct |
18 ms |
476 KB |
Output is correct |
12 |
Correct |
19 ms |
468 KB |
Output is correct |
13 |
Correct |
33 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
34 ms |
468 KB |
Output is correct |
17 |
Correct |
33 ms |
480 KB |
Output is correct |
18 |
Correct |
34 ms |
468 KB |
Output is correct |
19 |
Correct |
33 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
17 ms |
468 KB |
Output is correct |
24 |
Correct |
17 ms |
468 KB |
Output is correct |
25 |
Correct |
17 ms |
484 KB |
Output is correct |
26 |
Correct |
22 ms |
468 KB |
Output is correct |
27 |
Correct |
22 ms |
468 KB |
Output is correct |
28 |
Correct |
22 ms |
476 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
4382 ms |
688 KB |
Output is correct |
36 |
Correct |
4403 ms |
596 KB |
Output is correct |
37 |
Correct |
4465 ms |
692 KB |
Output is correct |
38 |
Correct |
4427 ms |
692 KB |
Output is correct |
39 |
Correct |
4611 ms |
692 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
3 ms |
468 KB |
Output is correct |
42 |
Correct |
3 ms |
468 KB |
Output is correct |
43 |
Correct |
3693 ms |
708 KB |
Output is correct |
44 |
Correct |
3724 ms |
692 KB |
Output is correct |
45 |
Correct |
3672 ms |
684 KB |
Output is correct |
46 |
Correct |
4030 ms |
688 KB |
Output is correct |
47 |
Correct |
4034 ms |
688 KB |
Output is correct |
48 |
Correct |
3971 ms |
596 KB |
Output is correct |
49 |
Correct |
4751 ms |
692 KB |
Output is correct |
50 |
Correct |
4570 ms |
688 KB |
Output is correct |
51 |
Correct |
4553 ms |
688 KB |
Output is correct |
52 |
Correct |
4516 ms |
696 KB |
Output is correct |
53 |
Correct |
4952 ms |
692 KB |
Output is correct |
54 |
Correct |
4 ms |
468 KB |
Output is correct |
55 |
Correct |
3 ms |
468 KB |
Output is correct |
56 |
Correct |
4 ms |
468 KB |
Output is correct |
57 |
Correct |
3862 ms |
816 KB |
Output is correct |
58 |
Correct |
3758 ms |
688 KB |
Output is correct |
59 |
Correct |
3780 ms |
716 KB |
Output is correct |
60 |
Correct |
4070 ms |
692 KB |
Output is correct |
61 |
Correct |
3976 ms |
708 KB |
Output is correct |
62 |
Correct |
3997 ms |
696 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 |
212 KB |
Output is correct |
4 |
Correct |
17 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
400 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
24 ms |
480 KB |
Output is correct |
10 |
Correct |
20 ms |
484 KB |
Output is correct |
11 |
Correct |
18 ms |
476 KB |
Output is correct |
12 |
Correct |
19 ms |
468 KB |
Output is correct |
13 |
Correct |
33 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
34 ms |
468 KB |
Output is correct |
17 |
Correct |
33 ms |
480 KB |
Output is correct |
18 |
Correct |
34 ms |
468 KB |
Output is correct |
19 |
Correct |
33 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
17 ms |
468 KB |
Output is correct |
24 |
Correct |
17 ms |
468 KB |
Output is correct |
25 |
Correct |
17 ms |
484 KB |
Output is correct |
26 |
Correct |
22 ms |
468 KB |
Output is correct |
27 |
Correct |
22 ms |
468 KB |
Output is correct |
28 |
Correct |
22 ms |
476 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Execution timed out |
5031 ms |
31648 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
17 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
400 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
24 ms |
480 KB |
Output is correct |
10 |
Correct |
20 ms |
484 KB |
Output is correct |
11 |
Correct |
18 ms |
476 KB |
Output is correct |
12 |
Correct |
19 ms |
468 KB |
Output is correct |
13 |
Correct |
33 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
34 ms |
468 KB |
Output is correct |
17 |
Correct |
33 ms |
480 KB |
Output is correct |
18 |
Correct |
34 ms |
468 KB |
Output is correct |
19 |
Correct |
33 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
17 ms |
468 KB |
Output is correct |
24 |
Correct |
17 ms |
468 KB |
Output is correct |
25 |
Correct |
17 ms |
484 KB |
Output is correct |
26 |
Correct |
22 ms |
468 KB |
Output is correct |
27 |
Correct |
22 ms |
468 KB |
Output is correct |
28 |
Correct |
22 ms |
476 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
4382 ms |
688 KB |
Output is correct |
36 |
Correct |
4403 ms |
596 KB |
Output is correct |
37 |
Correct |
4465 ms |
692 KB |
Output is correct |
38 |
Correct |
4427 ms |
692 KB |
Output is correct |
39 |
Correct |
4611 ms |
692 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
3 ms |
468 KB |
Output is correct |
42 |
Correct |
3 ms |
468 KB |
Output is correct |
43 |
Correct |
3693 ms |
708 KB |
Output is correct |
44 |
Correct |
3724 ms |
692 KB |
Output is correct |
45 |
Correct |
3672 ms |
684 KB |
Output is correct |
46 |
Correct |
4030 ms |
688 KB |
Output is correct |
47 |
Correct |
4034 ms |
688 KB |
Output is correct |
48 |
Correct |
3971 ms |
596 KB |
Output is correct |
49 |
Execution timed out |
5031 ms |
31648 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
17 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
400 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
24 ms |
480 KB |
Output is correct |
10 |
Correct |
20 ms |
484 KB |
Output is correct |
11 |
Correct |
18 ms |
476 KB |
Output is correct |
12 |
Correct |
19 ms |
468 KB |
Output is correct |
13 |
Correct |
33 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
34 ms |
468 KB |
Output is correct |
17 |
Correct |
33 ms |
480 KB |
Output is correct |
18 |
Correct |
34 ms |
468 KB |
Output is correct |
19 |
Correct |
33 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
17 ms |
468 KB |
Output is correct |
24 |
Correct |
17 ms |
468 KB |
Output is correct |
25 |
Correct |
17 ms |
484 KB |
Output is correct |
26 |
Correct |
22 ms |
468 KB |
Output is correct |
27 |
Correct |
22 ms |
468 KB |
Output is correct |
28 |
Correct |
22 ms |
476 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
4382 ms |
688 KB |
Output is correct |
36 |
Correct |
4403 ms |
596 KB |
Output is correct |
37 |
Correct |
4465 ms |
692 KB |
Output is correct |
38 |
Correct |
4427 ms |
692 KB |
Output is correct |
39 |
Correct |
4611 ms |
692 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
3 ms |
468 KB |
Output is correct |
42 |
Correct |
3 ms |
468 KB |
Output is correct |
43 |
Correct |
3693 ms |
708 KB |
Output is correct |
44 |
Correct |
3724 ms |
692 KB |
Output is correct |
45 |
Correct |
3672 ms |
684 KB |
Output is correct |
46 |
Correct |
4030 ms |
688 KB |
Output is correct |
47 |
Correct |
4034 ms |
688 KB |
Output is correct |
48 |
Correct |
3971 ms |
596 KB |
Output is correct |
49 |
Correct |
4751 ms |
692 KB |
Output is correct |
50 |
Correct |
4570 ms |
688 KB |
Output is correct |
51 |
Correct |
4553 ms |
688 KB |
Output is correct |
52 |
Correct |
4516 ms |
696 KB |
Output is correct |
53 |
Correct |
4952 ms |
692 KB |
Output is correct |
54 |
Correct |
4 ms |
468 KB |
Output is correct |
55 |
Correct |
3 ms |
468 KB |
Output is correct |
56 |
Correct |
4 ms |
468 KB |
Output is correct |
57 |
Correct |
3862 ms |
816 KB |
Output is correct |
58 |
Correct |
3758 ms |
688 KB |
Output is correct |
59 |
Correct |
3780 ms |
716 KB |
Output is correct |
60 |
Correct |
4070 ms |
692 KB |
Output is correct |
61 |
Correct |
3976 ms |
708 KB |
Output is correct |
62 |
Correct |
3997 ms |
696 KB |
Output is correct |
63 |
Execution timed out |
5031 ms |
31648 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |