N个数(N<=100000),M个询问,每次询问L,R,求F(L,R)。

F(L,R)=F(L,R-1)%A[R] , L<R

这道题数据比较鶸 可以直接用递减爆 正确做法应该是倍增

用倍增的思想,对于位置i右侧的最长下降子序列,next[i][j]表示位置i右边第2j个的位置,预处理出每个位置的next数组

每次做的时候尽量远跳,直到找到next[j][0]恰好小于当前的值z,则z对A[next[j][0]]取模,移动左标记,继续查找,直到超出R或者没有更小的数

//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define N 100004
#define M 18
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int a[N],s[N];
int nextt[N][M];
void init()
{
int i,j,k,top;
s[top=]=n;
for(i=;i<M;i++)nextt[n][i]=n+;
for(i=n-;i;i--)
{
while(top && a[i]<a[s[top]])top--;
if(!top)nextt[i][]=n+;
else nextt[i][]=s[top];
s[++top]=i;
}
for(j=;j<M;j++)
{
for(i=;i<n;i++)
{
k=nextt[i][j-];
nextt[i][j]=nextt[k][j-];
}
}
}
int work(int l,int r)
{
int i,j,z=a[l];
if(l==r)return z;
while(l<r)
{
if(!z)return z;
for(i=;i<M-;i++)
{
if(a[nextt[l][]]<=z || nextt[l][i]>r)break;
if(a[nextt[l][i+]]<=z || nextt[l][i+]>r)
{
l=nextt[l][i];
i=-;
continue;
}
}
if(i==M || nextt[l][i]>r || !a[nextt[l][i]])return z;
z%=a[nextt[l][]];l=nextt[l][];
}
return z;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// init();
for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d",&n))
{
scanf("%d",&n);
for(i=;i<=n;i++)scanf("%d",a+i);
init();
scanf("%d",&m);
for(i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
printf("%d\n",work(x,y));
}
}
return ;
}
/*
// //
*/

HDU 5875 Function 2016 ACM/ICPC Asia Regional Dalian Online的更多相关文章

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

  2. HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  3. HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  4. HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  5. 2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  6. 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  7. hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

    Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  8. 2016 ACM/ICPC Asia Regional Dalian Online 1008 Function 二分+RMQ

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

  9. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

随机推荐

  1. bootstrap文件上传C#实现

    https://www.cnblogs.com/landeanfen/p/5007400.html

  2. 基于Bootstrap 3可预览的HTML5文件上传插件

    前端常用资源地址: http://www.htmleaf.com/ http://www.htmleaf.com/html5/html5muban/201505091801.html 源代码地址 ht ...

  3. tomcat启动不了?

    tomcat是用Java编写的,首先需要配置好jdk虚拟机, 要安装 JDK,请转至http://www.oracle.com/technetwork/java/javase/downloads/jd ...

  4. Django auth 用户验证

    ## Django本身内建这样的功能,admin管理页面就使用了这样的验证机制,admin管理页面就有Groups和Users选项 ## 需要在models.py中导入 from django.con ...

  5. 安卓渗透测试工具——Drozer(安装和使用)

    移动端渗透测试工具相比丰富的web端真的是少之又少,最近在做app的安全测试,用到了drozer,drozer的安装过程真的是太心酸了,中间报错了有6次才成功安装.. 一.环境准备 首先准备以下环境: ...

  6. eclipse 安装TestNg

    通过eclipse安装TestNg,过程如下: 1.点击help-->Install New Software 2.打开如下窗口,点击add,name自定义输入,Location中输入http: ...

  7. @Conditional注解

    根据条件动态创建bean public class TestConditon implements Condition { public boolean matches(ConditionContex ...

  8. Java 高级 --- 多线程快速入门

    这世上有三样东西是别人抢不走的:一是吃进胃里的食物,二是藏在心中的梦想,三是读进大脑的书 多线程快速入门 1.线程与进程区别 每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程.线程是一组 ...

  9. 显示 隐藏 加减css

    < script type = "text/javascript" > $(document).ready(function() { $("#hide1&qu ...

  10. [HAOI2016]字符合并

    Luogu3736 很容易想到直接DP,关键是枚举顺序. \(1.\)设后一段构成最后一个点,前一段构成前面的点,那么能得到\(1\)个点的数量要求 : \(1,k,2k-1...\)相差\(k-1\ ...