답안 #1113078

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1113078 2024-11-15T16:31:31 Z duckindog Real Mountains (CCO23_day1problem2) C++17
18 / 25
2843 ms 203892 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1'000'000 + 10,
          M = 1'000'003,
          MAX = 1'000'000'001;
int n;
int h[N];

int prefMax[N], suffMax[N];
int goalH[N];

int f[N << 2];
void build(int s, int l, int r) { 
  if (l == r) { 
    f[s] = h[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(s << 1, l, mid); build(s << 1 | 1, mid + 1, r);
  f[s] = min(f[s << 1], f[s << 1 | 1]);
}
void update(int s, int l, int r, int p, int x) { 
  if (p < l || p > r) return;
  if (l == r) { 
    f[s] = x;
    return;
  }
  int mid = (l + r) >> 1;
  update(s << 1, l, mid, p, x); update(s << 1 | 1, mid + 1, r, p, x);
  f[s] = min(f[s << 1], f[s << 1 | 1]);
}
int query(int s, int l, int r, int u, int v) { 
  if (v < l || u > r) return MAX;
  if (u <= l && r <= v) return f[s];
  int mid = (l + r) >> 1;
  return min(query(s << 1, l, mid, u, v), query(s << 1 | 1, mid + 1, r, u, v));
}

set<int> s[N];
vector<int> del[N];

int32_t main() { 
  cin.tie(0)->sync_with_stdio(0);

  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> h[i];

  build(1, 1, n);
  
  vector<int> allValue(h + 1, h + n + 1);
  sort(allValue.begin(), allValue.end());
  allValue.erase(unique(allValue.begin(), allValue.end()), allValue.end());

  for (int i = 1; i <= n; ++i) 
    h[i] = lower_bound(allValue.begin(), allValue.end(), h[i]) - allValue.begin();

  for (int i = 1; i <= n; ++i) prefMax[i] = max(prefMax[i - 1], h[i]);
  for (int i = n; i >= 1; --i) suffMax[i] = max(suffMax[i + 1], h[i]);

  for (int i = 1; i <= n; ++i) goalH[i] = min(prefMax[i], suffMax[i]);
  
  for (int i = 1; i <= n; ++i) { 
    s[h[i]].insert(i);
    del[goalH[i]].push_back(i);
  }

  auto sum = [&](int l, int r) { 
    return (1ll * (r + l) * (r - l + 1) / 2) % M;
  };

  auto add = [&](auto& x, const auto& y) { 
    x += y;
    if (x >= M) x -= M;
  };

  long long answer = 0;

  set<int> vt;
  for (int it = 0; it < (int)allValue.size() - 1; ++it) { 
    int height = allValue[it], nxtHeight = allValue[it + 1];
    
    for (const auto& i : s[it]) { 
      vt.insert(i);
      update(1, 1, n, i, MAX);
    }
    for (const auto& i : del[it]) vt.erase(i);

    if (!vt.size()) continue;
    
    int cnt = nxtHeight - height;
    if (vt.size() == 1) { 
      int i = *vt.begin();
      add(answer, 1ll * cnt * (query(1, 1, n, 1, i) + query(1, 1, n, i, n)) % M);
      add(answer, sum(height, nxtHeight - 1));
    } else { 
      int l = *vt.begin(), r = *vt.rbegin();

      long long addLFirst = query(1, 1, n, 1, l) + query(1, 1, n, l, n) +
                            query(1, 1, n, r, n);
      long long addRFirst = query(1, 1, n, 1, r) + query(1, 1, n, r, n) +
                            query(1, 1, n, 1, l);
      
      add(answer, 1ll * min(addLFirst, addRFirst) % M * cnt % M);
      add(answer, sum(height, nxtHeight - 1));
      add(answer, sum(height, nxtHeight - 1));
      add(answer, sum(height + 1, nxtHeight));

      int total = 0;
      add(total, sum(height, nxtHeight - 1));
      add(total, sum(height + 1, nxtHeight));
      add(total, sum(height + 1, nxtHeight));
      
      add(answer, 1ll * (vt.size() - 2) % M * total % M);
    }
  }

  cout << answer % M << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 70728 KB Output is correct
2 Correct 49 ms 70728 KB Output is correct
3 Correct 52 ms 70728 KB Output is correct
4 Correct 52 ms 71560 KB Output is correct
5 Correct 53 ms 71240 KB Output is correct
6 Correct 51 ms 71508 KB Output is correct
7 Correct 52 ms 71240 KB Output is correct
8 Correct 52 ms 71496 KB Output is correct
9 Correct 54 ms 71240 KB Output is correct
10 Correct 56 ms 71240 KB Output is correct
11 Correct 56 ms 71508 KB Output is correct
12 Correct 56 ms 71496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 70728 KB Output is correct
2 Correct 49 ms 70728 KB Output is correct
3 Correct 52 ms 70728 KB Output is correct
4 Correct 52 ms 71560 KB Output is correct
5 Correct 53 ms 71240 KB Output is correct
6 Correct 51 ms 71508 KB Output is correct
7 Correct 52 ms 71240 KB Output is correct
8 Correct 52 ms 71496 KB Output is correct
9 Correct 54 ms 71240 KB Output is correct
10 Correct 56 ms 71240 KB Output is correct
11 Correct 56 ms 71508 KB Output is correct
12 Correct 56 ms 71496 KB Output is correct
13 Correct 56 ms 71496 KB Output is correct
14 Correct 54 ms 70752 KB Output is correct
15 Correct 53 ms 70728 KB Output is correct
16 Correct 56 ms 71396 KB Output is correct
17 Correct 56 ms 71496 KB Output is correct
18 Correct 56 ms 71508 KB Output is correct
19 Correct 55 ms 71576 KB Output is correct
20 Correct 58 ms 71496 KB Output is correct
21 Correct 57 ms 71592 KB Output is correct
22 Correct 58 ms 71420 KB Output is correct
23 Correct 57 ms 71592 KB Output is correct
24 Correct 53 ms 71496 KB Output is correct
25 Correct 51 ms 71496 KB Output is correct
26 Correct 49 ms 71504 KB Output is correct
27 Correct 49 ms 71452 KB Output is correct
28 Correct 51 ms 71348 KB Output is correct
29 Correct 49 ms 70736 KB Output is correct
30 Correct 49 ms 71056 KB Output is correct
31 Correct 54 ms 70840 KB Output is correct
32 Correct 53 ms 70728 KB Output is correct
33 Correct 50 ms 70728 KB Output is correct
34 Correct 58 ms 70760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 70728 KB Output is correct
2 Correct 49 ms 70728 KB Output is correct
3 Correct 52 ms 70728 KB Output is correct
4 Correct 52 ms 71560 KB Output is correct
5 Correct 53 ms 71240 KB Output is correct
6 Correct 51 ms 71508 KB Output is correct
7 Correct 52 ms 71240 KB Output is correct
8 Correct 52 ms 71496 KB Output is correct
9 Correct 54 ms 71240 KB Output is correct
10 Correct 56 ms 71240 KB Output is correct
11 Correct 56 ms 71508 KB Output is correct
12 Correct 56 ms 71496 KB Output is correct
13 Correct 56 ms 71496 KB Output is correct
14 Correct 54 ms 70752 KB Output is correct
15 Correct 53 ms 70728 KB Output is correct
16 Correct 56 ms 71396 KB Output is correct
17 Correct 56 ms 71496 KB Output is correct
18 Correct 56 ms 71508 KB Output is correct
19 Correct 55 ms 71576 KB Output is correct
20 Correct 58 ms 71496 KB Output is correct
21 Correct 57 ms 71592 KB Output is correct
22 Correct 58 ms 71420 KB Output is correct
23 Correct 57 ms 71592 KB Output is correct
24 Correct 53 ms 71496 KB Output is correct
25 Correct 51 ms 71496 KB Output is correct
26 Correct 49 ms 71504 KB Output is correct
27 Correct 49 ms 71452 KB Output is correct
28 Correct 51 ms 71348 KB Output is correct
29 Correct 49 ms 70736 KB Output is correct
30 Correct 49 ms 71056 KB Output is correct
31 Correct 54 ms 70840 KB Output is correct
32 Correct 53 ms 70728 KB Output is correct
33 Correct 50 ms 70728 KB Output is correct
34 Correct 58 ms 70760 KB Output is correct
35 Correct 52 ms 71488 KB Output is correct
36 Correct 51 ms 71496 KB Output is correct
37 Correct 56 ms 71484 KB Output is correct
38 Correct 50 ms 71496 KB Output is correct
39 Correct 52 ms 71468 KB Output is correct
40 Correct 57 ms 71504 KB Output is correct
41 Correct 55 ms 71496 KB Output is correct
42 Correct 56 ms 71396 KB Output is correct
43 Correct 57 ms 71532 KB Output is correct
44 Correct 58 ms 71496 KB Output is correct
45 Correct 57 ms 71496 KB Output is correct
46 Correct 58 ms 71568 KB Output is correct
47 Correct 57 ms 71496 KB Output is correct
48 Correct 66 ms 71380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 70728 KB Output is correct
2 Correct 49 ms 70728 KB Output is correct
3 Correct 52 ms 70728 KB Output is correct
4 Correct 52 ms 71560 KB Output is correct
5 Correct 53 ms 71240 KB Output is correct
6 Correct 51 ms 71508 KB Output is correct
7 Correct 52 ms 71240 KB Output is correct
8 Correct 52 ms 71496 KB Output is correct
9 Correct 54 ms 71240 KB Output is correct
10 Correct 56 ms 71240 KB Output is correct
11 Correct 56 ms 71508 KB Output is correct
12 Correct 56 ms 71496 KB Output is correct
13 Correct 56 ms 71496 KB Output is correct
14 Correct 54 ms 70752 KB Output is correct
15 Correct 53 ms 70728 KB Output is correct
16 Correct 56 ms 71396 KB Output is correct
17 Correct 56 ms 71496 KB Output is correct
18 Correct 56 ms 71508 KB Output is correct
19 Correct 55 ms 71576 KB Output is correct
20 Correct 58 ms 71496 KB Output is correct
21 Correct 57 ms 71592 KB Output is correct
22 Correct 58 ms 71420 KB Output is correct
23 Correct 57 ms 71592 KB Output is correct
24 Correct 53 ms 71496 KB Output is correct
25 Correct 51 ms 71496 KB Output is correct
26 Correct 49 ms 71504 KB Output is correct
27 Correct 49 ms 71452 KB Output is correct
28 Correct 51 ms 71348 KB Output is correct
29 Correct 49 ms 70736 KB Output is correct
30 Correct 49 ms 71056 KB Output is correct
31 Correct 54 ms 70840 KB Output is correct
32 Correct 53 ms 70728 KB Output is correct
33 Correct 50 ms 70728 KB Output is correct
34 Correct 58 ms 70760 KB Output is correct
35 Correct 52 ms 71488 KB Output is correct
36 Correct 51 ms 71496 KB Output is correct
37 Correct 56 ms 71484 KB Output is correct
38 Correct 50 ms 71496 KB Output is correct
39 Correct 52 ms 71468 KB Output is correct
40 Correct 57 ms 71504 KB Output is correct
41 Correct 55 ms 71496 KB Output is correct
42 Correct 56 ms 71396 KB Output is correct
43 Correct 57 ms 71532 KB Output is correct
44 Correct 58 ms 71496 KB Output is correct
45 Correct 57 ms 71496 KB Output is correct
46 Correct 58 ms 71568 KB Output is correct
47 Correct 57 ms 71496 KB Output is correct
48 Correct 66 ms 71380 KB Output is correct
49 Incorrect 60 ms 71496 KB Output isn't correct
50 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 70728 KB Output is correct
2 Correct 49 ms 70728 KB Output is correct
3 Correct 52 ms 70728 KB Output is correct
4 Correct 52 ms 71560 KB Output is correct
5 Correct 53 ms 71240 KB Output is correct
6 Correct 51 ms 71508 KB Output is correct
7 Correct 52 ms 71240 KB Output is correct
8 Correct 52 ms 71496 KB Output is correct
9 Correct 54 ms 71240 KB Output is correct
10 Correct 56 ms 71240 KB Output is correct
11 Correct 56 ms 71508 KB Output is correct
12 Correct 56 ms 71496 KB Output is correct
13 Correct 56 ms 71496 KB Output is correct
14 Correct 54 ms 70752 KB Output is correct
15 Correct 53 ms 70728 KB Output is correct
16 Correct 56 ms 71396 KB Output is correct
17 Correct 56 ms 71496 KB Output is correct
18 Correct 56 ms 71508 KB Output is correct
19 Correct 55 ms 71576 KB Output is correct
20 Correct 58 ms 71496 KB Output is correct
21 Correct 57 ms 71592 KB Output is correct
22 Correct 58 ms 71420 KB Output is correct
23 Correct 57 ms 71592 KB Output is correct
24 Correct 53 ms 71496 KB Output is correct
25 Correct 51 ms 71496 KB Output is correct
26 Correct 49 ms 71504 KB Output is correct
27 Correct 49 ms 71452 KB Output is correct
28 Correct 51 ms 71348 KB Output is correct
29 Correct 49 ms 70736 KB Output is correct
30 Correct 49 ms 71056 KB Output is correct
31 Correct 54 ms 70840 KB Output is correct
32 Correct 53 ms 70728 KB Output is correct
33 Correct 50 ms 70728 KB Output is correct
34 Correct 58 ms 70760 KB Output is correct
35 Correct 1598 ms 198572 KB Output is correct
36 Correct 1727 ms 198708 KB Output is correct
37 Correct 1743 ms 198860 KB Output is correct
38 Correct 1684 ms 198808 KB Output is correct
39 Correct 1550 ms 198708 KB Output is correct
40 Correct 44 ms 70984 KB Output is correct
41 Correct 46 ms 70992 KB Output is correct
42 Correct 880 ms 198576 KB Output is correct
43 Correct 970 ms 198504 KB Output is correct
44 Correct 1028 ms 198568 KB Output is correct
45 Correct 626 ms 198632 KB Output is correct
46 Correct 639 ms 198876 KB Output is correct
47 Correct 605 ms 198756 KB Output is correct
48 Correct 805 ms 198724 KB Output is correct
49 Correct 883 ms 198628 KB Output is correct
50 Correct 874 ms 198828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 70728 KB Output is correct
2 Correct 49 ms 70728 KB Output is correct
3 Correct 52 ms 70728 KB Output is correct
4 Correct 52 ms 71560 KB Output is correct
5 Correct 53 ms 71240 KB Output is correct
6 Correct 51 ms 71508 KB Output is correct
7 Correct 52 ms 71240 KB Output is correct
8 Correct 52 ms 71496 KB Output is correct
9 Correct 54 ms 71240 KB Output is correct
10 Correct 56 ms 71240 KB Output is correct
11 Correct 56 ms 71508 KB Output is correct
12 Correct 56 ms 71496 KB Output is correct
13 Correct 56 ms 71496 KB Output is correct
14 Correct 54 ms 70752 KB Output is correct
15 Correct 53 ms 70728 KB Output is correct
16 Correct 56 ms 71396 KB Output is correct
17 Correct 56 ms 71496 KB Output is correct
18 Correct 56 ms 71508 KB Output is correct
19 Correct 55 ms 71576 KB Output is correct
20 Correct 58 ms 71496 KB Output is correct
21 Correct 57 ms 71592 KB Output is correct
22 Correct 58 ms 71420 KB Output is correct
23 Correct 57 ms 71592 KB Output is correct
24 Correct 53 ms 71496 KB Output is correct
25 Correct 51 ms 71496 KB Output is correct
26 Correct 49 ms 71504 KB Output is correct
27 Correct 49 ms 71452 KB Output is correct
28 Correct 51 ms 71348 KB Output is correct
29 Correct 49 ms 70736 KB Output is correct
30 Correct 49 ms 71056 KB Output is correct
31 Correct 54 ms 70840 KB Output is correct
32 Correct 53 ms 70728 KB Output is correct
33 Correct 50 ms 70728 KB Output is correct
34 Correct 58 ms 70760 KB Output is correct
35 Correct 52 ms 71488 KB Output is correct
36 Correct 51 ms 71496 KB Output is correct
37 Correct 56 ms 71484 KB Output is correct
38 Correct 50 ms 71496 KB Output is correct
39 Correct 52 ms 71468 KB Output is correct
40 Correct 57 ms 71504 KB Output is correct
41 Correct 55 ms 71496 KB Output is correct
42 Correct 56 ms 71396 KB Output is correct
43 Correct 57 ms 71532 KB Output is correct
44 Correct 58 ms 71496 KB Output is correct
45 Correct 57 ms 71496 KB Output is correct
46 Correct 58 ms 71568 KB Output is correct
47 Correct 57 ms 71496 KB Output is correct
48 Correct 66 ms 71380 KB Output is correct
49 Correct 1598 ms 198572 KB Output is correct
50 Correct 1727 ms 198708 KB Output is correct
51 Correct 1743 ms 198860 KB Output is correct
52 Correct 1684 ms 198808 KB Output is correct
53 Correct 1550 ms 198708 KB Output is correct
54 Correct 44 ms 70984 KB Output is correct
55 Correct 46 ms 70992 KB Output is correct
56 Correct 880 ms 198576 KB Output is correct
57 Correct 970 ms 198504 KB Output is correct
58 Correct 1028 ms 198568 KB Output is correct
59 Correct 626 ms 198632 KB Output is correct
60 Correct 639 ms 198876 KB Output is correct
61 Correct 605 ms 198756 KB Output is correct
62 Correct 805 ms 198724 KB Output is correct
63 Correct 883 ms 198628 KB Output is correct
64 Correct 874 ms 198828 KB Output is correct
65 Correct 2373 ms 203892 KB Output is correct
66 Correct 2536 ms 203176 KB Output is correct
67 Correct 2843 ms 203168 KB Output is correct
68 Correct 2790 ms 203124 KB Output is correct
69 Correct 2591 ms 203260 KB Output is correct
70 Correct 891 ms 198388 KB Output is correct
71 Correct 959 ms 198564 KB Output is correct
72 Correct 1049 ms 198516 KB Output is correct
73 Correct 1055 ms 203084 KB Output is correct
74 Correct 1002 ms 203048 KB Output is correct
75 Correct 1028 ms 203052 KB Output is correct
76 Correct 1439 ms 202940 KB Output is correct
77 Correct 1459 ms 202924 KB Output is correct
78 Correct 1416 ms 202936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 70728 KB Output is correct
2 Correct 49 ms 70728 KB Output is correct
3 Correct 52 ms 70728 KB Output is correct
4 Correct 52 ms 71560 KB Output is correct
5 Correct 53 ms 71240 KB Output is correct
6 Correct 51 ms 71508 KB Output is correct
7 Correct 52 ms 71240 KB Output is correct
8 Correct 52 ms 71496 KB Output is correct
9 Correct 54 ms 71240 KB Output is correct
10 Correct 56 ms 71240 KB Output is correct
11 Correct 56 ms 71508 KB Output is correct
12 Correct 56 ms 71496 KB Output is correct
13 Correct 56 ms 71496 KB Output is correct
14 Correct 54 ms 70752 KB Output is correct
15 Correct 53 ms 70728 KB Output is correct
16 Correct 56 ms 71396 KB Output is correct
17 Correct 56 ms 71496 KB Output is correct
18 Correct 56 ms 71508 KB Output is correct
19 Correct 55 ms 71576 KB Output is correct
20 Correct 58 ms 71496 KB Output is correct
21 Correct 57 ms 71592 KB Output is correct
22 Correct 58 ms 71420 KB Output is correct
23 Correct 57 ms 71592 KB Output is correct
24 Correct 53 ms 71496 KB Output is correct
25 Correct 51 ms 71496 KB Output is correct
26 Correct 49 ms 71504 KB Output is correct
27 Correct 49 ms 71452 KB Output is correct
28 Correct 51 ms 71348 KB Output is correct
29 Correct 49 ms 70736 KB Output is correct
30 Correct 49 ms 71056 KB Output is correct
31 Correct 54 ms 70840 KB Output is correct
32 Correct 53 ms 70728 KB Output is correct
33 Correct 50 ms 70728 KB Output is correct
34 Correct 58 ms 70760 KB Output is correct
35 Correct 52 ms 71488 KB Output is correct
36 Correct 51 ms 71496 KB Output is correct
37 Correct 56 ms 71484 KB Output is correct
38 Correct 50 ms 71496 KB Output is correct
39 Correct 52 ms 71468 KB Output is correct
40 Correct 57 ms 71504 KB Output is correct
41 Correct 55 ms 71496 KB Output is correct
42 Correct 56 ms 71396 KB Output is correct
43 Correct 57 ms 71532 KB Output is correct
44 Correct 58 ms 71496 KB Output is correct
45 Correct 57 ms 71496 KB Output is correct
46 Correct 58 ms 71568 KB Output is correct
47 Correct 57 ms 71496 KB Output is correct
48 Correct 66 ms 71380 KB Output is correct
49 Incorrect 60 ms 71496 KB Output isn't correct
50 Halted 0 ms 0 KB -