Function

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

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
分析:每次取模第一个比当前答案小于等于的数,RMQ+二分;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,q,a[][maxn],p[maxn];
void init()
{
for(int i=;i<=;i++)
for(int j=;(ll)j+(<<i)-<=n;j++)
a[i][j]=min(a[i-][j],a[i-][j+(<<(i-))]);
}
int get(int l,int r)
{
int x=p[r-l+];
return min(a[x][l],a[x][r-(<<x)+]);
}
int main()
{
int i,j;
for(i=;i<=maxn-;i++)p[i]=+p[i>>];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
rep(i,,n)scanf("%d",&a[][i]);
init();
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
int ans=a[][l],_r=r;
l++;
while()
{
int b=l,r=_r,pos=-;
while(l<=r)
{
int mid=l+r>>;
if(get(b,mid)<=ans)pos=mid,r=mid-;
else l=mid+;
}
if(pos==-)break;
else ans%=a[][pos],l=pos+;
}
printf("%d\n",ans);
}
}
//system("Pause");
return ;
}

2016大连网络赛 Function的更多相关文章

  1. 2016大连网络赛 Football Games

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

  2. 2016大连网络赛 Different GCD Subarray Query

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

  3. 2016大连网络赛 Sparse Graph

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

  4. 2016大连网络赛 Weak Pair

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

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

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

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

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

  7. 大连网络赛 1006 Football Games

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

  8. HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)

    题目链接  2016 青岛网络赛  Problem C 题意  给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...

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

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

随机推荐

  1. 搭建Linux3.4.2内核编辑环境

    (1)准备工作:1. 准备虚拟机2.安装工具链sudo tar xjf arm-linux-gcc-4.3.2.tar.bz2 -C /设置环境变量:sudo vi /etc/environment ...

  2. 查看当前目录每个文件的大小(linux)

    du -sh * 查看当前目录每个文件的大小

  3. oc知道经纬度求位置

    CLLocation *newLocation = [locations lastObject]; CLGeocodeCompletionHandler handler = ^(NSArray *pl ...

  4. 【转载】CentOS 6.4下PXE+Kickstart无人值守安装操作系统

    [转载]CentOS 6.4下PXE+Kickstart无人值守安装操作系统 转自:CentOS 6.4下PXE+Kickstart无人值守安装操作系统 - David_Tang - 博客园 http ...

  5. make module失败的原因cc1: error: unrecognized command line option “-m64

    cc1: error: unrecognized command line option "-m64"cc1: error: unrecognized command line o ...

  6. ubuntu 设置vpn

    百度了资料 http://jingyan.baidu.com/article/fa4125aca7f1b628ad709271.html 1. 设置 VPN CONNECTION 2.configur ...

  7. 重启库,提示找不到mysqld

    --ledir=/usr/local/mysql/bin    加上server的 directory https://dev.mysql.com/doc/refman/5.5/en/mysqld-s ...

  8. js中对style中的多个属性进行设值

    js中对style中的多个属性进行设值: 看一下案例自然就明白: document.getElementById("my_wz1").style.cssText="bac ...

  9. java设计模式案例详解:工厂模式

    1.简单工厂模式 在不考虑扩展的情况下还是很好用的,其实我们写代码也很经常用到,其主要理解在于传入不同参数则构建不同对象,只有一个工厂,如需添加产品涉及到扩展需要修改比较多的东西,不符合开闭原则,如下 ...

  10. php 图片压缩处理

    <?php require dirname(__FILE__).'/../includes/common.inc.php'; $_clean = array(); $_info = array( ...