Function
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 851 Accepted Submission(s): 321
Problem Description
The shorter, the simpler. With this problem, you should be convinced of this truth.
You are given an array A of N postive integers, and M queries in the form (l,r). A function F(l,r) (1≤l≤r≤N) is defined as:
F(l,r)={AlF(l,r−1) modArl=r;l<r.
You job is to calculate F(l,r), for each query (l,r).
You are given an array A of N postive integers, and M queries in the form (l,r). A function F(l,r) (1≤l≤r≤N) is defined as:
F(l,r)={AlF(l,r−1) modArl=r;l<r.
You job is to calculate F(l,r), for each query (l,r).
Input
There are multiple test cases.
The first line of input contains a integer T, indicating number of test cases, and T test cases follow.
For each test case, the first line contains an integer N(1≤N≤100000).
The second line contains N space-separated positive integers: A1,…,AN (0≤Ai≤109).
The third line contains an integer M denoting the number of queries.
The following M lines each contain two integers l,r (1≤l≤r≤N), representing a query.
The first line of input contains a integer T, indicating number of test cases, and T test cases follow.
For each test case, the first line contains an integer N(1≤N≤100000).
The second line contains N space-separated positive integers: A1,…,AN (0≤Ai≤109).
The third line contains an integer M denoting the number of queries.
The following M lines each contain two integers l,r (1≤l≤r≤N), representing a query.
Output
For each query
(l,r), output
F(l,r) on one line.
Sample Input
1 3 2 3 3 1 1 3
Sample Output
2
Source
题意:给你n个数,给定区间[l,r],求a[l] % a[l + 1] % ... % a[r]
解题思路:对于%,只有比本身小到数才会影响最后结果,所以我们维护一个单调递增序列,和next数组,表示比当前数小到下一个数的位置,采用单调栈或者暴力都可以
暴力:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
int t,n,m,l,r,a[100009],next[100009];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
memset(next,-1,sizeof next);
for(int i=1; i<=n; i++)
{
for(int j=i+1; j<=n; j++)
{
if(a[j]<=a[i])
{
next[i]=j;
break;
}
}
}
scanf("%d",&m);
while(m--)
{
scanf("%d %d",&l,&r);
int x=a[l];
for(int i=next[l]; i<=r; i=next[i])
{
if(i==-1) break;
x%=a[i];
}
printf("%d\n",x);
}
}
return 0;
}
单调栈:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n, a[1000090], nt[1000009];
int main()
{
int t;
while (~scanf("%d", &t))
{
while (t--)
{
scanf("%d", &n);
memset(nt, -1, sizeof nt);
stack<int>s;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
while (!s.empty() && a[s.top()] > a[i]) nt[s.top()] = i, s.pop();
s.push(i);
}
int m;
scanf("%d", &m);
while (m--)
{
int l, r;
scanf("%d%d", &l, &r);
int ans = a[l], k = l;
while (nt[k] <= r&&nt[k] != -1)
{
ans %= a[nt[k]];
k = nt[k];
}
printf("%d\n", ans);
}
}
}
return 0;
}