Help me in solving RRSUM problem

My issue

Explain constraints of the problem.
How I know it is single loop problem or double loop?

My code

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
    long long N;
    cin>>N;
    int M;
    cin>>M;
    long long a[N]={0};
    long long b[N]={0};
    for(int i=0;i<N;i++)
    {
        a[i]=i+1;
        b[i]=i+N+1;
    }
    long long c[N*N];
    int k=0;
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            c[k++]=a[i]+b[j];
        }
    }
    sort(c,c+(N*N));
    long long q;
    map<int,int>mp;
    for(int i=0;i<N*N;i++)
    {
        mp[c[i]]++;
    }
    for(int i=0;i<M;i++)
    {
        cin>>q;
        if(mp[q])
        cout<<mp[q]<<endl;
        else
        cout<<0<<endl;
    }
}

Problem Link: Sum Queries Practice Coding Problem

@omdalvi1
constraints is upto 10^9 for n which is high for running loop so u have to do in either logn or constant time .
plzz refer my c++ code its a constant time solution .

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	long long int n,m;
	cin>>n>>m;
	while(m--)
	{
	    long long int q;
	    cin>>q;
	    long long int lo=n+2;
	    long long int hi=n+(2*n);
	    if(q<lo||q>hi)
	    cout<<0;
	    else
	    cout<<min(abs(q-lo)+1,abs(hi-q)+1);
	    cout<<endl;
	}
	return 0;
}