Function

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

Total Submission(s): 1498    Accepted Submission(s): 553

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
函数的意思解读出来就是在l到r的区间里,a[l],对区间里的数,逐个取余
那么比a[l]大的数,取余不变,主要看比a[l]小的数,所以在区间里找第一个比a[l]小的数,
然后继续在剩下的区间里面找比取完余的a[l]小的数
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <stdlib.h>
#include <vector> using namespace std;
const int maxn=1e5;
int cmin[maxn*4+5];
int a[maxn+5];
int n,m;
int l,r;
int x;
void PushUp(int node)
{
cmin[node]=min(cmin[node<<1],cmin[node<<1|1]);
}
void build(int node,int begin,int end)
{
if(begin==end)
{
scanf("%d",&x);
cmin[node]=x;
a[begin]=x;
return;
}
int m=(begin+end)>>1;
build(node<<1,begin,m);
build(node<<1|1,m+1,end);
PushUp(node);
}
bool tag;
int minn;
int pos;
void query(int node,int begin,int end,int left,int right,int value)
{
if(value<cmin[node])
return; if(begin==end)
{
minn=cmin[node];
pos=begin;
tag=true;
return;
} int m=(begin+end)>>1;
if(left<=m)
query(node<<1,begin,m,left,right,value);
if(tag) return;
if(right>m)
query(node<<1|1,m+1,end,left,right,value);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
build(1,1,n);
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&l,&r);
tag=false;
int x=a[l];
if(l==r)
{
printf("%d\n",x);
continue;
}
query(1,1,n,l+1,r,x);
if(!tag)
printf("%d\n",x);
else
{
while(1)
{
tag=false;
x%=minn;
if(pos+1>r)
{
printf("%d\n",x);
break;
}
query(1,1,n,pos+1,r,x);
if(!tag)
{
printf("%d\n",x);
break;
}
}
} }
}
return 0;
}

 

HDU 5875 Function 大连网络赛 线段树的更多相关文章

  1. HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)

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

  2. HDU6447 YJJ's Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门  原题目描述在最下面.  1e5个点,问 ...

  3. HDU 5869 Different GCD Subarray Query(2016大连网络赛 B 树状数组+技巧)

    还是想不到,真的觉得难,思路太巧妙 题意:给你一串数和一些区间,对于每个区间求出区间内每段连续值的不同gcd个数(该区间任一点可做起点,此点及之后的点都可做终点) 首先我们可以知道每次添加一个值时gc ...

  4. 大连网络赛 1006 Football Games

    //大连网络赛 1006 // 吐槽:数据比较水.下面代码可以AC // 但是正解好像是:排序后,前i项的和大于等于i*(i-1) #include <bits/stdc++.h> usi ...

  5. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  6. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  8. HDU 5875 Function (2016年大连网络赛 H 线段树+gcd)

    很简单的一个题的,结果后台数据有误,自己又太傻卡了3个小时... 题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对 ...

  9. HDU 5875 Function -2016 ICPC 大连赛区网络赛

    题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...

随机推荐

  1. CentOS安装使用git

    yum install git yum install git-gui (可选) git config --global user.name "gg" git config --g ...

  2. 二叉排序树及其C代码

    1.二叉排序树的定义   二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree).其定义为:二叉排序树或者是空树, 或者是满足例如以下性质的二叉树 ...

  3. CBiontCache

    /************************************************************************/ /* 预先加载一些生物以备将来使用 */ /* 专 ...

  4. HTTP Header Accept-Language的ctf

    题目也不知道该怎么取,但是是实在的一个案例.分享给大家. 种族歧视分值: 300 小明同学今天访问了一个网站,竟然不允许中国人访问!太坑了,于是小明同学决心一定要进去一探究竟!  发现accept-L ...

  5. XmlFactoryBean和DefaultListableBeanFactory学习

    首先提供了一个Spring容器最简单的例子. bean的定义,MyTestBean.java public class MyTestBean { private String testStr = &q ...

  6. 1.重学javascript (一)

    一.script标签解析 <script>xxx</script>这组标签,是用于在html 页面中插入js的主要方法.它主要有以下 几个属性: 1.charset:可选.表示 ...

  7. XML 是一种元语言, 可以用它来描述其他语言。

    A.正确 B.错误 解答:B XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Markup L ...

  8. 模拟form表单请求上传文件

    发请求 public string CameraFileUpload(string url,string path,string serverPath,string uploadfileName) { ...

  9. Ubuntu下Eclipse无法添加Tomcat7解决方法

    Ubuntu(Linux)下在eclipse中add一个tomcatserver时发现tomcat7无法选择,但是该tomcat已经在eclipse 的server->runtime envir ...

  10. Mysql命令行添加用户并且给予远程访问服务器的权限

    --查询用户SELECT User, Password, Host FROM user; --创建一个用户,任意主机可以登录%,密码是123456 '; -- 给用户赋予所有权限 GRANT ALL ...