constructive algorithms data structures dp dsu greedy sortings *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ull unsigned long long
#define f(i,m,n) for(int i=m; i<n;i++)
#define pii pair<int,int>
#define ff first
#define ss second
#define len(vec) (vec).size()
#define vi vector<int>
#define yup cout<<"hii"<<"\n";
#define endl "\n";
#define show(x) for(auto m:x) cout<<m<<" ";
#define all(m) (m).begin(), (m).end()
#define pb push_back
#define mp make_pair

int inf = LONG_LONG_MAX;
int mod = 1000000007;

void buildTree(int idx, int low, int high, vector<int> &seg, vector<int> &a)
{
    if (low == high)
    {
        seg[idx] = a[low];
        return;
    }
    int mid = (low + high) / 2;
    buildTree(2 * idx + 1, low, mid, seg, a);
    buildTree(2 * idx + 2, mid + 1, high, seg, a);
    seg[idx] = max(seg[2 * idx + 1], seg[2 * idx + 2]);
}

int N;

int query(int idx, int low, int high, int l, int r, vector<int> &seg)
{
    if (low>=l && high <= r)
    {
        return seg[idx];
    }
    if (l > high || r < low)
    {
        return INT_MIN;
    }
    int mid = (low + high) / 2;
    int left = query(2 * idx + 1, low, mid, l, r, seg);
    int right = query(2 * idx + 2, mid + 1, high, l, r, seg);
    return max(left, right);
}

bool solve(map<int,int> &m, map<int,vi> &mp,int i, int j, vi &seg, vi &a, vi &b)
{
    if(i>j) return true;
    if(i==j){
        if(b[i]>a[i]) return false;
        if(a[i]==b[i])  return true;
        if(m[b[i]]>0){
            m[b[i]]--;
            return true;
        }else{
            return false;
        }
    }
    int maxi=query(0,0,N-1,i,j,seg);

    bool temp = false;
    for(auto it:mp[maxi]){
        if(a[it]<b[it]){
        
        return false;}
        if(a[it]!=b[it]){
            temp = true;
        }
    }
    if(temp){
        if(m[maxi]<=0) {return false;}
        m[maxi]--;
    }
    bool ans = true;
    int prev=i-1;
    for(auto I:mp[maxi])
    {
        if(I>=i && I<=j)
        {
            ans=ans&solve(m,mp,prev+1,I-1,seg,a,b);
            prev=I;
        }
    }
    if(prev!=j)
    ans=ans&solve(m,mp,prev+1,j,seg,a,b);
    return ans;
}

void tathastu()
{
    int n;
    cin>>n;
    N= n;
    vi a(n), b(n);
    f(i,0,n)    cin>>a[i];
    f(i,0,n)    cin>>b[i];
    int m;
    cin>>m;
    vi x(m);
    f(i,0,m)    cin>>x[i];
    sort(all(x));

    vi seg(4*n);
    buildTree(0,0,n-1,seg,b);
    map<int,int> M;
    f(i,0,m)    M[x[i]]++;
    
    map<int,vector<int>> mp;
    f(i,0,n)    mp[b[i]].pb(i);

    if(solve(M,mp,0,N-1,seg,a,b))
    cout<<"YES\n";
    else
    cout<<"NO\n";
}

signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int testcases;
    cin>>testcases;
    for(int __=1; __<=testcases; __++)
    {
        //cout<<"Case #"<<__<<": ";
        tathastu();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String