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) (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).
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 = 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 */
博文系博主原创,转载请注明出处 o(* ̄▽ ̄*)ブ 更多博文源自https://www.cnblogs.com/yzm10
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