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 ",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-- ",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 ",ans); } } return 0; } /* 10 5 5 8 10 3 2 */