結果

問題 No.1079 まお
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-06-12 23:12:12
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 907 ms / 2,000 ms
コード長 4,498 bytes
コンパイル時間 1,131 ms
コンパイル使用メモリ 133,236 KB
実行使用メモリ 50,296 KB
最終ジャッジ日時 2024-06-22 07:23:47
合計ジャッジ時間 17,482 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 6 ms
6,940 KB
testcase_06 AC 6 ms
6,940 KB
testcase_07 AC 15 ms
6,944 KB
testcase_08 AC 15 ms
6,940 KB
testcase_09 AC 15 ms
6,944 KB
testcase_10 AC 17 ms
6,940 KB
testcase_11 AC 15 ms
6,940 KB
testcase_12 AC 405 ms
26,704 KB
testcase_13 AC 506 ms
38,168 KB
testcase_14 AC 658 ms
48,152 KB
testcase_15 AC 734 ms
47,024 KB
testcase_16 AC 826 ms
46,172 KB
testcase_17 AC 862 ms
49,032 KB
testcase_18 AC 860 ms
43,672 KB
testcase_19 AC 853 ms
43,728 KB
testcase_20 AC 869 ms
48,380 KB
testcase_21 AC 838 ms
48,768 KB
testcase_22 AC 851 ms
48,576 KB
testcase_23 AC 871 ms
49,192 KB
testcase_24 AC 888 ms
50,296 KB
testcase_25 AC 907 ms
49,360 KB
testcase_26 AC 868 ms
49,764 KB
testcase_27 AC 538 ms
15,144 KB
testcase_28 AC 752 ms
38,076 KB
testcase_29 AC 851 ms
49,148 KB
testcase_30 AC 858 ms
49,208 KB
testcase_31 AC 570 ms
14,032 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();
      const K = readLong();
      auto A = new long[N];
      foreach (i; 0 .. N) {
        A[i] = readLong();
      }
      
      long ans;
      foreach (i; 0 .. N) {
        if (A[i] + A[i] == K) {
          ans += 1;
        }
      }
      
      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!(long, "val", int, "pos", int, "cnt");
        Entry[] ps, qs;
        {
          long mn = long.max;
          int cnt;
          foreach_reverse (i; l .. mid) {
            if (chmin(mn, A[i])) cnt = 0;
            if (mn == A[i]) ++cnt;
            ps ~= Entry(mn, i, cnt);
          }
        }
        {
          long mn = long.max;
          int cnt;
          foreach (i; mid .. r) {
            if (chmin(mn, A[i])) cnt = 0;
            if (mn == A[i]) ++cnt;
            qs ~= Entry(mn, i, cnt);
          }
        }
        long[] vals;
        foreach (ref p; ps) vals ~= p.val;
        foreach (ref q; qs) vals ~= q.val;
        vals = vals.sort.uniq.array;
        debug {
          // writeln("solve ", l, " ", mid, " ", r);
          // writeln("  ps = ", ps);
          // writeln("  qs = ", qs);
        }
        const psLen = cast(int)(ps.length);
        const qsLen = cast(int)(qs.length);
        int kp, kq;
        long[long] s0P, s1P, s0Q, s1Q;
        foreach_reverse (val; vals) {
          int lp, lq;
          for (lp = kp; lp < psLen && ps[lp].val == val; ++lp) {}
          for (lq = kq; lq < qsLen && qs[lq].val == val; ++lq) {}
          debug {
            // writeln("  ", val, " ", [kp, lp], " ", [kq, lq]);
          }
          foreach (m; kp .. lp) {
            if (ps[m].cnt == 1) {
              const i = ps[m].pos;
              if ((K - A[i]) in s0Q) {
                debug {
                  // writeln("    P ", ps[m], " ", (s1Q[K - A[i]] - s0Q[K - A[i]] * (i - 1)));
                }
                ans += (s1Q[K - A[i]] - s0Q[K - A[i]] * (i - 1));
              }
            }
          }
          foreach (m; kq .. lq) {
            if (qs[m].cnt == 1) {
              const i = qs[m].pos;
              if ((K - A[i]) in s0P) {
                debug {
                  // writeln("    Q ", qs[m], " ", (s0P[K - A[i]] * (i + 1) - s1P[K - A[i]]));
                }
                ans += (s0P[K - A[i]] * (i + 1) - s1P[K - A[i]]);
              }
            }
          }
          foreach (m; kp .. lp) {
            const i = ps[m].pos;
            s0P[A[i]] += 1;
            s1P[A[i]] += i;
          }
          foreach (m; kq .. lq) {
            const i = qs[m].pos;
            s0Q[A[i]] += 1;
            s1Q[A[i]] += i;
          }
          kp = lp;
          kq = lq;
        }
      }
      
      solve(0, N);
      writeln(ans);
      
      debug {
        long brt;
        foreach (i; 0 .. N) foreach (j; i .. N) {
          if (A[i] + A[j] == K) {
            auto asBrt = A[i .. j + 1].dup.sort;
            if (asBrt.length == 1 || asBrt[0] < asBrt[1]) {
              brt += (j - i + 1);
            }
          }
        }
        writeln("brt = ", brt);
        assert(brt == ans);
      }
    }
  } catch (EOFException e) {
  }
}
0