hdu-6406-dp+ST表
Taotao Picks Apples
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1446 Accepted Submission(s): 449
When Taotao picks apples, Taotao scans these apples from the first one to the last one. If the current apple is the first apple, or it is strictly higher than the previously picked one, then Taotao will pick this apple; otherwise, he will not pick.
Given the heights of these apples h1,h2,⋯,hn, you are required to answer some independent queries. Each query is two integers p,q, which asks the number of apples Taotao would pick, if the height of the p-th apple were q (instead of hp). Can you answer all these queries?
Each test case begins with a line of two integers n,m (1≤n,m≤105), denoting the number of apples and the number of queries. It is then followed by a single line of n integers h1,h2,⋯,hn (1≤hi≤109), denoting the heights of the apples. The next m lines give the queries. Each of these m lines contains two integers p (1≤p≤n) and q (1≤q≤109), as described in the problem statement.
5 3
1 2 3 4 4
1 5
5 5
2 3
#include <iostream>
#include<cmath>
using namespace std;
int N,a[];
int f[][],dp[][];
void init(){
for(int i=;i<=N;++i)f[i][]=i;
for(int k=;(<<k)<=N;++k){
for(int i=;i+(<<k)-<=N;++i){
if(a[f[i][k-]]>=a[f[i+(<<(k-))][k-]]) f[i][k]=f[i][k-];
else f[i][k]=f[i+(<<(k-))][k-];
}
}
}
int query(int L,int R){
if(R<L)return ;
int k=;
while((<<(k+))-<=R-L) k++;
if(a[f[L][k]]>=a[f[R-(<<k)+][k]]) return f[L][k];
else return f[R-(<<k)+][k];
}
int main() {
int t,n,m,i,p,q;
cin>>t;
while(t--){
scanf("%d%d",&n,&m);
for(i=;i<=n;++i)scanf("%d",a+i);
N=n,init();
dp[][]=;
int maxn=;
for(i=;i<=n;++i){
if(a[i]>a[maxn]){
dp[][i]=dp[][maxn]+;
maxn=i;
}
else dp[][i]=-;
} dp[][n]=;
for(i=n-;i>=;--i){
int l=i+,r=n;
while(l<r){
int mid=l+(r-l)/;
if(a[query(l,mid)]>a[i]) r=mid;
else l=mid+;
}
dp[][i]=a[r]>a[i]?dp[][l]+:;
}
while(m--){
scanf("%d%d",&p,&q);
int x=query(,p-),xx=a[x],ans=dp[][x];
if(q>a[x]) xx=q,ans++;
int l=p+,r=n;
while(l<r){
int mid=l+(r-l)/;
if(a[query(p+,mid)]>xx)r=mid;
else l=mid+;
}
if(a[l]>xx) ans+=dp[][l];
cout<<ans<<endl;
}
}
return ;
}
hdu-6406-dp+ST表的更多相关文章
- Hdu 5289-Assignment 贪心,ST表
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...
- Codeforces Round #278 (Div. 1) B - Strip dp+st表+单调队列
B - Strip 思路:简单dp,用st表+单调队列维护一下. #include<bits/stdc++.h> #define LL long long #define fi first ...
- 【Codeforces Round #466】E. Cashback DP+ST表
题意 给定$n$个数,将其划分成若干个连续的子序列,求最小价值,数组价值定义为,数组和减去$\lfloor \frac{k}{c} \rfloor$,$k$为数组长度,$c$为给定数 可以列得朴素方程 ...
- 刷题总结——Bob's Race(hdu4123 树形dp+st表)
题目: Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the ro ...
- (DP ST表 线段树)51NOD 1174 区间中最大的数
给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,最大的数是多少. 例如: 1 7 6 3 1.i = 1, j = 3,对应的数为7 6 3,最大的数为7. ...
- Find the hotel HDU - 3193 (ST表RMQ)
Summer again! Flynn is ready for another tour around. Since the tour would take three or more days, ...
- Interviewe HDU - 3486 (ST表+枚举 )(非二分,看下这个数据、)
YaoYao has a company and he wants to employ m people recently. Since his company is so famous, there ...
- HDU 2861 (DP+打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2861 题目大意:n个位置,m个人,分成k段,统计分法.S(n)=∑nk=0CknFibonacci(k ...
- HDU 4123 Bob’s Race 树的直径+ST表
Bob’s Race Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=41 ...
- HDU 5875 Function(ST表+二分)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...
随机推荐
- CIFAR-10与ImageNet图像识别
2.1.2 下载CIFAR-10 数据 python cifar10_download.py # Copyright 2015 The TensorFlow Authors. All Rights R ...
- EPPlus实战篇——Excel写入
.net core 项目 可以向excel写入任何类型(T)的数据,只要T中的field的[Display(Name = "1233", Description = "# ...
- VirtuablBox 出错: VERR_SUPLIB_OWNER_NOT_ROOT 解决方法
刚刚把 VirtualBox 升级, 从 3.2 到 4.0.4 后,虚拟机上的系统无法运行, 提示: VERR_SUPLIB_OWNER_NOT_ROOT 查了一下,发现是因为 /opt 的 own ...
- 5、lvs使用进阶(01)
四层.七层负载均衡的区别 https://jaminzhang.github.io/lb/L4-L7-Load-Balancer-Difference/ netfilter/iptables简介 ...
- POJ 1733 Parity game(种类并查集)
http://poj.org/problem?id=1733 题意: 给出一个01串,有多次询问,每次回答[l,r]这个区间内1的个数的奇偶性,但是其中有一些回答是错误的,问到第几个回答时与前面的回答 ...
- JavaScript中 call和apply
call()方法和apply()方法的作用相同,他们的区别在于接收参数的方式不同. 对于call(),第一个参数是this值没有变化,变化的是其余参数都直接传递给函数.(在使用call()方法时,传递 ...
- 【Web Service】
Restful: (Representational State Transfer 表现层[指客户端]状态[指服务器端]转化) RPC: RPC 风格的开发关注于服务器/客户端之间的方法调用, 而并 ...
- unity3d 加载优化建议 总结 from 侑虎科技
第一部分 我们对于纹理资源的加载建议如下: 1.严格控制RGBA32和ARGB32纹理的使用,在保证视觉效果的前提下,尽可能采用“够用就好”的原则,降低纹理资源的分辨率,以及使用硬件支持的纹理格式. ...
- 设计模式(一)Chain Of Responsibility责任链模式
设计模式篇章,源于网课的学习,以及个人的整理 在我们接收用户提交的字符时,常常会使用到过滤,在学习责任链模式前,我们是这样做的 1.定义一个类 public class MsgProcesser { ...
- 《剑指offer》第五十六题(数组中唯一只出现一次的数字)
// 面试题56(二):数组中唯一只出现一次的数字 // 题目:在一个数组中除了一个数字只出现一次之外,其他数字都出现了三次.请 // 找出那个吃出现一次的数字. #include <iostr ...