Submission #501973

#TimeUsernameProblemLanguageResultExecution timeMemory
501973tengiz05Financial Report (JOI21_financial)C++17
100 / 100
1331 ms22328 KiB
#include <bits/stdc++.h>

using i64 = long long;

struct Info {
    int mx;
    Info(int mx = 0) : mx(mx) {}
};
Info operator+(const Info &a, const Info &b) {
    return Info(std::max(a.mx, b.mx));
}
struct Tag {
    int x;
    Tag(int x = -1) : x(x) {}
};
void apply(Tag &a, Tag b) {
    if (b.x != -1) {
        a.x = b.x;
    }
}
void apply(Info &a, Tag b) {
    if (b.x != -1) {
        a.mx = b.x;
    }
}

template<class Info, class Tag,
    class Merge = std::plus<Info>>
struct LazySegmentTree {
    const int n;
    const Merge merge;
    std::vector<Info> info;
    std::vector<Tag> tag;
    LazySegmentTree(int n) : n(n), merge(Merge()), info(4 << std::__lg(n)), tag(4 << std::__lg(n)) {}
    LazySegmentTree(std::vector<Info> init) : LazySegmentTree(init.size()) {
        std::function<void(int, int, int)> build = [&](int p, int l, int r) {
            if (r - l == 1) {
                info[p] = init[l];
                return;
            }
            int m = (l + r) / 2;
            build(2 * p, l, m);
            build(2 * p + 1, m, r);
            pull(p);
        };
        build(1, 0, n);
    }
    void pull(int p) {
        info[p] = merge(info[2 * p], info[2 * p + 1]);
    }
    void apply(int p, const Tag &v) {
        ::apply(info[p], v);
        ::apply(tag[p], v);
    }
    void push(int p) {
        apply(2 * p, tag[p]);
        apply(2 * p + 1, tag[p]);
        tag[p] = Tag();
    }
    void modify(int p, int l, int r, int x, Info v) {
        if (r - l == 1) {
            info[p] = v;
            return;
        }
        int m = (l + r) / 2;
        push(p);
        if (x < m) {
            modify(2 * p, l, m, x, v);
        } else {
            modify(2 * p + 1, m, r, x, v);
        }
        pull(p);
    }
    void modify(int x, Info v) {
        modify(1, 0, n, x, v);
    }
    Info rangeQuery(int p, int l, int r, int x, int y) {
        if (l >= y || r <= x) {
            return Info();
        }
        if (l >= x && r <= y) {
            return info[p];
        }
        int m = (l + r) / 2;
        push(p);
        return merge(rangeQuery(2 * p, l, m, x, y), rangeQuery(2 * p + 1, m, r, x, y));
    }
    Info rangeQuery(int l, int r) {
        if (l >= r) return Info();
        return rangeQuery(1, 0, n, l, r);
    }
    void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
        if (l >= y || r <= x) {
            return;
        }
        if (l >= x && r <= y) {
            apply(p, v);
            return;
        }
        int m = (l + r) / 2;
        push(p);
        rangeApply(2 * p, l, m, x, y, v);
        rangeApply(2 * p + 1, m, r, x, y, v);
        pull(p);
    }
    void rangeApply(int l, int r, const Tag &v) {
        if (l >= r) return;
        return rangeApply(1, 0, n, l, r, v);
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, d;
    std::cin >> n >> d;
    
    std::vector<int> a(n), f{-1};
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
        f.push_back(a[i]);
    }
    
    std::sort(f.begin(), f.end());
    f.erase(std::unique(f.begin(), f.end()), f.end());
    
    int m = f.size();
    
    for (int i = 0; i < n; i++) {
        a[i] = std::lower_bound(f.begin(), f.end(), a[i]) - f.begin();
    }
    
    LazySegmentTree<Info, Tag> s(m), t(m);
    
    for (int i = 0; i < n - 1; i++) {
        int x = s.rangeQuery(0, a[i]).mx;
        s.modify(a[i], std::max(s.rangeQuery(a[i], a[i] + 1).mx, x + 1));
        t.modify(a[i], i);
        int l = -1, r = m;
        while (l + 1 < r) {
            int m = (l + r) / 2;
            if (t.rangeQuery(0, m).mx <= i - d) {
                l = m;
            } else {
                r = m;
            }
        }
        s.rangeApply(0, l, Tag(0));
    }
    
    int x = s.rangeQuery(0, a[n - 1]).mx;
    int ans = std::max(s.rangeQuery(a[n - 1], m).mx, x + 1);
    
    std::cout << ans << "\n";
    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...