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. OGG目的端的checkpoint table被drop的修复方法

    OGG目的端的checkpoint table被drop的修复方法 參考自:OGG Replicat Failed Due To Check_point Table beingTruncated (文 ...

  2. 每日英语:Vender Assault Shines Ugly Light on China's Urban Enforcers

    The sudden death of a street vendor in Hunan province on Wednesday provoked outrage chengguan, or ur ...

  3. Missing separate debuginfos, use: debuginfo-install

    环境:CentOS6.2 64位 操作:使用gdb调试C++查询MySQL数据库的程序 原因: 解决办法: 1.  修改文件/etc/yum.repos.d/CentOS-Debuginfo.repo ...

  4. cf 459c Pashmak and Buses

    E - Pashmak and Buses Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  5. 继承log4.net的类

    using System; using System.Diagnostics; [assembly: log4net.Config.XmlConfigurator(Watch = true)] nam ...

  6. [shell]简单的shell提示和参数脚本

    该shell脚本有如下点: bash or dash case语句的写法 脚本help写法 参数是否为空的写法 算数运算的写法 #! /bin/bash case "$1" in ...

  7. STM32F10x_硬件I2C主从通信(轮询发送,中断接收)

    Ⅰ.写在前面 关注我分享文章的朋友应该知道我在前面讲述过(软件.硬件)I2C主机控制从机EEPROM的例子.在I2C通信主机控制程序是比较常见的一种,可以说在实际项目中,很多应用都会使用到I2C通信. ...

  8. PHP——内测:联系人管理

    要求见文件-内测:联系人管理.pdf 数据库为mycontacts 表格为contacts,groups 表格内容为: zhuye.php <!DOCTYPE html PUBLIC " ...

  9. oozie开发注意事项

    ooziejob执行后 1. job.properties.coordinatior.xml中设置的值都是不可变的,除非将job kill掉,然后重新调度. oozie job -kill 00000 ...

  10. CI 结合Bootstrap 分页

    #显示列表 public function index(){ //载入分页类 $this->load->library('pagination'); //每页显示的条数 $perPage= ...