C - Function

The shorter, the simpler. With this problem, you should be convinced of this truth. 
   
  You are given an array AA of NN postive integers, and MM queries in the form (l,r)(l,r). A function F(l,r) (1≤l≤r≤N)F(l,r) (1≤l≤r≤N) is defined as: 
F(l,r)={AlF(l,r−1) modArl=r;l<r.F(l,r)={All=r;F(l,r−1) modArl<r. 
You job is to calculate F(l,r)F(l,r), for each query (l,r)(l,r).

InputThere are multiple test cases. 
   
  The first line of input contains a integer TT, indicating number of test cases, and TT test cases follow. 
   
  For each test case, the first line contains an integer N(1≤N≤100000)N(1≤N≤100000). 
  The second line contains NN space-separated positive integers: A1,…,AN (0≤Ai≤109)A1,…,AN (0≤Ai≤109). 
  The third line contains an integer MM denoting the number of queries. 
  The following MM lines each contain two integers l,r (1≤l≤r≤N)l,r (1≤l≤r≤N), representing a query.OutputFor each query(l,r)(l,r), output F(l,r)F(l,r) on one line.Sample Input

1
3
2 3 3
1
1 3

Sample Output

2

开一个nest数组,next[i]存的是i之后的第一个da[i]

#include <bits/stdc++.h>

using namespace std;

int n;
int th[100010];
int nest[100010];
int main()
{
    int num;
    scanf("%d", &num);
    while(num --){
        scanf("%d", &n);
        for(int i = 1; i <= n; i ++){
            scanf("%d", &th[i]);
        }
        for(int i = 1; i <= n; i ++){
            for(int j = i + 1; j <= n; j ++){
                nest[i] = n + 1;
                if(th[i] >= th[j]){
                    nest[i] = j;
                    break;
                }
            }
        }
        //nest[n] = n + 1;
        int m;
        scanf("%d", &m);
        for(int i = 0; i < m; i ++){
            int l, r;
            scanf("%d %d", &l, &r);
            int ans = th[l];
            if(l == r) cout << ans << endl;
            else {
                    for(int j = nest[l]; j <= r; j = nest[j]){
                    if(th[j] == 0) break;
                    ans %= th[j];
                    if(ans == 0) break;
                }
                cout << ans << endl;
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值