algo

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub dnx04/algo

:heavy_check_mark: tests/LIS.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/longest_increasing_subsequence"

#include "../misc/macros.h"
#include "../misc/Compressor.h"
#include "../ds/SegTree.h"

void solve() {
  int n;
  cin >> n;
  vi a(n), tr(n, -1);
  for (auto& x : a) cin >> x;
  a = compressor(a);
  SegTree st(n, [&](pii a, pii b) { return max(a, b); }, pii{0, -1});
  st.apply(a[0], {1, 0});
  for (int i = 1; i < n; ++i) {
    auto [lis, idx] = st.query(0, a[i]);
    if (idx == -1)
      tr[i] = i;
    else
      tr[i] = idx;
    st.apply(a[i], {lis + 1, i});
  }
  auto [lis, u] = st.query(0, n);
  cout << lis << '\n';
  vi pos;
  while (true) {
    pos.eb(u);
    if (tr[u] == -1 || tr[u] == u) break;
    u = tr[u];
  }
  reverse(all(pos));
  for (auto u : pos) cout << u << ' ';
}

int main() {
  solve();
}
#line 1 "tests/LIS.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/longest_increasing_subsequence"

#line 1 "misc/macros.h"
// #pragma GCC optimize("Ofast,unroll-loops")       // unroll long, simple loops
// #pragma GCC target("avx2,fma")                   // vectorizing code
// #pragma GCC target("lzcnt,popcnt,abm,bmi,bmi2")  // for fast bitset operation

#include <bits/extc++.h>
#include <tr2/dynamic_bitset>

using namespace std;
using namespace __gnu_pbds;  // ordered_set, gp_hash_table
// using namespace __gnu_cxx; // rope

// for templates to work
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define pb push_back
#define eb emplace_back
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using ld = long double;
using pii = pair<i32, i32>;
using vi = vector<i32>;

// fast map
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {  // customize hash function for gp_hash_table
  int operator()(int x) const { return x ^ RANDOM; }
};
gp_hash_table<int, int, chash> table;

/* ordered set
    find_by_order(k): returns an iterator to the k-th element (0-based)
    order_of_key(k): returns the number of elements in the set that are strictly less than k
*/
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

/*  rope
    rope <int> cur = v.substr(l, r - l + 1);
    v.erase(l, r - l + 1);
    v.insert(v.mutable_begin(), cur);
*/
#line 1 "misc/Compressor.h"
template <class T>
vi compressor(vector<T>& v) {
  auto cv = v;
  sort(all(cv));
  cv.erase(unique(all(cv)), cv.end());
  for (auto& e : v) e = lower_bound(all(cv), e) - cv.begin();
  return v;
}
#line 1 "ds/SegTree.h"
// 0-indexed
template <class T, class F>
struct SegTree {
  int n, size;  // smallest size = 2^k >= n
  vector<T> seg;
  const F f;
  const T I;
  SegTree(int n, F f, const T& I) : n(n), f(f), I(I) {
    size = 1;
    while (size < n) size <<= 1;
    seg.assign(size << 1, I);
  }
  T& operator[](int k) { return seg[k + size]; }
  void set(int k, T x) { seg[k + size] = x; }  // to build
  void build() {
    for (int i = size - 1; i > 0; --i) seg[i] = f(seg[i << 1], seg[i << 1 | 1]);
  }
  void apply(int k, T x) {
    k += size, seg[k] = x;
    while (k >>= 1) seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
  }
  // query [l, r)
  T query(int l, int r) {
    T L = I, R = I;
    for (l += size, r += size; l < r; l >>= 1, r >>= 1) {
      if (l & 1) L = f(L, seg[l++]);
      if (r & 1) R = f(seg[--r], R);
    }
    return f(L, R);
  }
  template <class C>
  int max_right(int l, C check) {
    assert(0 <= l && l <= n && check(I) == true);
    if (l == n) return n;
    l += size;
    T sm = I;
    do {
      while (l % 2 == 0) l >>= 1;
      if (!check(f(sm, seg[l]))) {
        while (l < size) {
          l = l << 1;
          if (check(f(sm, seg[l]))) sm = f(sm, seg[l]), l++;
        }
        return l - size;
      }
      sm = f(sm, seg[l]), l++;
    } while ((l & -l) != l);
    return n;
  }
  template <class C>
  int min_left(int r, C check) {
    assert(0 <= r && r <= n && check(I) == true);
    if (r == 0) return 0;
    r += size;
    T sm = I;
    do {
      r--;
      while (r > 1 && (r % 2)) r >>= 1;
      if (!check(f(seg[r], sm))) {
        while (r < size) {
          r = r << 1 | 1;
          if (check(f(seg[r], sm))) sm = f(seg[r], sm), r--;
        }
        return r + 1 - size;
      }
      sm = f(seg[r], sm);
    } while ((r & -r) != r);
    return 0;
  }
};
#line 6 "tests/LIS.test.cpp"

void solve() {
  int n;
  cin >> n;
  vi a(n), tr(n, -1);
  for (auto& x : a) cin >> x;
  a = compressor(a);
  SegTree st(n, [&](pii a, pii b) { return max(a, b); }, pii{0, -1});
  st.apply(a[0], {1, 0});
  for (int i = 1; i < n; ++i) {
    auto [lis, idx] = st.query(0, a[i]);
    if (idx == -1)
      tr[i] = i;
    else
      tr[i] = idx;
    st.apply(a[i], {lis + 1, i});
  }
  auto [lis, u] = st.query(0, n);
  cout << lis << '\n';
  vi pos;
  while (true) {
    pos.eb(u);
    if (tr[u] == -1 || tr[u] == u) break;
    u = tr[u];
  }
  reverse(all(pos));
  for (auto u : pos) cout << u << ' ';
}

int main() {
  solve();
}
Back to top page