Submission #1316118

#TimeUsernameProblemLanguageResultExecution timeMemory
1316118khanhphucscratchGlobal Warming (CEOI18_glo)C++20
100 / 100
259 ms32844 KiB
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct Segtree
{
    int n, st[1600020]; /**/
    Segtree(int n): n(n){
        memset(st, 0, sizeof(st));
    }
    void update(int node, int l, int r, int p, int v)
    {
        if(l > p || r < p) return;
        if(l == p && r == p) st[node] = max(st[node], v);
        else{
            update(node*2, l, (l+r)/2, p, v);
            update(node*2+1, (l+r)/2+1, r, p, v);
            st[node] = max(st[node*2], st[node*2+1]);
        }
    }
    int query(int node, int l, int r, int tl, int tr)
    {
        if(l > tr || r < tl) return 0;
        if(l >= tl && r <= tr) return st[node];
        else return max(query(node*2, l, (l+r)/2, tl, tr), query(node*2+1, (l+r)/2+1, r, tl, tr));
    }
};
int a[200005], b[200005];
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, x; cin>>n>>x;
    for(int i = 1; i <= n; i++){
        cin>>a[i]; b[i] = a[i]+x;
    }
    vector<int> vec;
    for(int i = 1; i <= n; i++){
        vec.push_back(a[i]);
        vec.push_back(b[i]);
    }
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    for(int i = 1; i <= n; i++){
        a[i] = upper_bound(vec.begin(), vec.end(), a[i]) - vec.begin();
        b[i] = upper_bound(vec.begin(), vec.end(), b[i]) - vec.begin();
    }
    Segtree st0(2*n), st1(2*n);
    int m = 2*n, ans = 0;
    for(int i = 1; i <= n; i++){
        int dp0 = st0.query(1, 1, m, 1, a[i]-1)+1;
        int dp1 = max(st0.query(1, 1, m, 1, b[i]-1), st1.query(1, 1, m, 1, b[i]-1))+1;
        ans = max(ans, max(dp0, dp1));
        st0.update(1, 1, m, a[i], dp0);
        st1.update(1, 1, m, b[i], dp1);
    }
    cout<<ans;
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...