CSES Increasing Subsequence
// compile: make data
// run: ./data < data.in
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) _debug_print(#x, x);
#define Debug(x...) _debug_print_format(#x, x);
std::ifstream terminal("/dev/tty");
#define PP cerr<<"\033[1;30mpause...\e[0m",terminal.ignore();
#else
#define debug(x...)
#define Debug(x...)
#define PP
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define VI vector<int>
#define VII vector<vector<int>>
#define VIII vector<vector<vector<int>>>
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define FIND(a, x) ((find(a.begin(),a.end(),(x))!=a.end())?1:0)
#define cmin(x,...) x=min({(x), __VA_ARGS__})
#define cmax(x,...) x=max({(x), __VA_ARGS__})
#define INTMAX (int)(9223372036854775807)
#define INF (int)(1152921504606846976)
#define NaN (int)(0x8b88e1d0595d51d1)
#define double long double
#define int long long
#define uint unsigned long long
#define MAXN 200010

int corcomp(vector<int> &a) {
    int maxv = 0;
    vector<pair<int, int>> l(sz(a)), r(sz(a));
    rep(i, 0, sz(a)) l[i] = {a[i], i};
    sort(l.begin(), l.end());
    r[0] = {l[0].second, 0};
    rep(i, 1, sz(a)) {
        r[i].first = l[i].second;
        r[i].second = r[i-1].second + (l[i].first != l[i-1].first);
    }
    maxv = r.back().second;
    sort(r.begin(), r.end());
    rep(i, 0, sz(a)) a[i] = r[i].second;
    return maxv;
}

template <class T> struct segtree {
    struct node {
        int l, r;
        T val;
        node *ch[2];
    };
    int cnt = 0;
    T DEFAULT_VALUE;
    node *newnode(int l, int r) {
        node *x = (l==r) ? &datalist[l] : &datalist[cnt++];
        x->l = l, x->r = r;
        x->ch[0] = x->ch[1] = nullptr;
        return x;
    }
    vector<int> a;
    vector<node> datalist;
    node *root;
    segtree(vector<int> arr) {
        cnt = sz(arr);
        a = arr, build();
    }
    void pushup(node *x) {
        if (!x) return;
        if (!x->ch[0] && !x->ch[1]) return;
        if (x->ch[0] && x->ch[1]) x->val = x->ch[0]->val + x->ch[1]->val;
        else x->val = x->ch[!x->ch[0]]->val;
    }
    T query(int ql, int qr) {
        if (qr - ql < 10) {
            T res = DEFAULT_VALUE;
            rep(i, ql, qr+1) res = res + datalist[i].val;
            return res;
        }
        function<T(node*)> dfs = [&](node *x) -> T {
            if (ql > x->r || qr < x->l) return DEFAULT_VALUE;
            if (x->l >= ql && x->r <= qr) return x->val;
            return dfs(x->ch[0]) + dfs(x->ch[1]);
        };
        return dfs(root);
    }
    template <typename U> void modify(int q, U val) {
        function<void(node*)> dfs = [&](node *x) -> void {
            if (q > x->r || q < x->l) return;
            if (x->l >= q && x->r <= q) {
                x->val.update(val);
                return;
            }
            dfs(x->ch[0]), dfs(x->ch[1]);
            pushup(x);
        };
        dfs(root);
    }
    void build() {
        datalist.resize(sz(a) * 2);
        function<node*(int, int)> dfs = [&](int l, int r) {
            node *x = newnode(l, r);
            if (r > l) x->ch[0] = dfs(l, (l+r) >> 1), x->ch[1] = dfs(((l+r)>>1) + 1, r);
            else x->val.set(a[l]);
            pushup(x);
            return x;
        };
        root = dfs(0, sz(a)-1);
    }
    void graphviz_dump(string filename = "graph.dot") {
        FILE *f = fopen(filename.c_str(), "w");
        fprintf(f, "digraph {\n");
        rep(i, 0, cnt) {
            node *x = &datalist[i];
            fprintf(f, "    %lld;\n");
        }
        rep(i, 0, cnt) {
            node *x = &datalist[i];
            if (x->ch[0]) fprintf(f, "    %lld -> %ld;\n", i, x->ch[0] - &datalist[0]);
            if (x->ch[1]) fprintf(f, "    %lld -> %ld;\n", i, x->ch[1] - &datalist[0]);
        }
        fprintf(f, "}\n");
    }
};


int32_t main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    int n; cin >> n;
    vector<int> a(n);
    rep(i, 0, n) cin >> a[i];
    int N = corcomp(a);

    struct value {
        int maxv = -INF;
        int size;
        value operator+(const value &other) const {
            value res;
            res.maxv = max(maxv, other.maxv);
            return res;
        }
        void set(int rhs) {
            maxv = rhs;
        }
        void update(int val) {
            cmax(maxv, val);
        }
    };
    vector<int> cnt(N+1);
    cnt[a[0]] = 1;
    segtree<value> t(cnt);
    rep(i, 1, n) {
        int v = max(t.query(0, a[i]-1).maxv, 0ll);
        if (v + 1 < cnt[a[i]]) continue;
        cnt[a[i]] = v + 1;
        t.modify(a[i], v+1);
    }
    cout << t.query(0, N).maxv << endl;

    return 0;
}

