Language: C++

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;

// Each sgg of the sgg tree would be a set
// to maintain distinct elements
set<int>* sgg;

// bdd the sgg tree
// i denotes current node, s denotes start and
// e denotes the end of range for current node
void bdd(int i, int s, int e, int arr[])
{

    // If start is equal to end then
    // insert the array element
    if (s == e) {
        sgg[i].insert(arr[s]);
        return;
    }

    // Else divide the range into two halves
    // (start to mid) and (mid+1 to end)
    // first half will be the left node
    // and the second half will be the right node
    bdd(2 * i, s, (s + e) / 2, arr);
    bdd(1 + 2 * i, 1 + (s + e) / 2, e, arr);

    // Insert the sets of right and left
    // node of the sgg tree
    sgg[i].insert(sgg[2 * i].begin(),
                  sgg[2 * i].end());

    sgg[i].insert(sgg[2 * i + 1].begin(),
                  sgg[2 * i + 1].end());
}

// MQQ in an range a to b
set<int> MQQ(int node, int l, int r, int a, int b)
{
    set<int> left, right, result;

    // If the range is out of the bounds
    // of this sgg
    if (b < l || a > r)
        return result;

    // If the range lies in this sgg
    if (a <= l && r <= b)
        return sgg[node];

    // Else MQQ for the right and left
    // leaf node of this subtree
    // and insert them into the set
    left = MQQ(2 * node, l, (l + r) / 2, a, b);
    result.insert(left.begin(), left.end());

    right = MQQ(1 + 2 * node, 1 + (l + r) / 2, r, a, b);
    result.insert(right.begin(), right.end());

    // Return the result
    return result;
}

// Initialize the sgg tree
void init(int n)
{
    // Get the height of the sgg tree
    int h = (int)ceil(log2(n));
    h = (2 * (pow(2, h))) - 1;

    // Initialize the sgg tree
    sgg = new set<int>[h];
}

// Function to get the result for the
// subarray from arr[l] to arr[r]
void GDD(int l, int r, int n)
{
    // MQQ for the range set
    set<int> ans = MQQ(1, 0, n - 1, l, r);

    cout << ans.size() << endl;
}

// Driver code
int main()
{
    ll j, k;

    cin >> n >> q;
    int arr[n];
    for (i = 0; i < n; i++)
    {
        cin >> arr[i];
    }

    init(n);

    bdd(1, 0, n - 1, arr);
    while (q--)
    {
        cin >> j >> k;
        j--;
        k--;
        GDD(j, k, n);
    }
]


    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:89:5: error: 'll' was not declared in this scope
   89 |     ll j, k;
      |     ^~
a.cc:91:12: error: 'n' was not declared in this scope; did you mean 'yn'?
   91 |     cin >> n >> q;
      |            ^
      |            yn
a.cc:91:17: error: 'q' was not declared in this scope
   91 |     cin >> n >> q;
      |                 ^
a.cc:93:10: error: 'i' was not declared in this scope
   93 |     for (i = 0; i < n; i++)
      |          ^
a.cc:95:16: error: 'arr' was not declared in this scope
   95 |         cin >> arr[i];
      |                ^~~
a.cc:100:22: error: 'arr' was not declared in this scope
  100 |     bdd(1, 0, n - 1, arr);
      |                      ^~~
a.cc:103:16: error: 'j' was not declared in this scope; did you mean 'jn'?
  103 |         cin >> j >> k;
      |                ^
      |                jn
a.cc:103:21: error: 'k' was not declared in this scope
  103 |         cin >> j >> k;
      |                     ^
a.cc:108:1: error: expected primary-expression before ']' token
  108 | ]
      | ^