結果

問題 No.992 最長増加部分列の数え上げ
ユーザー tnakao0123tnakao0123
提出日時 2020-02-15 23:57:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 2,252 bytes
コンパイル時間 1,114 ms
コンパイル使用メモリ 99,632 KB
実行使用メモリ 9,572 KB
最終ジャッジ日時 2024-04-16 03:22:10
合計ジャッジ時間 6,984 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,064 KB
testcase_01 AC 4 ms
8,064 KB
testcase_02 AC 4 ms
7,680 KB
testcase_03 AC 4 ms
7,904 KB
testcase_04 AC 56 ms
8,320 KB
testcase_05 AC 40 ms
8,448 KB
testcase_06 AC 70 ms
8,704 KB
testcase_07 AC 52 ms
8,192 KB
testcase_08 AC 26 ms
8,120 KB
testcase_09 AC 50 ms
8,448 KB
testcase_10 AC 71 ms
8,704 KB
testcase_11 AC 89 ms
8,576 KB
testcase_12 AC 19 ms
8,192 KB
testcase_13 AC 47 ms
8,448 KB
testcase_14 AC 48 ms
8,300 KB
testcase_15 AC 19 ms
7,888 KB
testcase_16 AC 151 ms
9,080 KB
testcase_17 AC 25 ms
8,192 KB
testcase_18 AC 47 ms
8,448 KB
testcase_19 AC 88 ms
8,704 KB
testcase_20 AC 161 ms
9,344 KB
testcase_21 AC 160 ms
9,444 KB
testcase_22 AC 161 ms
9,288 KB
testcase_23 AC 162 ms
9,196 KB
testcase_24 AC 163 ms
9,344 KB
testcase_25 AC 160 ms
9,312 KB
testcase_26 AC 166 ms
9,344 KB
testcase_27 AC 165 ms
9,344 KB
testcase_28 AC 161 ms
9,564 KB
testcase_29 AC 160 ms
9,572 KB
testcase_30 AC 108 ms
9,472 KB
testcase_31 AC 109 ms
9,308 KB
testcase_32 AC 108 ms
9,440 KB
testcase_33 AC 107 ms
9,264 KB
testcase_34 AC 106 ms
9,472 KB
testcase_35 AC 101 ms
9,472 KB
testcase_36 AC 101 ms
9,344 KB
testcase_37 AC 100 ms
9,472 KB
testcase_38 AC 100 ms
9,344 KB
testcase_39 AC 101 ms
9,216 KB
testcase_40 AC 108 ms
9,344 KB
testcase_41 AC 110 ms
9,344 KB
testcase_42 AC 110 ms
9,216 KB
testcase_43 AC 112 ms
9,472 KB
testcase_44 AC 112 ms
9,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 992.cc:  No.992 最長増加部分列の数え上げ - 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 MOD = 1000000007;
const int INF = 1 << 30;

/* typedef */

struct Elm {
  int l, n;
  Elm() {}
  Elm(int _l, int _n): l(_l), n(_n) {}

  Elm operator+(const Elm &e) const {
    if (l > e.l) return *this;
    if (l < e.l) return e;
    return Elm(l, (n + e.n) % MOD);
  }

  void print() { printf("(%d,%d)", l, n); }
};

template <typename T, const int MAX_E2>
struct SegTreeSum {
  int n, e2;
  T nodes[MAX_E2], z;
  SegTreeSum() {}

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

  T &get(int i) { return nodes[e2 - 1 + i]; }

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

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

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

/* global variables */

int as[MAX_N], xs[MAX_N + 1];
SegTreeSum<Elm,MAX_E2> st;

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) scanf("%d", as + i), xs[i] = as[i];
  xs[n] = -INF;
  sort(xs, xs + n + 1);
  int m = unique(xs, xs + n + 1) - xs;

  st.init(m, Elm(0, 0));
  st.set(0, Elm(0, 1));

  for (int i = 0; i < n; i++) {
    int xi = lower_bound(xs, xs + m, as[i]) - xs;
    Elm e0 = st.sum_range(0, xi);
    e0.l++;
    Elm e = e0 + st.get(xi);
    st.set(xi, e);
  }

  printf("%d\n", st.nodes[0].n);
  return 0;
}
0