Function

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1701    Accepted Submission(s): 615

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) (1≤l≤r≤N) is defined as:
F(l,r)={AlF(l,r−1) 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(1≤N≤100000).
  The second line contains N space-separated positive integers: A1,…,AN (0≤Ai≤109).
  The third line contains an integer M denoting the number of queries. 
  The following M lines each contain two integers l,r (1≤l≤r≤N), representing a query.
 
Output
For each query(l,r), output F(l,r) on one line.
 
Sample Input
1
3
2 3 3
1
1 3
 
Sample Output
2
思路:用val[l],每次%上在[l+1,r]中下一个最小的值。
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = ;
struct Node{
int l, r, mn;
}a[MAXN*];
int n, m, val[MAXN];
void build(int rt, int l, int r)
{
a[rt].l = l;
a[rt].r = r;
if(l == r)
{
scanf("%d", &val[l]);
a[rt].mn = val[l];
return ;
}
int mid = (l + r) >> ;
build(rt << , l, mid);
build((rt << ) | , mid + , r);
a[rt].mn = min(a[rt<<].mn, a[(rt<<)|].mn);
}
void query(int rt, int l, int r, int &x)
{
if(x == ) return ;
if(a[rt].mn > x) return ;
if(a[rt].l == a[rt].r)
{
x %= a[rt].mn;
return ;
}
int mid = (a[rt].l + a[rt].r) >> ;
if(r <= mid)
{
query(rt << , l, r, x);
}
else if(mid < l)
{
query((rt << ) | , l, r, x);
}
else
{
query(rt << , l, mid, x);
query((rt << ) | , mid + , r, x);
}
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
build(, , n);
scanf("%d", &m);
while(m--)
{
int l, r;
scanf("%d %d", &l, &r);
if(l == r)
{
printf("%d\n", val[l]);
}
else
{
int x = val[l];
query(, l + , r, x);
printf("%d\n", x);
}
}
}
return ;
}
 

最新文章

  1. SqlServer SET IDENTITY_INSERT ON | OFF
  2. C#递归遍历子目录与子目录中的文件
  3. UVA 10780 Again Prime No Time.(数学)
  4. 笔记--mysql rpm 安装
  5. 【BZOJ-2434】阿狸的打字机 AC自动机 + Fail树 + DFS序 + 树状数组
  6. 剑指Offer:面试题13——在O(1)时间删除链表结点
  7. Windows Socket 最大连接数
  8. Linux(Centos)全自动异地备份数据(WEB+Mysql)
  9. 【Android】Android SDK在线更新镜像服务器
  10. Android动态加载so文件
  11. linux下python3连接mysql数据库
  12. 金融量化之Tushare模块
  13. Application、QueryString、session、cookie、ViewState、Server.Transfer等
  14. Django(十三)ajax 与 Bootstrap,font-awesome
  15. [JOISC2014]友だちをつくろう
  16. Hibernate SQL查询 addScalar()或addEntity()【转】
  17. Python+OpenCV图像处理(十二)—— 图像梯度
  18. 回声UDP服务器端/客户端
  19. 乌龙之Ignoring query to other database问题
  20. javascript中有关this的解析题

热门文章

  1. WebApplication和WebSite的简单区别
  2. Centos6安装MariaDB-yum方式
  3. hdu 5802 Windows 10 (dfs)
  4. SCM-MANAGER
  5. 【转】busybox分析——arp设置ARP缓存表中的mac地址
  6. vue.js 源代码学习笔记 ----- instance proxy
  7. [置顶] flume高并发优化——(14)解决空行停止收集数据问题,及offsets变小问题
  8. PHP常见带有下划线的常量
  9. linux vim 命令使用
  10. ubuntu下mysql安装提供外网访问