Submission #805965

#TimeUsernameProblemLanguageResultExecution timeMemory
805965mrwangMatryoshka (JOI16_matryoshka)C++14
100 / 100
287 ms24140 KiB
#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=3e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
ll st[4*maxN];
ll n;

void upd(ll p,ll v,ll id=1,ll l=1,ll r=n)
{
    if(l==r)
    {
        st[id]=v;
        return;
    }
    ll mid=l+r>>1;
    if(p<=mid) upd(p,v,id*2,l,mid);
    else upd(p,v,id*2+1,mid+1,r);
    st[id]=max(st[id*2],st[id*2+1]);
}
ll get(ll i,ll j,ll id=1,ll l=1,ll r=n)
{
    if(j<l||r<i) return 0;
    if(i<=l&&r<=j) return st[id];
    ll mid=l+r>>1;
    return max(get(i,j,id*2,l,mid),get(i,j,id*2+1,mid+1,r));
}
ll q;
struct qq
{
    ll h,r;
    bool operator<(const qq&o)
    {
        if(h==o.h) return r>o.r;
        return h<o.h;
    }
}a[maxN];
bool cmp(pli x,pli y)
{
    if(x.fi==y.fi) return x.se>y.se;
    return x.fi<y.fi;
}
struct qq2
{
    ll fi,se,id;
    bool operator<(const qq2&o)
    {
        return fi<o.fi;
    }
}t[maxN];
ll ans[maxN];
void solve()
{
    cin >> n >> q;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i].h >> a[i].r;
    }
    sort(a+1,a+n+1);
    // tim day con ko giam dai nhat ? ....
    vector<pli> vec;
    for(int i=1;i<=n;i++)
    {
        vec.pb({a[i].r,i});
    }
    sort(vec.begin(),vec.end(),cmp);
    for(int i=1;i<=q;i++)
    {
        cin >> t[i].se >> t[i].fi;
        t[i].id=i;
    }
    sort(t+1,t+q+1);
    ll j=0;
    for(int i=1;i<=q;i++)
    {
        while(j<n&&vec[j].fi<=t[i].fi)
        {
            ll p=vec[j].se;
            ll x=get(p+1,n)+1;
            upd(p,x);
            j++;
        }
        ll low=1,high=n;
        while(low<=high)
        {
            ll mid=low+high>>1;
            if(a[mid].h>=t[i].se) high=mid-1;
            else low=mid+1;
        }
        ans[t[i].id]=get(low,n);
    }
    for(int i=1;i<=q;i++) cout << ans[i]<<'\n';
}
int main()
{
    fastio
    //freopen(TASKNAME".INP","r",stdin);
    //freopen(TASKNAME".OUT","w",stdout);
    solve();
}

Compilation message (stderr)

matryoshka.cpp: In function 'void upd(ll, ll, ll, ll, ll)':
matryoshka.cpp:23:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   23 |     ll mid=l+r>>1;
      |            ~^~
matryoshka.cpp: In function 'll get(ll, ll, ll, ll, ll)':
matryoshka.cpp:32:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   32 |     ll mid=l+r>>1;
      |            ~^~
matryoshka.cpp: In function 'void solve()':
matryoshka.cpp:93:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   93 |             ll mid=low+high>>1;
      |                    ~~~^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...