hdu-5875
题目大意:
f(l,r)=a[l] l==r
f(l,r)=f(l,r-1)%a[r] l<r
思路: 由此可以推出f(l,r)=a[l]%a[l+1]%a[l+2]%....%a[r]
根据取余的性质,只要后面取余的数不小于前面的数值不会改变,因此我们只要记录比a[l]小的第一个数,假如为a[x]然后接着找比a[x]小的第一个数
记为a[y] 如此一直找下去直到a[r]
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 100005 int n,q,a[MAX],nex[MAX];
//n 数组的个数,q次查询,nex[i]=j代表a[j]是第一个小于a[i]的数
int main()
{
freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(nex,-1,sizeof(nex));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
if(a[i]>a[j])
{
nex[i]=j;
break;
}
scanf("%d",&q);
int l,r;
while(q--)
{
scanf("%d%d",&l,&r);
int ans = a[l];
int temp = l;
while(nex[temp]<=r&&nex[temp]!=-1)
{
temp = nex[temp];
ans = ans%a[temp];
}
printf("%d\n",ans);
}
}
}
hdu-5875的更多相关文章
- HDU 5875 Function(RMQ-ST+二分)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 5875 Function(ST表+二分)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...
- HDU 5875 Function 优先队列+离线
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5875 Function Time Limit: 7000/3500 MS (Java/Others) ...
- HDU 5875 H - Function 用单调栈水过了
http://acm.hdu.edu.cn/showproblem.php?pid=5875 单调栈,预处理to[i]表示第一个比a[i]小的数字,一直跳就可以. 这题是数据水而已. 这里学习下单调栈 ...
- HDU 5875 Function -2016 ICPC 大连赛区网络赛
题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...
- HDU 5875 Function st + 二分
Function Problem Description The shorter, the simpler. With this problem, you should be convinced ...
- HDU 5875 Function (2016年大连网络赛 H 线段树+gcd)
很简单的一个题的,结果后台数据有误,自己又太傻卡了3个小时... 题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对 ...
- hdu 5875 ACM/ICPC Dalian Online 1008 Function
题目链接 分析:用RMQ预处理每段的最小值,然后对每次查询的区间找最靠近左边的小于的值,取模后递归操作.因为每次取模至少会使原来的值减半,所以递归操作是的.每次查询最小值如果通过线段树那么最终的复杂度 ...
- HDU 5875 Function
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
随机推荐
- spring boot2.03 spring cloud Finchley.RELEASE遇到的问题
1.spring cloud bus 本地不能更新 原因是@RefreshScope注解要加在需要更新的controller上 2.No instances found of configserver ...
- thrift相关资源
官网资料,具有各语言的例子 https://thrift.apache.org/tutorial/ https://thrift.apache.org/tutorial/py
- smrtlink
SMRT Link is the web-based end-to-end workflow manager for the Sequel™ System. It includes software ...
- 【JS】判断浏览器类型
判断原理 JavaScript是前端开发的主要语言,我们可以通过 编写JavaScript程序来判断浏览器的类型及版本.JavaScript判断浏览器类型一般有两种办法,一种是根据各种浏览器独有的属性 ...
- 【Mac】使用PicGIF制作gif动态图片
动态图片是我们常常需要的,mac系统下制作gif图片,可以使用PicGIF,AppStore中有一个简单版本免费的 环境与工具 1.mac系统 2.PicGIF Lite(可以在AppStore下载) ...
- 使用PrintWriter out=response.getWriter();输出script脚本时乱码解决
使用PrintWriter out=response.getWriter();输出script脚本时乱码解决 最近遇到了一个奇怪的事情,仅仅用out.print("<script ty ...
- Part 6 - Class-Based Views(21-26)
https://github.com/sibtc/django-beginners-guide/tree/v0.6-lw urlpatterns = [ views.PostUpdateView.as ...
- gj11 多线程、多进程和线程池编程
11.1 python中的GIL # coding=utf-8 # gil global interpreter lock (cpython) # python中一个线程对应于c语言中的一个线程 # ...
- day08(File类 ,字节流)
File类 构造方法 File(String path); FIle(String parent, String child); File(File parent, String child) ...
- SSH整合 第三篇 Spring的加入
1.思路和想法. 目前理解到的,觉得是的,可能的,应该这样的……………… Spring的两大核心是IoC和AOP Ioc:帮助实例化对象,加载到容器中,在注入到需要用到的地方.这样就可以减少在不同的方 ...