Exists or Not Exists

You will be given some numbers. Then there will contain some queries. In every query there will be given a range and a number. You have to say that, the given number exists in the range of values or not.


This is a companion discussion topic for the original entry at https://toph.co/p/exists-or-not-exists

Please help me with my code, I don’t understand what’s going wrong.

#include<iostream>

int main()
{
    int n,q,l,r,x;
    std::cin >> n >> q;
    int a[n];
    for(int i=0;i<n;i++){
        std::cin>> a[i];
    }
    for(int i=0;i<q;i++){
        std::cin >> l >> r >> x;
        bool c = false;
        for(int j=l-1;j<r;j++){
            if(x==a[j]){
                c = true;
                break;
            }
        }

        if(c){
            std::cout << "YES\n" ;
        }
        else{
            std::cout << "NO\n";
        }
    }

}