hdu5875 Function (离线处理)

来源:互联网 发布:sketch 激活码 mac 编辑:程序博客网 时间:2024/06/05 20:14



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) (1lrN) is defined as:
F(l,r)={AlF(l,r1) 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(1N100000).
  The second line contains N space-separated positive integers: A1,,AN (0Ai109).
  The third line contains an integer M denoting the number of queries. 
  The following M lines each contain two integers l,r (1lrN), representing a query.
 

Output
For each query(l,r), output F(l,r) on one line.
 

Sample Input
132 3 311 3
 

Sample Output
2
 

#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include <vector>#include <queue>using namespace std;#define LL long long#define INF 0x3f3f3f3f3f3f3f3fint T;int n,m;int a[120000];int net[120000];int pos[120000];int ans[120000];int cnt=0;struct Node{    int l,r,id;} node[120000];bool cmp(Node a,Node b){    if(a.l!=b.l)        return a.l<b.l;    return a.r<b.r;}int main(){    while(scanf("%d",&T)!=EOF)    {        while(T--)        {            scanf("%d",&n);            for(int i=1; i<=n; i++)                scanf("%d",&a[i]);            cnt=0;            a[n+1]=0;            pos[cnt]=n+1;            for(int i=n; i>=1; i--)            {                while(a[i]<=a[pos[cnt]]) cnt--;                net[i] = pos[cnt];                pos[++cnt] = i;            }            scanf("%d",&n);            for(int i=1; i<=n; i++)            {                scanf("%d%d",&node[i].l,&node[i].r);                node[i].id=i;            }            sort(node+1,node+n+1,cmp);            node[0].l=node[0].r=node[0].id=0;            int pre=0;            for(int i=1; i<=n; i++)            {                if(node[pre].l==node[i].l)                {                    int re = ans[node[pre].id];                    int pp = net[node[pre].r];                    while(pp <= node[i].r&&re)                    {                        re %=a[pp];                        pp = net[pp];                    }                    ans[node[i].id]=re;                    pre=i;                }                else                {                    int re = a[node[i].l];                    int pp = net[node[i].l];                    while(pp <= node[i].r &&re)                    {                        re %= a[pp];                        pp=net[pp];                    }                    ans[node[i].id]=re;                    pre = i;                }            }            for(int i=1; i<=n; i++)                printf("%d\n",ans[i]);        }    }    return 0;}




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) (1lrN) is defined as:
F(l,r)={AlF(l,r1) 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(1N100000).
  The second line contains N space-separated positive integers: A1,,AN (0Ai109).
  The third line contains an integer M denoting the number of queries. 
  The following M lines each contain two integers l,r (1lrN), representing a query.
 

Output
For each query(l,r), output F(l,r) on one line.
 

Sample Input
132 3 311 3
 

Sample Output
2
 

0 0