Comments

  1. 11 months ago
    2025-12-06 2:41:30

    999jllogin, well alright. Seamless login, decent interface. Pretty happy with the whole experience so far. Why not log in yourself through 999jllogin?

  2. 11 months ago
    2025-12-19 13:56:11

    23winslot? Name kinda speaks for itself, right? Slots galore! Some of them are surprisingly fun, and payouts aren’t terrible. Worth a spin or two, I reckon. More info here: 23winslot

  3. 10 months ago
    2025-12-29 6:24:34

    Is mnl777legit really legit? Ang hirap magtiwala these days. Need real reviews from real players before I put any money in. Anybody can vouch for this? See for yourself: mnl777legit

  4. 7 months ago
    2026-4-21 11:22:29

    Need some tips for rong bach kim 88! Hit me with your best strategies! Trying to crack the code here, haha! rong bach kim 88

  5. 7 months ago
    2026-4-21 11:22:45

    Just a heads up, if you’re looking for results, 1 ketqua.net might lead you to ketqua04.info. Always double-check to be sure! Go get them results at 1 ketqua.net!

  6. 7 months ago
    2026-4-21 11:23:00

    Alright, gamers! Help a brother out. Searching for the Tiranga download APK. Need that sweet, sweet offline play. Know a legit place? Get yours now! tiranga download apk

  7. 2 months ago
    2026-8-27 13:17:36

    [4455]44bd Official Login | অনলাইন ক্যাসিনো বোনাস ও দ্রুত পেমেন্ট,44bd এর অফিসিয়াল লগইন করে উপভোগ করুন সেরা স্লট গেম এবং আকর্ষণীয় বোনাস। দ্রুত বিকাশ পেমেন্টের সুবিধা পেতে আজই অ্যাপটি ডাউনলোড করুন। visit: 44bd

  8. 2 months ago
    2026-9-09 20:24:09

    Solid experience overall. The graphics are crisp and the navigation is very intuitive for new players. Love 4444wim

  9. 2 months ago
    2026-9-17 1:10:37

    Best place for those who love big wins and great bonuses. The customer support is actually helpful and responds quickly. Give bdjuya777 a try!

  10. 1 month ago
    2026-9-27 6:03:12

    Best experience I’ve had in a long time. The community is friendly and the wins are consistent. I’m sticking with idkastil89 from now on.

  11. 1 month ago
    2026-9-27 16:26:10

    Really impressed with the fast payouts here. The interface is smooth and I had a great run last night. Definitely checking out krwinbet30 again soon!

  12. 1 month ago
    2026-10-03 5:30:46

    [3884]CB999 | অনলাইন ক্যাসিনো প্ল্যাটফর্ম | অ্যাপ ডাউনলোড,বাংলাদেশে অনলাইন গ্যাম্বলিং সাইট CB999 ব্যবহারকারী খেলোয়াড়রা এখানে অ্যাকাউন্ট খোলার নিয়ম জেনে রেজিস্ট্রেশন এবং লগইন করতে পারেন, পাশাপাশি অ্যাপ ডাউনলোড করে ক্যাসিনো গেম খেলতে এবং প্রমোশনগুলো চেক করতে পারেন। visit: cb999

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
Previous
Next