HDU 5875 Function 优先队列+离线
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5875
Function
Time Limit: 7000/3500 MS (Java/Others)Memory Limit: 262144/262144 K (Java/Others)
#### 问题描述
> 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 You job is to calculate F(l,r), for each query (l,r).
>
#### 输入
> 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.
>
#### 输出
> For each query(l,r), output F(l,r) on one line.
>
####样例输入
> 1
> 3
> 2 3 3
> 1
> 1 3
样例输出
2
题意
给你n个数,m个查询,对于每个查询(l,r),输出a[l]%a[l+1]%a[l+2]%...%a[r]。
题解
离线做,用优先队列维护下,吧所有的查询放在一起做,对于当前数a[i],只计算那些>a[i]的数%a[i],然后再放进优先队列,由于x%a[i]>x/2,所以每个数最多做logA[i]次,所以总共做了n*log(A[i])次,每次O(logn)的优先队列操作。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1e5+10;
int arr[maxn],ans[maxn];
struct Query{
int l,r,id;
Query(int l,int r,int id):l(l),r(r),id(id){}
bool operator < (const Query& tmp) const {
return l<tmp.l;
}
};
struct Node{
int id,v;
Node(int id,int v):id(id),v(v){}
bool operator < (const Node& tmp)const {
return v<tmp.v;
}
};
void init(){
clr(ans,-1);
}
int n,m;
int main() {
int tc,kase=0;
scanf("%d",&tc);
while(tc--){
scf("%d",&n);
init();
for(int i=1;i<=n;i++) scf("%d",&arr[i]);
scf("%d",&m);
vector<Query> que;
rep(i,0,m){
int l,r;
scf("%d%d",&l,&r);
que.pb(Query(l,r,i));
}
sort(all(que));
priority_queue<Node> pq;
int p=0;
for(int i=1;i<=n;i++){
while(!pq.empty()&&pq.top().v>=arr[i]){
Node nd=pq.top(); pq.pop();
if(que[nd.id].r<i) continue;
pq.push(Node(nd.id,nd.v%arr[i]));
ans[que[nd.id].id]=nd.v%arr[i];
}
while(p<que.sz()&&que[p].l==i){
pq.push(Node(p,arr[i]));
ans[que[p].id]=arr[i];
p++;
}
}
rep(i,0,m){
prf("%d\n",ans[i]);
}
}
return 0;
}
//end-----------------------------------------------------------------------
/*
3
3
2 3 3
1
2 2
*/
HDU 5875 Function 优先队列+离线的更多相关文章
- HDU 5875 Function(RMQ-ST+二分)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 5875 Function(ST表+二分)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...
- HDU 5875 Function st + 二分
Function Problem Description The shorter, the simpler. With this problem, you should be convinced ...
- HDU 5875 Function
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 5875 Function 大连网络赛 线段树
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- HDU - 5875 Function(预处理)
Function The shorter, the simpler. With this problem, you should be convinced of this truth. Yo ...
- HDU 5875 Function -2016 ICPC 大连赛区网络赛
题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...
- HDU 5875 Function (2016年大连网络赛 H 线段树+gcd)
很简单的一个题的,结果后台数据有误,自己又太傻卡了3个小时... 题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对 ...
随机推荐
- Web前端表单验证
表单选择器 :input(匹配所有input.textarea.select和button元素) :text(匹配所有单行文本框) :password(匹配所有密码框) :radio(匹配 ...
- less.js插件监听
<script>less.watch();</script> 在不手动刷新/重新加载页面会自动监听less的变化,页面做出相应的变化 . 写在这两行后面就好 了 <lin ...
- 4. HTML表单标签
表单是网页中最常见的元素,也是用户和我们交互的重要手段,在网站中的登录.注册.信息更新这些功能都是依赖表单实现的.在HTML中对于表单提供了一系列的标签,即输入框.下拉框.按钮.文本域,如下是一个最常 ...
- 大数据学习:Spark是什么,如何用Spark进行数据分析
给大家分享一下Spark是什么?如何用Spark进行数据分析,对大数据感兴趣的小伙伴就随着小编一起来了解一下吧. 大数据在线学习 什么是Apache Spark? Apache Spark是一 ...
- 一个sqoop export案例中踩到的坑
案例分析: 需要将hdfs上的数据导出到mysql里的一张表里. 虚拟机集群的为:centos1-centos5 问题1: 在centos1上将hdfs上的数据导出到centos1上的mysql里: ...
- pytorch之Tensor
#tensor和numpy import torch import numpy as np numpy_tensor = np.random.randn(3,4) print(numpy_tensor ...
- [SDOI2011]染色 树链剖分
LG传送门 我写这道题的题解主要是因为洛谷上的题解要么讲的不清要么代码丑滑稽,导致初学时的我调了很久,所以想发个题解方便后来人. 由于要维护的信息还是具有区间可加性,只需要记录一下每个区间的左右端点颜 ...
- pager-taglib分页注意事项
必须先导包,尤其是 jsp 这种工具类和标签库的
- mysql-5.7.17-winx64免安装版环境配置 问题小记
安装版问题请自行百度或google 这里总结几个免安装版mysql-5.7.17-winx64配置后,出现问题的解决方法. 具体的环境配置请先参考mysql-5.7.17-winx64免安装版,win ...
- DDD实战成绩管理---需求分析
需求的分析我们采用四色模型.从用户故事中找出MI,然后围绕MI找出其中的role,ppt,des.本次先对两个优先级最高的用户故事进行四色模型建模. 1.用户故事一建模:作为教务处老师,我要建立教学班 ...