hdu 5875 单调栈+离线



链接:戳这里


Function

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

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).
 
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.
 
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
 


题意:

给出长度为n的正整数序列,q个询问[l,r]

f(l,r) = f(l,r-1)%a[r] l<r

f(l,r) = a[r] l=r

对于每个询问[l,r],输出f(l,r)的值


思路:

可以推出f(l,r)=a[l]%a[l+1]%...%a[r]

对于当前的l,在其右边只可能是比它小的数才会对答案造成影响,并且是递减的小(越来越小),注意这里不是比取模之后的答案还小

从后往前维护一个单调栈,依次更新n->1的递增序列,同时维护一个pre[]数组,表示上一个比它小的数出现的位置

对于询问[l,r] ,可以快速的拿a[l]%第一个比它小的数,接着%第二个第三个比它小的数....

离线处理询问[l,r],对于l相等的,如果r1>r2,求r1的答案完全可以从r2的答案继承过去接着往后%更小的数

而l1<l2的直接%过去一直%到当前r1的位置或者答案为0弹出(当然看到这里肯定可以构造数据让程序T


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int T;
int n,num,q;
int a[100100];
int s[100100],nex[100100];
struct node{
    int l,r,id;
    bool operator < (const node &a) const{
        if(l==a.l) return r<a.r;
        return l<a.l;
    }
}p[100100];
int anw[100100];
int main(){
    while(scanf("%d",&T)!=EOF){
        for(int cas=1;cas<=T;cas++){
            scanf("%d",&n);
            for(int i=1;i<=n;i++) scanf("%d",&a[i]);
            a[n+1]=0;
            num=1;
            s[num]=n+1;
            for(int i=n;i>=1;i--){
                while(a[i]<=a[s[num]]) num--;
                nex[i]=s[num];
                s[++num]=i;
            }
            scanf("%d",&q);
            for(int i=1;i<=q;i++){
                scanf("%d%d",&p[i].l,&p[i].r);
                p[i].id=i;
            }
            sort(p+1,p+q+1);
            p[0].l=0;p[0].r=0;p[0].id=0;
            for(int i=1;i<=q;i++){
                if(p[i].l==p[i-1].l){
                    int r=p[i-1].r;
                    int now=p[i].id;
                    anw[now]=anw[p[i-1].id];
                    while(nex[r]<=p[i].r && anw[now]){
                        anw[now]%=a[nex[r]];
                        r=nex[r];
                    }
                } else {
                    int l=p[i].l;
                    int now=p[i].id;
                    anw[now]=a[l];
                    while(nex[l]<=p[i].r && anw[now]){
                        anw[now]%=a[nex[l]];
                        l=nex[l];
                    }
                }
            }
            for(int i=1;i<=q;i++) printf("%d\n",anw[i]);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值