Function

The shorter, the simpler. With this problem, you should be convinced of this truth. 
   
  You are given an array AA of NN postive integers, and MM queries in the form (l,r)(l,r). A function F(l,r) (1≤l≤r≤N)F(l,r) (1≤l≤r≤N) is defined as: 
F(l,r)={AlF(l,r−1) modArl=r;l<r.F(l,r)={All=r;F(l,r−1) modArl<r. 
You job is to calculate F(l,r)F(l,r), for each query (l,r)(l,r).

InputThere are multiple test cases. 
   
  The first line of input contains a integer TT, indicating number of test cases, and TT test cases follow. 
   
  For each test case, the first line contains an integer N(1≤N≤100000)N(1≤N≤100000). 
  The second line contains NN space-separated positive integers: A1,…,AN (0≤Ai≤109)A1,…,AN (0≤Ai≤109). 
  The third line contains an integer MM denoting the number of queries. 
  The following MM lines each contain two integers l,r (1≤l≤r≤N)l,r (1≤l≤r≤N), representing a query.OutputFor each query(l,r)(l,r), output F(l,r)F(l,r) on one line.Sample Input

1
3
2 3 3
1
1 3

Sample Output

2

预处理出每个数下一个比他小(或等于)的数的位置。然后跳着取模即可。

数据是有多水。。n^2预处理都能过。。

#include <bits/stdc++.h>
#define MAXN 100005
using namespace std; int a[MAXN],nxt[MAXN]; int main(void)
{
int T,n,m,l,r,ans;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(int i = ; i <= n; i++) {
scanf("%d",&a[i]);
nxt[i]=n+;
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(a[i]>=a[j]){
nxt[i]=j;
break;
}
}
}
scanf("%d",&m);
while(m--) {
scanf("%d %d",&l,&r);
int ans=a[l];
for(int i=nxt[l];i<=r;i=nxt[i]){
if(i==n+) break;
ans%=a[i];
}
printf("%d\n",ans);
}
}
return ;
}

暴力预处理(非正解)

#include <bits/stdc++.h>
#define MAXN 100005
#define lson num << 1
#define rson num << 1 | 1
using namespace std;
struct node
{
int l,r;
int Min;
}tree[MAXN << ];
int a[MAXN],d1[MAXN];
int cur;
void pushup(int num)
{
tree[num].Min = min(tree[lson].Min,tree[rson].Min);
}
void build(int num,int l,int r)
{
tree[num].l = l;
tree[num].r = r;
if(l == r) {
tree[num].Min = a[l];
return;
}
int mid = (l + r) >> ;
build(lson,l,mid);
build(rson,mid + ,r);
pushup(num);
}
void query1(int num,int l,int r,int val)
{
if(tree[num].l == tree[num].r) {
if(tree[num].Min <= val) cur = min(cur,tree[num].l);
return;
}
int mid = (tree[num].l + tree[num].r) >> ;
if(tree[num].l == l && tree[num].r == r) {
if(tree[lson].Min <= val) query1(lson,l,mid,val);
else if(tree[rson].Min <= val) query1(rson,mid + ,r,val);
return;
}
if(r <= mid) query1(lson,l,r,val);
else if(l > mid) query1(rson,l,r,val);
else {
query1(lson,l,mid,val);
query1(rson,mid + ,r,val);
}
}
int main(void)
{
int T,n,m,l,r,Max,pos,val,ans;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
Max = ;
for(int i = ; i <= n; i++) {
scanf("%d",&a[i]);
d1[i] = ;
}
build(,,n);
d1[n] = n + ;
for(int i = ; i <= n - ; i++) {
cur = n + ;
query1(,i + ,n,a[i]);
d1[i] = cur;
}
/*for(int i = 1; i <= n; i++) {
printf("%d--\n",d1[i]);
}*/
scanf("%d",&m);
while(m--) {
scanf("%d %d",&l,&r);
int ans=a[l];
for(int i=d1[l];i<=r;i=d1[i]){
if(i==n+) break;
ans%=a[i];
}
printf("%d\n",ans);
}
}
return ;
}
/*
10
5
5 8 10 3 2 */

线段树预处理(正解)

HDU - 5875 Function(预处理)的更多相关文章

  1. HDU 5875 Function 优先队列+离线

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5875 Function Time Limit: 7000/3500 MS (Java/Others) ...

  2. HDU 5875 Function(RMQ-ST+二分)

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

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

  4. HDU 5875 Function(ST表+二分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...

  5. HDU 5875 Function

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

  6. HDU 5875 Function st + 二分

    Function Problem Description   The shorter, the simpler. With this problem, you should be convinced ...

  7. HDU 5875 Function 大连网络赛 线段树

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

  8. HDU 5875 Function 2016 ACM/ICPC Asia Regional Dalian Online

    N个数(N<=100000),M个询问,每次询问L,R,求F(L,R). F(L,R)=F(L,R-1)%A[R] , L<R 这道题数据比较鶸 可以直接用递减爆 正确做法应该是倍增 用倍 ...

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

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

随机推荐

  1. 设计模式系列一创建型模式之(简单工厂VS工厂方法)

    1.简单工厂简介 诞生背景:在我们平常编程当中,经常会使用new进行实例化一个对象,此时该类完全依赖于该对象,专业术语来说就是耦合度高.当需求发生变化时我们不得不去修改此类的源码,造成整个系统难以维护 ...

  2. input file 选择Excel文件 相关操作

    1.HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFo ...

  3. AWS:4.VPC

    主要介绍 1.Amazon混合云 2.将EC2加入VPC 3.VPC经典场景 4.VPC安全保障 Amazon混合云 : 在公有云的基础上创建私有云 VPC概念 VPC(VPC Virtual Pri ...

  4. ABAP-创建物料主数据

    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA' *&------------------------------------------------------- ...

  5. DLL中导出ANSI和UNICODE函数

    模仿window中的DLL导出ANSI和UNICODE版本的函数,使用UNICODE宏来控制使用哪个版本: 在函数实际的执行代码UNICODE版本中,在ANSI函数的版本中只做参数的转换,及ANSI字 ...

  6. 《程序员代码面试指南》第八章 数组和矩阵问题 打印N 个数组整体最大的Top K

    题目 打印N 个数组整体最大的Top K java代码 package com.lizhouwei.chapter8; /** * @Description: 打印N 个数组整体最大的Top K * ...

  7. 复制一个带random指针的链表

    一个单链表,其中除了next指针外,还有一个random指针,指向链表中的任意某个元素.如何复制这样一个链表呢? 通过next来复制一条链是很容易的,问题的难点在于如何恰当地设置新链表中的random ...

  8. jira与wiki官方文档记录

    jira:https://confluence.atlassian.com/display/JIRA/Home wiki:https://confluence.atlassian.com/doc/co ...

  9. 创建Django博客的数据库模型

    声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 blog最主要的功能就是展示我们写的文章 ...

  10. Windows内存性能分析(二)性能瓶颈

    内存瓶颈: 由于可用内存缺乏导致系统性能下降的现像. (一).相关的性能对象 主要考虑内存的页面操作和磁盘的I/O操作,需要考虑如下性能对象: Memory性能对象: 用于分析整个系统的内存瓶颈问题. ...