結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-04-17 22:34:32
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 268 ms / 3,500 ms
コード長 2,948 bytes
コンパイル時間 2,192 ms
コンパイル使用メモリ 111,288 KB
実行使用メモリ 10,536 KB
最終ジャッジ日時 2023-09-04 07:24:17
合計ジャッジ時間 12,641 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 8 ms
4,380 KB
testcase_04 AC 8 ms
4,376 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 8 ms
4,380 KB
testcase_07 AC 8 ms
4,380 KB
testcase_08 AC 8 ms
4,376 KB
testcase_09 AC 8 ms
4,384 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 134 ms
6,676 KB
testcase_12 AC 132 ms
6,056 KB
testcase_13 AC 161 ms
6,188 KB
testcase_14 AC 139 ms
7,244 KB
testcase_15 AC 152 ms
7,048 KB
testcase_16 AC 131 ms
6,292 KB
testcase_17 AC 152 ms
6,248 KB
testcase_18 AC 146 ms
6,772 KB
testcase_19 AC 154 ms
7,128 KB
testcase_20 AC 162 ms
6,908 KB
testcase_21 AC 147 ms
6,432 KB
testcase_22 AC 131 ms
6,012 KB
testcase_23 AC 135 ms
6,196 KB
testcase_24 AC 154 ms
6,100 KB
testcase_25 AC 160 ms
6,324 KB
testcase_26 AC 150 ms
6,852 KB
testcase_27 AC 139 ms
5,972 KB
testcase_28 AC 161 ms
6,312 KB
testcase_29 AC 162 ms
6,296 KB
testcase_30 AC 162 ms
6,556 KB
testcase_31 AC 161 ms
6,280 KB
testcase_32 AC 163 ms
7,112 KB
testcase_33 AC 162 ms
6,376 KB
testcase_34 AC 268 ms
10,536 KB
testcase_35 AC 247 ms
8,060 KB
testcase_36 AC 247 ms
7,636 KB
testcase_37 AC 251 ms
8,180 KB
testcase_38 AC 237 ms
7,268 KB
testcase_39 AC 230 ms
7,092 KB
testcase_40 AC 236 ms
7,000 KB
testcase_41 AC 238 ms
7,588 KB
testcase_42 AC 237 ms
6,900 KB
testcase_43 AC 213 ms
6,480 KB
testcase_44 AC 210 ms
6,572 KB
testcase_45 AC 205 ms
7,128 KB
testcase_46 AC 250 ms
9,660 KB
testcase_47 AC 241 ms
7,584 KB
testcase_48 AC 252 ms
8,068 KB
testcase_49 AC 251 ms
7,512 KB
testcase_50 AC 249 ms
7,676 KB
testcase_51 AC 264 ms
8,632 KB
testcase_52 AC 265 ms
8,600 KB
testcase_53 AC 266 ms
8,652 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }




void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto P = new int[N];
      foreach (i; 0 .. N) {
        P[i] = readInt() - 1;
      }
      
      long ans;
      void solve(int l, int r) {
        if (r - l <= 1) {
          return;
        }
        const mid = (l + r) / 2;
        solve(l, mid);
        solve(mid, r);
        alias Entry = Tuple!(int, "mn", int, "mx");
        Entry[] ps, qs;
        {
          int mn = N, mx = -1;
          foreach_reverse (i; l .. mid) {
            chmax(mx, P[i]);
            if (chmin(mn, P[i])) {
              ps ~= Entry(mn, mx);
            }
          }
        }
        {
          int mn = N, mx = -1;
          foreach (i; mid .. r) {
            chmin(mn, P[i]);
            if (chmax(mx, P[i])) {
              qs ~= Entry(mn, mx);
            }
          }
        }
        debug {
          writeln("ps = ", ps);
          writeln("qs = ", qs);
        }
        foreach (ref p; ps) {
          /*
            q.mx >= p.mx
            q.mn >= p.mn
          */
          const lb = qs.binarySearch!(q => (q.mx >= p.mx));
          const ub = qs.binarySearch!(q => (q.mn < p.mn));
          if (lb < ub) {
            ans += (ub - lb);
          }
        }
      }
      
      foreach (phase; 0 .. 2) {
        solve(0, N);
        P.reverse;
      }
      writeln(ans);
      
      debug {
        long brt;
        foreach (i; 0 .. N) foreach (j; i + 1 .. N) {
          if (P[i] == P[i .. j + 1].minElement && P[j] == P[i .. j + 1].maxElement ||
              P[i] == P[i .. j + 1].maxElement && P[j] == P[i .. j + 1].minElement) {
            ++brt;
          }
        }
        writeln("brt = ", brt);
        assert(brt == ans);
      }
    }
  } catch (EOFException e) {
  }
}
0