Tall Building#12300

Tall Building (Editorial)

Problem Author:

De3b0o

Prepared by:

De3b0o

Edited by:

Hasan Saleh (MathModel)

Solution

If at the start two people share the same stating floor then the answer is immediately NO. Otherwise we can always make all people reach their destination starting from the person in the highest floor, unless there are two people where l_i \le l_j and r_j \ge r_i.

So it's enough to check if there's a pair where l_i \le l_j and r_j \ge r_i, we can do that by sorting the people based on l_i and checking if the the values r_i are sorted.

Complexity \mathcal{O}(n \cdot \log(n)).


#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define in insert
#define pb push_back
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cans cout << ans << "\n";
#define yes cout << "YES" << "\n";
#define no cout << "NO" << "\n";
#define pll pair<ll, ll>
#define lin cout << "\n";
#define mid ((l + r) / 2)
#define lc (2 * x)
#define rc (2 * x + 1)
#define double long double

using namespace std;

mt19937 rnd(time(0));

const ll N = 2e6+9;

ll n;
pll a[N];

int main()
{
    d3
    ll T = 1;
    cin >> T;
    while(T--)
    {
        cin >> n;
        for(int i = 1 ; n>=i ; i++)
            cin >> a[i].F;
        for(int i = 1 ; n>=i ; i++)
            cin >> a[i].S;
        sort(a+1,a+n+1);
        a[0]={-1e10,-1e10};
        bool g = 1;
        for(int i = 1 ; n>=i ; i++)
        {
            if(a[i].F==a[i-1].F||a[i].S<=a[i-1].S)
                g=0;
        }
        if(g)
            yes
        else
            no
    }
}