Submission #1158266

#TimeUsernameProblemLanguageResultExecution timeMemory
1158266vladiliusMatryoshka (JOI16_matryoshka)C++20
100 / 100
565 ms36756 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second

struct FT{
    vector<int> bit;
    int n;
    FT(int ns){
        n = ns;
        bit.resize(n + 1);
    }
    int get(int v){
        int out = 0;
        while (v > 0){
            out = max(out, bit[v]);
            v = (v & (v + 1)) - 1;
        }
        return out;
    }
    void upd(int v, int x){
        while (v <= n){
            bit[v] = max(bit[v], x);
            v |= (v + 1);
        }
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, q; cin>>n>>q;
    vector<pii> all = {{}};
    for (int i = 1; i <= n; i++){
        int x, y; cin>>x>>y;
        all.pb({x, y});
    }
    
    auto cmp = [&](pii x, pii y){
        if (x.ff != y.ff) return x.ff < y.ff;
        return x.ss > y.ss;
    };
    
    sort(all.begin() + 1, all.end(), cmp);
    vector<int> a(n + 1), b(n + 1), f = {0};
    for (int i = 1; i <= n; i++){
        a[i] = all[i].ff; b[i] = all[i].ss;
        f.pb(b[i]);
    }
    
    vector<pii> qs[n + 1];
    vector<int> out(q + 1);
    vector<int> :: iterator it;
    for (int i = 1; i <= q; i++){
        int l, r; cin>>l>>r;
        it = lower_bound(a.begin() + 1, a.end(), l);
        if (it == a.end()) continue;
        int j = (int) (it - a.begin());
        qs[j].pb({r, i});
        f.pb(r);
    }
    
    sort(f.begin() + 1, f.end());
    map<int, int> mp;
    for (int i = 0; i < f.size(); i++) mp[f[i]] = i + 1;
    
    FT T((int) f.size());
    
    vector<int> dp(n + 1);
    int i = n;
    while (i > 0){
        int j = i;
        while (j > 0 && a[i] == a[j]){
            dp[j] = T.get(mp[b[j]]) + 1;
            T.upd(mp[b[j]], dp[j]);
            j--;
        }
        j++;
        
        for (auto [r, ii]: qs[j]){
            r = mp[r];
            out[ii] = T.get(r);
        }
        i = j - 1;
    }
    
    for (int i = 1; i <= q; i++){
        cout<<out[i]<<"\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...