标签:
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1866 Accepted Submission(s): 674
///pro do this : a[l]%a[l+1]%...%a[r] #include <stdio.h> #include <iostream> #include <algorithm> #include <queue> #include <string.h> #include <vector> using namespace std; typedef long long LL; const LL INF = 1e10; const int N = 100005; LL a[N],R[N]; int main(){ int tcase,n; scanf("%d",&tcase); while(tcase--){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); } memset(R,-1,sizeof(R)); for(int i=n-1;i>=1;i--){ ///单调栈维护其右边小于 a[i] 的第一个数 int t =i+1; while(true){ if(a[i]>=a[t]){ R[i] = t; break; } if(R[t]==-1){ break; } t = R[t]; } R[i] = t; } int m; scanf("%d",&m); while(m--){ int l,r; scanf("%d%d",&l,&r); LL ans = a[l]; int nxt = l; while(R[nxt]<=r&&R[nxt]!=-1){ nxt = R[nxt]; ans = ans%a[nxt]; } printf("%lld\n",ans); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5877542.html