HDU5875-Function

本文介绍了一种区间取余查询问题的解决方法,通过维护单调递增序列和使用单调栈来提高查询效率。针对给定的整数数组,实现快速查询指定区间内元素依次取余的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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) (1lrN) is defined as:
F(l,r)={AlF(l,r1) 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(1N100000).
  The second line contains  N space-separated positive integers:  A1,,AN (0Ai109).
  The third line contains an integer  M denoting the number of queries. 
  The following  M lines each contain two integers  l,r (1lrN), 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值