HDU - 5875 Function(预处理)

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) (1lrN)F(l,r) (1≤l≤r≤N) is defined as: 
F(l,r)={AlF(l,r1) 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(1N100000)N(1≤N≤100000). 
  The second line contains NN space-separated positive integers: A1,,AN (0Ai109)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 (1lrN)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 = 1; i <= n; i++) {
            scanf("%d",&a[i]);
            nxt[i]=n+1;
        }
        for(int i=1;i<=n;i++){
            for(int j=i+1;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+1) break;
                ans%=a[i];
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}
暴力预处理(非正解)
复制代码
复制代码
#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 << 2];
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) >> 1;
    build(lson,l,mid);
    build(rson,mid + 1,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) >> 1;
    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 + 1,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 + 1,r,val);
    }
}
int main(void)
{
    int T,n,m,l,r,Max,pos,val,ans;
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        Max = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d",&a[i]);
            d1[i] = 0;
        }
        build(1,1,n);
        d1[n] = n + 1;
        for(int i = 1; i <= n - 1; i++) {
            cur = n + 1;
            query1(1,i + 1,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+1) break;
                ans%=a[i];
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}
/*
10
5
5 8 10 3 2

*/
线段树预处理(正解)
复制代码

 

posted @   yzm10  阅读(147)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
点击右上角即可分享
微信分享提示