258062026-03-03 17:19:55anonTiltott pár (45 pont)cpp17Accepted 45/45231ms2400 KiB
#include <bits/stdc++.h>
#define sz(x) ((ll) (x).size())
#define all(x) (x).begin(), (x).end()
#define FastIO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
using namespace std;
typedef long long ll;
int main() {
    FastIO;
    ll N;
    cin>>N;
    vector<array<ll,2>>A(N);
    for(ll i=0;i<N;i++){
        cin>>A[i][0];
        A[i][1]=i;
    }
    sort(all(A));
    ll Q;
    cin>>Q;
    while(Q--){
        ll X,Y;
        cin>>Y>>X;
        array<ll,2>q;
        q[0]=X;
        q[1]=0;
        ll xbeg=lower_bound(all(A),q)-A.begin();
        q[1]=N;
        ll xend=lower_bound(all(A),q)-A.begin();
        ll xlo=0;
        ll xhi=xend-xbeg;
        ll xc=xhi;
        if(!xc){
            cout<<"0\n";
            continue;
        }
        q[0]=Y;
        q[1]=0;
        ll ybeg=lower_bound(all(A),q)-A.begin();
        q[1]=N;
        ll yend=lower_bound(all(A),q)-A.begin();
        ll ylo=0;
        ll yhi=yend-ybeg;
        if(!yhi){
            cout<<"0\n";
            continue;
        }
        ll lo=0;
        ll hi=N;
        ll ans=-1;
        while(lo+1<hi){
            ll mid=(lo+hi)>>1;
            q[0]=X;
            q[1]=mid;
            ll c1=upper_bound(A.begin()+xbeg+xlo,A.begin()+xbeg+xhi,q)-A.begin()-xbeg;
            q[0]=Y;
            ll c2=upper_bound(A.begin()+ybeg+ylo,A.begin()+ybeg+yhi,q)-A.begin()-ybeg;
            if(c1+c2<=xc){
                lo=mid;
                xlo=c1;
                ylo=c2;
                ans=c2;
            }
            else{
                hi=mid;
                xhi=c1;
                yhi=c2;
            }
        }
        if(ans<0)
            ans=A[ybeg][1]<A[xbeg][1];
        cout<<ans<<'\n';
    }
    return 0;
}

SubtaskSumTestVerdictTimeMemory
base45/45
1Accepted0/01ms316 KiB
2Accepted0/02ms316 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms500 KiB
5Accepted2/21ms500 KiB
6Accepted2/22ms316 KiB
7Accepted2/22ms372 KiB
8Accepted2/23ms352 KiB
9Accepted3/32ms316 KiB
10Accepted2/2184ms2000 KiB
11Accepted2/2184ms2016 KiB
12Accepted2/2188ms2020 KiB
13Accepted2/2187ms2004 KiB
14Accepted3/3207ms2008 KiB
15Accepted3/3202ms2100 KiB
16Accepted2/2207ms2192 KiB
17Accepted2/2231ms2008 KiB
18Accepted2/2225ms2100 KiB
19Accepted2/2129ms1332 KiB
20Accepted2/2229ms2356 KiB
21Accepted2/2214ms2400 KiB
22Accepted2/2222ms2352 KiB
23Accepted2/2212ms2356 KiB