题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5875
思路:显然F(l , r) = Al % A(l+1) % ***Ar,大于当前数的直接跳过。
#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
const int N = 1e5 + 10;
int a[N] ,Next[N];
stack<int> s;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
while(!s.empty())
s.pop();
memset(Next,-1,sizeof(Next));
for(int i = 1 ;i <= n ;i++)
{
scanf("%d",a + i);
if(s.empty()||a[i]>=a[s.top()])
s.push(i);
else
{
while(!s.empty())
{
if(a[i] < a[s.top()])
{
Next[s.top()] = i;
s.pop();
}
else
break;
}
s.push(i);
}
}
int q;
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d %d",&l,&r);
int ans = a[l];
for(int i = Next[l] ;i <= r && i != -1 ;i = Next[i])
ans %= a[i];
printf("%d\n",ans);
}
}
return 0;
}