思路:预处理出右边第一个比它小的数,然后询问的话就跳跳跳就可以了....当然可以随便构造数据让它T...
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000+7;
int pos[maxn];
int a[maxn],n,m;
void init()
{
memset(pos,-1,sizeof(pos));
for(int i = n-1;i>=1;i--)
{
int p = i+1;
for(;;)
{
if(a[i]>a[p])
{
pos[i]=p;
break;
}
if(pos[p]==-1)
{
pos[i]=-1;
break;
}
p = pos[p];
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = 1;i<=n;i++)
scanf("%d",&a[i]);
init();
scanf("%d",&m);
for(int i = 1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
int ans = a[x];
x = pos[x];
while(x<=y && x!=-1)
{
ans%=a[x];
x=pos[x];
}
printf("%d\n",ans);
}
}
}
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
&spm=1001.2101.3001.5002&articleId=52506245&d=1&t=3&u=687ab2eac16745119eb8104551d2483e)
430

被折叠的 条评论
为什么被折叠?



