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