Language: C++

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

#define mp(x, y) make_pair((x), (y))

typedef long long ll;

int n;
char s[500005];
int dp[500005][27];
int next[500005][21];
int nn[500005][26];

int main()
{
	scanf("%s\n", s);
	n=strlen(s);
	for(int i=0; i<n; i++) s[i]-='a';
	for(int i=0; i<n; i++) for(int j=0; j<27; j++) dp[i][j]=-1;
	for(int i=0; i<n; i++) for(int j=0; j<26; j++) nn[i][j]=-1;
	for(int i=0; i<n; i++) for(int j=0; j<21; j++) next[i][j]=-1;
	for(int i=n-1; i>=0; i--) {
		dp[i][s[i]]=i;
		int cur=i+1;
		for(int j=s[i]+1; j<27 && cur<n; j++) {
			if(dp[cur][j-1]==-1) break;
			dp[i][j]=dp[cur][j-1];
			cur=dp[cur][j-1]+1;
		}
		if(dp[i][26]==-1 || dp[i][26]+1>=n) continue;
		for(int j=0; j<26; j++) {
			nn[i][j]=nn[dp[i][26]+1][j];
		}
		nn[i][s[dp[i][26]+1]]=dp[i][26];
		for(int j=0; j<s[i]; j++) {
			dp[i][j]=nn[i][j]+1;
		}
	}
	for(int i=0; i<n; i++) {
		next[i][0]=dp[i][26];
	}
	for(int j=1; j<21; j++) {
		for(int i=0; i<n; i++) {
			if(next[i][j-1]==-1 || next[i][j-1]+1>=n) continue;
			next[i][j]=next[next[i][j-1]+1][j-1];
		}
	}
	int Q;
	scanf("%d\n", &Q);
	for(int x=0; x<Q; x++) {
		int l, r;
		scanf("%d %d\n", &l, &r);
		if(l==r) {
			printf("No\n");
			continue;
		}
		l-=2; r--;
		for(int k=20; k>=0; k--) {
			if(l+1<n && next[l+1][k]!=-1 && next[l+1][k]<=r) {
				l=next[l+1][k];
			}
		}
		if(l!=r && next[l][0]!=r) {
			printf("No\n");
		} else {
			printf("Yes\n");
		}
	}

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:21:56: error: reference to 'next' is ambiguous
   21 |         for(int i=0; i<n; i++) for(int j=0; j<21; j++) next[i][j]=-1;
      |                                                        ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [500005][21]'
   11 | int next[500005][21];
      |     ^~~~
a.cc:40:17: error: reference to 'next' is ambiguous
   40 |                 next[i][0]=dp[i][26];
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [500005][21]'
   11 | int next[500005][21];
      |     ^~~~
a.cc:44:28: error: reference to 'next' is ambiguous
   44 |                         if(next[i][j-1]==-1 || next[i][j-1]+1>=n) continue;
      |                            ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [500005][21]'
   11 | int next[500005][21];
      |     ^~~~
a.cc:44:48: error: reference to 'next' is ambiguous
   44 |                         if(next[i][j-1]==-1 || next[i][j-1]+1>=n) continue;
      |                                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [500005][21]'
   11 | int next[500005][21];
      |     ^~~~
a.cc:45:25: error: reference to 'next' is ambiguous
   45 |                         next[i][j]=next[next[i][j-1]+1][j-1];
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [500005][21]'
   11 | int next[500005][21];
      |     ^~~~
a.cc:45:36: error: reference to 'next' is ambiguous
   45 |                         next[i][j]=next[next[i][j-1]+1][j-1];
      |                                    ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [500005][21]'
   11 | int next[500005][21];
      |     ^~~~
a.cc:45:41: error: reference to 'next' is ambiguous
   45 |                         next[i][j]=next[next[i][j-1]+1][j-1];
      |                                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [500005][21]'
   11 | int next[500005][21];
      |     ^~~~
a.cc:59:37: error: reference to 'next' is ambiguous
   59 |                         if(l+1<n && next[l+1][k]!=-1 && next[l+1][k]<=r) {
      |                                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [500005][21]'
   11 | int next[500005][21];
      |     ^~~~
a.cc:59:57: error: reference to 'next' is ambiguous
   59 |                         if(l+1<n && next[l+1][k]!=-1 && next[l+1][k]<=r) {
      |                                                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [500005][21]'
   11 | int next[500005][21];
      |     ^~~~
a.cc:60:35: error: reference to 'next' is ambiguous
   60 |                                 l=next[l+1][k];
      |                                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [500005][21]'
   11 | int next[500005][21];
      |     ^~~~
a.cc:63:28: error: reference to 'next' is ambiguous
   63 |                 if(l!=r && next[l][0]!=r) {
      |                            ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [500005][21]'
   11 | int next[500005][21];
      |     ^~~~