답안 #1113225

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1113225 2024-11-16T05:06:32 Z VinhLuu Real Mountains (CCO23_day1problem2) C++17
10 / 25
1052 ms 186800 KB
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define all(lpv) lpv.begin(), lpv.end()
#define fi first
#define se second
using namespace std;

typedef pair<int,int> pii;
const int N = 1e6 + 5;
const int oo = 1e16;
const int mod = 1e6 + 3;

int n, m, a[N], pf[N], sf[N], st[N << 1], des[N], le[N], ri[N];

void update(int i,int x){
  i += n - 1;
  st[i] = x;
  while(i > 1){
    i /= 2;
    st[i] = min(st[i << 1], st[i << 1|1]);
  }
}

int get(int l,int r){
  if(l > r) return oo;
  r++;
  int ret = oo;
  for(l += n - 1, r += n - 1; l < r; l /= 2, r /= 2){
    if(l & 1) ret = min(ret, st[l ++]);
    if(r & 1) ret = min(ret, st[-- r]);
  }
  return ret;
}

void add(int &x,int y){
  x = (x + y) % mod;
}
vector<int> open[N], close[N], rrh;
int spe;

int lt(int u,int v){
  if(!v) return 1;
  int x = u;
  int kq = 1;
  while(v){
    if(v & 1) kq = kq * x % mod;
    x = x * x % mod;
    v /= 2;
  }
  return kq;
}

int cal(int l,int r){
  return (l + r) * spe % mod * (r - l + 1) % mod % mod;
}

