#include <bits/stdc++.h>
#define int long long
using namespace std;
set<pair<pair<int,int>,pair<int,int>>> dp, sel;
int s, q;
bool solve(int C, int F, int c, int f)
{
    if(C <= 0)
    {
        return false;
    }
    if(C <= s * f && c <= s * F)
    {
        int D = f - (C - 1) / s;
        int d = F - (c - 1) / s;
        if(D <= d)
        {
            return solve(C, F - D, c, f - D);
        }
        else
        {
            return !solve(c, f - d - 1, C, F - d);
        }
    }
    if(C - s * f >= s && c - s * F <= s)
    {
        return true;
    }
    if(C - s * f >= 0 && c - s * F <= 0)
    {
        if(f > 0)
        {
            return true;
        }
        if(C - s * f >= s)
        {
            return true;
        }
        int cnt = (c - 1) / C + 1;
        int d = F - (c - 1) / s;
        if(cnt <= d + 1)
        {
            return true;
        }
        return solve(C, F - d, c - d * C, f);
    }
    auto it = sel.lower_bound({{C, F}, {c,f}});
    if(it != sel.end())
    {
        if(it->first.first == C && it->first.second == F && it->second.first == c && it->second.second == f)
        {
            return (dp.find({{C, F}, {c,f}}) != dp.end());
        }
        if(it->first.first == C && it->first.second == F && it->second.first == c && dp.find(*it) != dp.end())
        {
            return true;
        }
    }
    if(it != sel.begin())
    {
        --it;
        if(it->first.first == C && it->first.second == F && it->second.first == c && dp.find(*it) == dp.end())
        {
            return false;
        }
    }
    if((s * (1 + F) - c) * f >= (s * (1 + f) - C) && f > 0 && C > f * s && C < (f + 1) * s && c > F * s && c < (F + 1) * s)
    {
        return true;
    }
    sel.insert({{C, F}, {c,f}});
    bool ok = false;
    if(f > 0 && C > f * s && C < (f + 1) * s && c > F * s && c < (F + 1) * s)
    {
        if((s * f - C) * F >= s)
        {
            ok = !solve(c, f - 1, C, F);
        }
        else
        {
            if(f > 0 && !solve(c, f - 1, C, F))
            {
                ok = true;
            }
            else if(C - f * s > 0 && !solve(c - (C - f * s), f, C, F))
            {
                ok = true;
            }
        }
    }
    else
    {
        if(C - f * s > 0)
        {
            ok = !solve(c - (C - f * s), f, C, F);
        }
        else if(f > 0)
        {
            ok = !solve(c, f - 1, C, F);
        }
    }
    /*if(f > 0 && ok != !solve(c, f - 1, C, F) && C > f * s && C < (f + 1) * s)
    {
        cerr<<C<<' '<<F<<' '<<c<<' '<<f<<'\n';
    }*/
    if(ok)
    {
        dp.insert({{C, F}, {c,f}});
    }
    return ok;
}
bool solve1(int C, int F, int c, int f)
{
    if(C <= 0)
    {
        return false;
    }
    if(C <= f && c <= F)
    {
        if(f - C <= F - c)
        {
            return true;
        }
        return false;
    }
    if(C <= f)
    {
        return false;
    }
    return (!solve1(c - (C - f), f, C, F));
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifdef home
    freopen("nr.in","r",stdin);
    freopen("nr.out","w",stdout);
#endif // home
    cin>>s>>q;
    for(int i=1; i<=q; i++)
    {
        int ch, fh, cg, fg;
        cin>>ch>>fh>>cg>>fg;
        bool ok;
        if(s == 1)
        {
            ok = solve1(ch, fh, cg, fg);
        }
        else
        {
            ok = solve(ch, fh, cg, fg);
        }
        if(ok)
        {
            cout<<"YES\n";
        }
        else
        {
            cout<<"NO\n";
        }
    }
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |