Submission #1015399

#TimeUsernameProblemLanguageResultExecution timeMemory
1015399CookieMatryoshka (JOI16_matryoshka)C++14
100 / 100
176 ms17872 KiB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const int dx[8] = {0, 0, 1, -1, 1, -1, 1, -1};
const int dy[8] = {1, -1, 0, 0, 1, -1, -1, 1};
const int mxn = 2e5 + 5;
const ll inf = 2e9 + 1;

struct Q{
    int a, b, id;
    bool operator <(const Q &other){
        return(a > other.a);
    }
};
int n, q;
pll doll[mxn + 1];
int bit[mxn + 1], l[mxn + 1], ans[mxn + 1];
vt<int>comp;
void upd(int p, int v){
    while(p <= sz(comp)){
        bit[p] = max(bit[p], v); p += p & (-p);
    }
}
int get(int p){
    int ans = 0;
    while(p){
        ans = max(ans, bit[p]); p -= p & (-p);
    }
    return(ans);
}

bool cmp(pll a, pll b){
    if(a.fi != b.fi)return(a.fi > b.fi);
    return(a.se < b.se);
}
int find(int x){
    return(lower_bound(ALL(comp), x) - comp.begin() + 1);
}
Q queries[mxn + 1];
void solve(){
    cin >> n >> q;
    for(int i = 1; i <= n; i++)l[i] = inf;
    for(int i = 1; i <= n; i++){
        cin >> doll[i].fi >> doll[i].se;
    }

    sort(doll + 1, doll + n + 1, cmp);
    for(int i = 0; i < q; i++){
        cin >> queries[i].a >> queries[i].b; queries[i].id = i; comp.pb(queries[i].b);
    }
    sort(ALL(comp));
    sort(queries, queries + q);
    int r = 1;
    for(int i = 0; i < q; i++){
        while(r <= n && doll[r].fi >= queries[i].a){
            int id = upper_bound(l + 1, l + n + 1, doll[r].se) - l;
            upd(find(doll[r].se), id);
            l[id] = doll[r].se;
            r++;
        }
        ans[queries[i].id]= get(find(queries[i].b));
    }
    for(int i =0 ; i < q; i++)cout << ans[i] << "\n";
}
signed main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tt; tt = 1;
    while(tt--)solve();
    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...