signed main(){
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #define task "v"
  if(fopen(task ".inp","r")){
    freopen(task ".inp","r",stdin);
    freopen(task ".out","w",stdout);
  }

  cin >> n;

  spe = lt(2ll, mod - 2);

  for(int i = 1; i <= n; i ++){
    cin >> a[i];
    rrh.push_back(a[i]);
  }

  for(int i = 1; i <= n; i ++){
    le[i] = le[i - 1] + (pf[i] > a[i] ? pf[i] - a[i] : 0);
    pf[i] = max(a[i], pf[i - 1]);
    update(i, a[i]);
  }
  for(int i = n; i >= 1; i --){
    ri[i] = ri[i + 1] + (sf[i] > a[i] ? sf[i] - a[i] : 0);
    sf[i] = max(sf[i + 1], a[i]);
  }
  int pos = 1, ans = oo;
  for(int i = 1; i <= n; i ++){
    int val = max(pf[i], sf[i]) - a[i] + le[i - 1] + ri[i + 1];
    if(val < ans){
      ans = val;
      pos = i;
    }
  }
  sort(all(rrh)); rrh.resize(unique(all(rrh)) - rrh.begin());
  m = (int)rrh.size();
  for(int i = 1; i <= n; i ++) a[i] = lower_bound(all(rrh), a[i]) - rrh.begin() + 1;
  ans = 0;
  des[pos] = lower_bound(all(rrh), max(pf[pos], sf[pos])) - rrh.begin() + 1;
  for(int i = 1; i < pos; i ++) des[i] = lower_bound(all(rrh), pf[i]) - rrh.begin() + 1;
  for(int i = pos + 1; i <= n; i ++) des[i] = lower_bound(all(rrh), sf[i]) - rrh.begin() + 1;

  for(int i = 1; i <= n; i ++){
//    cerr << i << " " << a[i] << " " << des[i] << " p\n";
    if(a[i] < des[i]){
      open[a[i]].push_back(i);
    }
    close[des[i]].push_back(i);

  }

  ans = 0;

  set<int> col;

  for(int k = 1; k <= m; k ++){
    for(auto j : close[k]){
      if(col.find(j) != col.end()) col.erase(j);
      update(j, oo);
    }
    for(auto j : open[k]){
      col.insert(j);
      update(j, oo);
    }
    if(col.empty()) continue;
    int tmp = rrh[k - 1], nxt = rrh[k];
    int x = (*col.begin());
    int y = (*col.rbegin());
    if(x == y){
      int val = ((get(1, x - 1) + get(x + 1, n)) * (nxt - tmp) % mod + cal(tmp, nxt - 1)) % mod;
      add(ans, val);
    }else{
      int cx = ((get(1, x - 1) + get(x + 1, n)) * (nxt - tmp) % mod + cal(tmp, nxt - 1)) % mod;
      int cy = ((get(1, y - 1) + get(y + 1, n)) * (nxt - tmp) % mod + cal(tmp, nxt - 1)) % mod;
//      cerr << cx << " " << cy << " q\n";
      int val = min(cx + 2 * cal(tmp, nxt - 1) % mod + (nxt - tmp) + get(y + 1, n) * (nxt - tmp) % mod,
                    cy + 2 * cal(tmp, nxt - 1) % mod + (nxt - tmp) + get(1, x - 1) * (nxt - tmp) % mod);
//      cerr << val << " " << cx << " " << 2 * cal(tmp, nxt - 1) % mod + (nxt - tmp) << " " << get(y + 1, n) << " r\n";
      add(ans, val % mod);
    }

//    cerr << k << " " << ans << " r\n";

    int cnt = (int)col.size();

    int val = max(0ll, cnt - 2) * (3 * cal(tmp, nxt - 1) % mod + 2 * (nxt - tmp) % mod) % mod;
    add(ans, val);

  }
  cout << ans;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:62:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |     freopen(task ".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:63:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |     freopen(task ".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 47432 KB Output is correct
2 Correct 29 ms 47440 KB Output is correct
3 Correct 29 ms 47440 KB Output is correct
4 Correct 36 ms 47952 KB Output is correct
5 Correct 30 ms 47952 KB Output is correct
6 Correct 34 ms 47952 KB Output is correct
7 Correct 34 ms 47812 KB Output is correct
8 Correct 37 ms 47924 KB Output is correct
9 Correct 35 ms 47696 KB Output is correct
10 Correct 37 ms 47696 KB Output is correct
11 Correct 35 ms 47952 KB Output is correct
12 Correct 34 ms 47944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 47432 KB Output is correct
2 Correct 29 ms 47440 KB Output is correct
3 Correct 29 ms 47440 KB Output is correct
4 Correct 36 ms 47952 KB Output is correct
5 Correct 30 ms 47952 KB Output is correct
6 Correct 34 ms 47952 KB Output is correct
7 Correct 34 ms 47812 KB Output is correct
8 Correct 37 ms 47924 KB Output is correct
9 Correct 35 ms 47696 KB Output is correct
10 Correct 37 ms 47696 KB Output is correct
11 Correct 35 ms 47952 KB Output is correct
12 Correct 34 ms 47944 KB Output is correct
13 Correct 34 ms 47944 KB Output is correct
14 Correct 33 ms 47420 KB Output is correct
15 Correct 32 ms 47432 KB Output is correct
16 Correct 36 ms 48140 KB Output is correct
17 Correct 36 ms 47944 KB Output is correct
18 Correct 38 ms 47992 KB Output is correct
19 Correct 35 ms 47944 KB Output is correct
20 Correct 36 ms 47952 KB Output is correct
21 Correct 34 ms 47952 KB Output is correct
22 Correct 35 ms 47952 KB Output is correct
23 Correct 35 ms 48128 KB Output is correct
24 Correct 40 ms 47944 KB Output is correct
25 Correct 37 ms 47952 KB Output is correct
26 Correct 33 ms 47996 KB Output is correct
27 Correct 38 ms 47988 KB Output is correct
28 Correct 36 ms 47944 KB Output is correct
29 Correct 34 ms 47424 KB Output is correct
30 Correct 33 ms 47440 KB Output is correct
31 Correct 34 ms 47432 KB Output is correct
32 Correct 34 ms 47440 KB Output is correct
33 Correct 34 ms 47228 KB Output is correct
34 Correct 35 ms 47436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 47432 KB Output is correct
2 Correct 29 ms 47440 KB Output is correct
3 Correct 29 ms 47440 KB Output is correct
4 Correct 36 ms 47952 KB Output is correct
5 Correct 30 ms 47952 KB Output is correct
6 Correct 34 ms 47952 KB Output is correct
7 Correct 34 ms 47812 KB Output is correct
8 Correct 37 ms 47924 KB Output is correct
9 Correct 35 ms 47696 KB Output is correct
10 Correct 37 ms 47696 KB Output is correct
11 Correct 35 ms 47952 KB Output is correct
12 Correct 34 ms 47944 KB Output is correct
13 Correct 34 ms 47944 KB Output is correct
14 Correct 33 ms 47420 KB Output is correct
15 Correct 32 ms 47432 KB Output is correct
16 Correct 36 ms 48140 KB Output is correct
17 Correct 36 ms 47944 KB Output is correct
18 Correct 38 ms 47992 KB Output is correct
19 Correct 35 ms 47944 KB Output is correct
20 Correct 36 ms 47952 KB Output is correct
21 Correct 34 ms 47952 KB Output is correct
22 Correct 35 ms 47952 KB Output is correct
23 Correct 35 ms 48128 KB Output is correct
24 Correct 40 ms 47944 KB Output is correct
25 Correct 37 ms 47952 KB Output is correct
26 Correct 33 ms 47996 KB Output is correct
27 Correct 38 ms 47988 KB Output is correct
28 Correct 36 ms 47944 KB Output is correct
29 Correct 34 ms 47424 KB Output is correct
30 Correct 33 ms 47440 KB Output is correct
31 Correct 34 ms 47432 KB Output is correct
32 Correct 34 ms 47440 KB Output is correct
33 Correct 34 ms 47228 KB Output is correct
34 Correct 35 ms 47436 KB Output is correct
35 Incorrect 36 ms 48208 KB Output isn't correct
36 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 47432 KB Output is correct
2 Correct 29 ms 47440 KB Output is correct
3 Correct 29 ms 47440 KB Output is correct
4 Correct 36 ms 47952 KB Output is correct
5 Correct 30 ms 47952 KB Output is correct
6 Correct 34 ms 47952 KB Output is correct
7 Correct 34 ms 47812 KB Output is correct
8 Correct 37 ms 47924 KB Output is correct
9 Correct 35 ms 47696 KB Output is correct
10 Correct 37 ms 47696 KB Output is correct
11 Correct 35 ms 47952 KB Output is correct
12 Correct 34 ms 47944 KB Output is correct
13 Correct 34 ms 47944 KB Output is correct
14 Correct 33 ms 47420 KB Output is correct
15 Correct 32 ms 47432 KB Output is correct
16 Correct 36 ms 48140 KB Output is correct
17 Correct 36 ms 47944 KB Output is correct
18 Correct 38 ms 47992 KB Output is correct
19 Correct 35 ms 47944 KB Output is correct
20 Correct 36 ms 47952 KB Output is correct
21 Correct 34 ms 47952 KB Output is correct
22 Correct 35 ms 47952 KB Output is correct
23 Correct 35 ms 48128 KB Output is correct
24 Correct 40 ms 47944 KB Output is correct
25 Correct 37 ms 47952 KB Output is correct
26 Correct 33 ms 47996 KB Output is correct
27 Correct 38 ms 47988 KB Output is correct
28 Correct 36 ms 47944 KB Output is correct
29 Correct 34 ms 47424 KB Output is correct
30 Correct 33 ms 47440 KB Output is correct
31 Correct 34 ms 47432 KB Output is correct
32 Correct 34 ms 47440 KB Output is correct
33 Correct 34 ms 47228 KB Output is correct
34 Correct 35 ms 47436 KB Output is correct
35 Incorrect 36 ms 48208 KB Output isn't correct
36 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 47432 KB Output is correct
2 Correct 29 ms 47440 KB Output is correct
3 Correct 29 ms 47440 KB Output is correct
4 Correct 36 ms 47952 KB Output is correct
5 Correct 30 ms 47952 KB Output is correct
6 Correct 34 ms 47952 KB Output is correct
7 Correct 34 ms 47812 KB Output is correct
8 Correct 37 ms 47924 KB Output is correct
9 Correct 35 ms 47696 KB Output is correct
10 Correct 37 ms 47696 KB Output is correct
11 Correct 35 ms 47952 KB Output is correct
12 Correct 34 ms 47944 KB Output is correct
13 Correct 34 ms 47944 KB Output is correct
14 Correct 33 ms 47420 KB Output is correct
15 Correct 32 ms 47432 KB Output is correct
16 Correct 36 ms 48140 KB Output is correct
17 Correct 36 ms 47944 KB Output is correct
18 Correct 38 ms 47992 KB Output is correct
19 Correct 35 ms 47944 KB Output is correct
20 Correct 36 ms 47952 KB Output is correct
21 Correct 34 ms 47952 KB Output is correct
22 Correct 35 ms 47952 KB Output is correct
23 Correct 35 ms 48128 KB Output is correct
24 Correct 40 ms 47944 KB Output is correct
25 Correct 37 ms 47952 KB Output is correct
26 Correct 33 ms 47996 KB Output is correct
27 Correct 38 ms 47988 KB Output is correct
28 Correct 36 ms 47944 KB Output is correct
29 Correct 34 ms 47424 KB Output is correct
30 Correct 33 ms 47440 KB Output is correct
31 Correct 34 ms 47432 KB Output is correct
32 Correct 34 ms 47440 KB Output is correct
33 Correct 34 ms 47228 KB Output is correct
34 Correct 35 ms 47436 KB Output is correct
35 Correct 1028 ms 186800 KB Output is correct
36 Correct 969 ms 186420 KB Output is correct
37 Correct 928 ms 186612 KB Output is correct
38 Correct 1052 ms 186668 KB Output is correct
39 Correct 939 ms 186436 KB Output is correct
40 Correct 35 ms 47440 KB Output is correct
41 Correct 34 ms 47440 KB Output is correct
42 Correct 540 ms 183472 KB Output is correct
43 Correct 559 ms 184232 KB Output is correct
44 Correct 523 ms 184740 KB Output is correct
45 Correct 499 ms 186788 KB Output is correct
46 Correct 509 ms 186788 KB Output is correct
47 Correct 488 ms 186700 KB Output is correct
48 Correct 617 ms 186788 KB Output is correct
49 Correct 614 ms 186528 KB Output is correct
50 Correct 663 ms 186332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 47432 KB Output is correct
2 Correct 29 ms 47440 KB Output is correct
3 Correct 29 ms 47440 KB Output is correct
4 Correct 36 ms 47952 KB Output is correct
5 Correct 30 ms 47952 KB Output is correct
6 Correct 34 ms 47952 KB Output is correct
7 Correct 34 ms 47812 KB Output is correct
8 Correct 37 ms 47924 KB Output is correct
9 Correct 35 ms 47696 KB Output is correct
10 Correct 37 ms 47696 KB Output is correct
11 Correct 35 ms 47952 KB Output is correct
12 Correct 34 ms 47944 KB Output is correct
13 Correct 34 ms 47944 KB Output is correct
14 Correct 33 ms 47420 KB Output is correct
15 Correct 32 ms 47432 KB Output is correct
16 Correct 36 ms 48140 KB Output is correct
17 Correct 36 ms 47944 KB Output is correct
18 Correct 38 ms 47992 KB Output is correct
19 Correct 35 ms 47944 KB Output is correct
20 Correct 36 ms 47952 KB Output is correct
21 Correct 34 ms 47952 KB Output is correct
22 Correct 35 ms 47952 KB Output is correct
23 Correct 35 ms 48128 KB Output is correct
24 Correct 40 ms 47944 KB Output is correct
25 Correct 37 ms 47952 KB Output is correct
26 Correct 33 ms 47996 KB Output is correct
27 Correct 38 ms 47988 KB Output is correct
28 Correct 36 ms 47944 KB Output is correct
29 Correct 34 ms 47424 KB Output is correct
30 Correct 33 ms 47440 KB Output is correct
31 Correct 34 ms 47432 KB Output is correct
32 Correct 34 ms 47440 KB Output is correct
33 Correct 34 ms 47228 KB Output is correct
34 Correct 35 ms 47436 KB Output is correct
35 Incorrect 36 ms 48208 KB Output isn't correct
36 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 47432 KB Output is correct
2 Correct 29 ms 47440 KB Output is correct
3 Correct 29 ms 47440 KB Output is correct
4 Correct 36 ms 47952 KB Output is correct
5 Correct 30 ms 47952 KB Output is correct
6 Correct 34 ms 47952 KB Output is correct
7 Correct 34 ms 47812 KB Output is correct
8 Correct 37 ms 47924 KB Output is correct
9 Correct 35 ms 47696 KB Output is correct
10 Correct 37 ms 47696 KB Output is correct
11 Correct 35 ms 47952 KB Output is correct
12 Correct 34 ms 47944 KB Output is correct
13 Correct 34 ms 47944 KB Output is correct
14 Correct 33 ms 47420 KB Output is correct
15 Correct 32 ms 47432 KB Output is correct
16 Correct 36 ms 48140 KB Output is correct
17 Correct 36 ms 47944 KB Output is correct
18 Correct 38 ms 47992 KB Output is correct
19 Correct 35 ms 47944 KB Output is correct
20 Correct 36 ms 47952 KB Output is correct
21 Correct 34 ms 47952 KB Output is correct
22 Correct 35 ms 47952 KB Output is correct
23 Correct 35 ms 48128 KB Output is correct
24 Correct 40 ms 47944 KB Output is correct
25 Correct 37 ms 47952 KB Output is correct
26 Correct 33 ms 47996 KB Output is correct
27 Correct 38 ms 47988 KB Output is correct
28 Correct 36 ms 47944 KB Output is correct
29 Correct 34 ms 47424 KB Output is correct
30 Correct 33 ms 47440 KB Output is correct
31 Correct 34 ms 47432 KB Output is correct
32 Correct 34 ms 47440 KB Output is correct
33 Correct 34 ms 47228 KB Output is correct
34 Correct 35 ms 47436 KB Output is correct
35 Incorrect 36 ms 48208 KB Output isn't correct
36 Halted 0 ms 0 KB -