链接:http://acm.hdu.edu.cn/showproblem.php?pid=5875
题意:给定数组a[],m 个询问L,R,求a[L],依次取模a[L+1]...a[R]后的值。
题解:找出每个数后面第一个比他小的数的位置,但在最坏的情况下还是O(mn)。。。
CODE:
#include <bits/stdc++.h>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define bug cout<<"bug"<<endl
const int MAXN = 200007;
const int MAXM = 20007;
const int MOD = 1e9 + 9;
using namespace std;
int a[MAXN],link[MAXN];
stack<int> s;
int main()
{
int T;
scanf("%d",&T);
int n,m;
while(T--)
{
scanf("%d",&n);
memset(link,0,sizeof(link));
while(!s.empty())s.pop();
for(int i=1; i<=n; ++i)
{
scanf("%d",&a[i]);
while(!s.empty()&&a[s.top()]>=a[i])
{
link[s.top()]=i;
s.pop();
}
s.push(i);
}
int l,r;
scanf("%d",&m);
for(int i=0; i<m; ++i)
{
scanf("%d%d",&l,&r);
int x=a[l];
while(link[l] && link[l]<=r)
{
l=link[l];
x%=a[l];
}
printf("%d\n",x);
}
}
return 0;
}
/*
1
3
2 3 3
1
1 3
*/