結果

問題 No.1687 What the Heck?
ユーザー tnakao0123tnakao0123
提出日時 2021-09-25 00:56:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,206 bytes
コンパイル時間 929 ms
コンパイル使用メモリ 96,200 KB
実行使用メモリ 6,624 KB
最終ジャッジ日時 2023-09-18 22:34:30
合計ジャッジ時間 2,546 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,572 KB
testcase_01 AC 3 ms
5,576 KB
testcase_02 WA -
testcase_03 AC 3 ms
5,512 KB
testcase_04 AC 3 ms
5,820 KB
testcase_05 AC 3 ms
5,504 KB
testcase_06 AC 3 ms
5,596 KB
testcase_07 AC 30 ms
5,860 KB
testcase_08 AC 41 ms
6,172 KB
testcase_09 WA -
testcase_10 AC 19 ms
5,752 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 88 ms
6,364 KB
testcase_14 AC 81 ms
6,284 KB
testcase_15 AC 83 ms
6,356 KB
testcase_16 AC 70 ms
6,408 KB
testcase_17 AC 7 ms
5,676 KB
testcase_18 AC 84 ms
6,372 KB
testcase_19 AC 4 ms
5,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1687.cc:  No.1687 What the Heck? - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_E2 = 1 << 19; // = 524288
const int INF = 1 << 30;

/* typedef */

typedef long long ll;

template <typename T, const int MAX_E2>
struct SegTreeMin {
  int e2;
  T nodes[MAX_E2], defv;
  SegTreeMin() {}

  void init(int n, T _defv) {
    defv = _defv;
    for (e2 = 1; e2 < n; e2 <<= 1);
    fill(nodes, nodes + MAX_E2, defv);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]);
    }
  }

  T min_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return defv;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = min_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = min_range(r0, r1, k * 2 + 2, im, i1);
    return min(v0, v1);
  }
  T min_range(int r0, int r1) { return min_range(r0, r1, 0, 0, e2); }
};

/* global variables */

int ps[MAX_N];
SegTreeMin<int,MAX_E2> st;

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", ps + i), ps[i]--;

  st.init(n, INF);
  for (int i = 0; i < n; i++) st.seti(i, i);
  st.setall();

  ll sum = 0;
  for (int i = n - 1; i >= 0; i--) {
    int v = st.min_range(ps[i] + 1, n);
    if (v < INF) {
      sum += i + 1;
      st.set(v, INF);
    }
    else if (st.geti(ps[i]) < INF) {
      st.set(ps[i], INF);
    }
    else {
      int w = st.min_range(0, n);
      sum -= i + 1;
      st.set(w, INF);
    }
  }

  printf("%lld\n", sum);
  return 0;
}
0