HDU 5875 Function 大连网络赛 线段树
Function
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1498 Accepted Submission(s): 553
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).
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 F(l,r) on
one line.
1
3
2 3 3
1
1 3
2函数的意思解读出来就是在l到r的区间里,a[l],对区间里的数,逐个取余那么比a[l]大的数,取余不变,主要看比a[l]小的数,所以在区间里找第一个比a[l]小的数,然后继续在剩下的区间里面找比取完余的a[l]小的数#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <stdlib.h>
#include <vector> using namespace std;
const int maxn=1e5;
int cmin[maxn*4+5];
int a[maxn+5];
int n,m;
int l,r;
int x;
void PushUp(int node)
{
cmin[node]=min(cmin[node<<1],cmin[node<<1|1]);
}
void build(int node,int begin,int end)
{
if(begin==end)
{
scanf("%d",&x);
cmin[node]=x;
a[begin]=x;
return;
}
int m=(begin+end)>>1;
build(node<<1,begin,m);
build(node<<1|1,m+1,end);
PushUp(node);
}
bool tag;
int minn;
int pos;
void query(int node,int begin,int end,int left,int right,int value)
{
if(value<cmin[node])
return; if(begin==end)
{
minn=cmin[node];
pos=begin;
tag=true;
return;
} int m=(begin+end)>>1;
if(left<=m)
query(node<<1,begin,m,left,right,value);
if(tag) return;
if(right>m)
query(node<<1|1,m+1,end,left,right,value);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
build(1,1,n);
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&l,&r);
tag=false;
int x=a[l];
if(l==r)
{
printf("%d\n",x);
continue;
}
query(1,1,n,l+1,r,x);
if(!tag)
printf("%d\n",x);
else
{
while(1)
{
tag=false;
x%=minn;
if(pos+1>r)
{
printf("%d\n",x);
break;
}
query(1,1,n,pos+1,r,x);
if(!tag)
{
printf("%d\n",x);
break;
}
}
} }
}
return 0;
}
HDU 5875 Function 大连网络赛 线段树的更多相关文章
- HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Tota ...
- HDU6447 YJJ's Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. 1e5个点,问 ...
- HDU 5869 Different GCD Subarray Query(2016大连网络赛 B 树状数组+技巧)
还是想不到,真的觉得难,思路太巧妙 题意:给你一串数和一些区间,对于每个区间求出区间内每段连续值的不同gcd个数(该区间任一点可做起点,此点及之后的点都可做终点) 首先我们可以知道每次添加一个值时gc ...
- 大连网络赛 1006 Football Games
//大连网络赛 1006 // 吐槽:数据比较水.下面代码可以AC // 但是正解好像是:排序后,前i项的和大于等于i*(i-1) #include <bits/stdc++.h> usi ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模
Multiply game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
- HDU 5875 Function (2016年大连网络赛 H 线段树+gcd)
很简单的一个题的,结果后台数据有误,自己又太傻卡了3个小时... 题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对 ...
- HDU 5875 Function -2016 ICPC 大连赛区网络赛
题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...
随机推荐
- 【iOS开发之Objective-C】书签管理器项目
1.项目 新建一个书签管理器的项目,能够存储书签的网址.中文名.星级.訪问量和权限信息.具有增.删.改.查和排序的功能. 2.找对象,抽象类 书签管理器,书签管理器.书签管理器-- 多读几次书是不是 ...
- Error-Project facet Java version 1.8 is not supported
最近导入最新的Strtus2.5.10.1 Demo时出现了这个错误 解决方案如下: 选中工程——右键——Properties 然后依次展开找到如图所示内容,将1.8改成1.7即可. 原因:工程默认配 ...
- Atitit.404错误解决标准流程and url汉字中文路径404错误resin4 resin chinese char path 404 err解决
Atitit.404错误解决标准流程and 错误resin4 resin chinese char path 404 err解决 1. #原因解析 1 2. #解决方式 2 3. 输出图片流... 2 ...
- Taking A Fresh Look At What Open Source API Management Architecture Is Available
http://apievangelist.com/2014/10/05/taking-a-fresh-look-at-what-open-source-api-management-architect ...
- 每日英语:Online Education a New Frontier in China
In a country as obsessed with education as China, it makes sense that online teaching has huge poten ...
- Python 实现抽象类的两种方式+邮件提醒+动态导入模块+反射(参考Django中间件源码)
实现抽象类的两种方式 方式一 from abc import ABCMeta from abc import abstractmethod class BaseMessage(metaclass=AB ...
- 工作流Activiti的学习总结(十二) activiti官方十分钟快速学习 (zhuan)
http://topmanopensource.iteye.com/blog/1315341 ***************************************************** ...
- 利用Python操作Word文档【图片】
利用Python操作Word文档
- Windows Phone 提升开发效率(一)使用d:DataContext添加设计时Binding
[问题的提出] 在开发过程中我们经常会遇到将UI同学提供的效果图转化成实际的页面,而在这过程中,多数时候Blend等设计工具默认情况下并不能提供很好的可视化支持. 举个简单的例子来说下吧: ...
- Eclipse报Caused by: java.lang.OutOfMemoryError: PermGen space解决思路
一.修改tomcat/bin目录下的catalina.bat 在“rem ----- Execute The Requested Command ----------------------”下加入 ...