結果

問題 No.992 最長増加部分列の数え上げ
ユーザー tnakao0123tnakao0123
提出日時 2020-02-15 23:57:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 2,252 bytes
コンパイル時間 945 ms
コンパイル使用メモリ 96,624 KB
実行使用メモリ 9,400 KB
最終ジャッジ日時 2023-07-28 19:26:11
合計ジャッジ時間 7,151 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,620 KB
testcase_01 AC 4 ms
7,528 KB
testcase_02 AC 4 ms
7,668 KB
testcase_03 AC 4 ms
7,544 KB
testcase_04 AC 56 ms
8,160 KB
testcase_05 AC 40 ms
8,040 KB
testcase_06 AC 68 ms
8,380 KB
testcase_07 AC 48 ms
8,152 KB
testcase_08 AC 26 ms
8,000 KB
testcase_09 AC 50 ms
8,168 KB
testcase_10 AC 69 ms
8,288 KB
testcase_11 AC 85 ms
8,532 KB
testcase_12 AC 18 ms
7,832 KB
testcase_13 AC 47 ms
8,204 KB
testcase_14 AC 46 ms
8,136 KB
testcase_15 AC 18 ms
7,800 KB
testcase_16 AC 138 ms
8,984 KB
testcase_17 AC 26 ms
7,884 KB
testcase_18 AC 48 ms
8,076 KB
testcase_19 AC 86 ms
8,536 KB
testcase_20 AC 154 ms
9,400 KB
testcase_21 AC 153 ms
9,196 KB
testcase_22 AC 153 ms
9,148 KB
testcase_23 AC 156 ms
9,252 KB
testcase_24 AC 152 ms
9,124 KB
testcase_25 AC 146 ms
9,096 KB
testcase_26 AC 151 ms
9,196 KB
testcase_27 AC 153 ms
9,192 KB
testcase_28 AC 150 ms
9,164 KB
testcase_29 AC 152 ms
9,160 KB
testcase_30 AC 109 ms
9,276 KB
testcase_31 AC 107 ms
9,256 KB
testcase_32 AC 108 ms
9,336 KB
testcase_33 AC 106 ms
9,160 KB
testcase_34 AC 107 ms
9,148 KB
testcase_35 AC 101 ms
9,192 KB
testcase_36 AC 101 ms
9,276 KB
testcase_37 AC 101 ms
9,164 KB
testcase_38 AC 101 ms
9,116 KB
testcase_39 AC 101 ms
9,172 KB
testcase_40 AC 107 ms
9,192 KB
testcase_41 AC 108 ms
9,164 KB
testcase_42 AC 108 ms
9,260 KB
testcase_43 AC 111 ms
9,228 KB
testcase_44 AC 109 ms
9,164 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