HDU - 6406 Taotao Picks Apples (RMQ+dp+二分)
题意:N个高度为hi的果子,摘果子的个数是从位置1开始从左到右的严格递增子序列的个数。有M次操作,每次操作对初始序列修改位置p的果子高度为q。每次操作后输出修改后能摘到得数目。
分析:将序列分为左、右两部分,每次修改之后的结果是p左部到p递增的子序列长度,加上右部第一个高度大于max(q,p位置之前最大的高度)位置开始的递增子序列长度。
左部的查询可以线性递推预处理得到,右部的查询需借助ST表预处理出区间最大值,并二分求得位置。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = ;
int N, m, tot;
int h[];
int st[MAXN][-__builtin_clz(MAXN)];
int dp[];
int sel[], pre[], c[]; void init_st() {
int l = - __builtin_clz(N);
for(int i=;i<N;++i) st[i][] = h[i];
for(int j=;j<l;++j)
for (int i=;i<+N-(<<j);++i)
st[i][j+] = max(st[i][j], st[i+(<<j)][j]);
} int rmq(int l, int r) {
int k = - __builtin_clz(r - l + );
return max(st[l][k], st[r-(<<k)+][k]);
} int getbignext(int pos, int val) {
int l = pos + , r = N, mid;
while (r > l) {
mid = (l + r) / ;
if (rmq(pos+, mid) <= val) l = mid + ;
else r = mid;
}
return l;
}
void initdp() {
int cnt = ;
dp[N] = ;
for (int i = N - ; i >= ; i--) {
int pos = getbignext(i, h[i]);
dp[i] = dp[pos] + ;
}
sel[] = ; pre[] = -; c[] = ;
int last = , lasth = h[];
for (int i = ; i < N; i++) {
pre[i] = last;
if (h[i] > lasth){
sel[i] = ;
last = i;
lasth = h[i];
cnt++;
} else sel[i] = ;
c[i] = cnt;
}
tot = cnt;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T;scanf("%d",&T);
while(T--){
int Q;
scanf("%d%d",&N,&Q);
for(int i=;i<N;++i) scanf("%d",&h[i]);
init_st();
initdp();
int p,q;
while(Q--){
scanf("%d%d",&p,&q);
p--;
int res=;
if(sel[p]){ //这个位置保持递增
if(p==){
res = dp[getbignext(p,q)]+;
}
else{
if(q>h[pre[p]]){
res += c[p];
res += dp[getbignext(p,q)];
}
else{
res += c[pre[p]];
res += dp[getbignext(p,h[pre[p]])];
}
}
}
else{
if(q<=h[pre[p]]){
res= tot;
}
else{
res += c[pre[p]]+;
res += dp[getbignext(p,q)];
}
}
printf("%d\n",res);
}
}
return ;
}
HDU - 6406 Taotao Picks Apples (RMQ+dp+二分)的更多相关文章
- [乱搞]hdu 6406 Taotao picks apples 笛卡尔树+倍增
题目链接 Problem Description There is an apple tree in front of Taotao's house. When autumn comes, n app ...
- hdu 6406 Taotao Picks Apples 线段树 单点更新
Taotao Picks Apples Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Ot ...
- hdu 6406 Taotao Picks Apples (线段树)
Problem Description There is an apple tree in front of Taotao's house. When autumn comes, n apples o ...
- hdu 6406 Taotao Picks Apples (2018 Multi-University Training Contest 8 1010)(二分,前缀和)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6406 思路: 暴力,预处理三个前缀和:[1,n]桃子会被摘掉,1到当前点的最大值,1到当前点被摘掉的桃子的 ...
- HDU 6406 Taotao Picks Apples & FJUT3592 做完其他题后才能做的题(线段树)题解
题意(FJUT翻译HDU): 钱陶陶家门前有一棵苹果树. 秋天来了,树上的n个苹果成熟了,淘淘会去采摘这些苹果. 到园子里摘苹果时,淘淘将这些苹果从第一个苹果扫到最后一个. 如果当前的苹果是第一个苹果 ...
- HDU 6406 Taotao Picks Apples 线段树维护
题意:给个T,T组数据: 每组给个n,m:n个数,m个操作: (对序列的操作是,一开始假设你手上东西是-INF,到i=1时拿起1,之后遍历,遇到比手头上的数量大的数时替换(拿到手的算拿走),问最后拿走 ...
- 【杂题总汇】HDU-6406 Taotao Picks Apples
[HDU 6406]Taotao Picks Apples 多校赛的时候多写了一行代码就WA了……找了正解对拍,在比赛结束后17分钟AC了
- hdu6406 Taotao Picks Apples(线段树)
Taotao Picks Apples 题目传送门 解题思路 建立一颗线段树,维护当前区间内的最大值maxx和可摘取的苹果数num.最大值很容易维护,主要是可摘取的苹果数怎么合并.合并左右孩子时,左孩 ...
- 【hdu 6406】Taotao Picks Apples
[链接] 我是链接,点我呀:) [题意] 题意相当于问你改变一个位置之后. 从左往右扫描最大值.这个最大值会改变多少次. [题解] 假设我们改变的是i这个位置,下面说的a[i]都是改成q之后的a[i] ...
随机推荐
- heap corruption detected错误解决方法调试方法以及内存管理相关
1.heap corruption detected http://vopit.blog.51cto.com/2400931/645980 heap corruption detected:aft ...
- kvm和qemu的关系
KVM (Kernel Virtual Machine) is a Linux kernel module that allows a user space program to utilize th ...
- 用用匿名函数和闭包加apply强制待定函数调用时使用特定上下文
<button id="test">点我</button> <script> var button={ clicked:false, click ...
- ajaxupload异步上传文件
ajaxupload使用说明: http://blog.csdn.net/teresa502/article/details/7952486 servlet ajaxupload demo: http ...
- 第8步:安装Oracle
安装Oracle 注意,安装Oracle时需要以oracle用户身份执行,在那之前需要以root身份执行xhost+,即命令: 代码1 [root@sgdb1~]# xhost+ [root@sgdb ...
- Django项目实战 - 搜索功能(转)
首先,前端已实现搜索功能页面, 我们直接写后台逻辑: Q()可以实现 逻辑或的判断, name_ _ icontains 表示 name字段包含搜索的内容,i表示忽略大小写. from djang ...
- Asynchronous calls and remote callbacks using Lingo Spring Remoting
http://www.jroller.com/sjivan/entry/asynchronous_calls_and_callbacks_using Asynchronous calls and re ...
- 学习tornado
http://old.sebug.net/paper/books/tornado/ http://demo.pythoner.com/itt2zh/index.html http://tornado- ...
- JZOJ.5273【NOIP2017模拟8.14】亲戚
Description
- Asp.Net WebApi核心对象解析
在接着写Asp.Net WebApi核心对象解析(下篇)之前,还是一如既往的扯扯淡,元旦刚过,整个人还是处于晕的状态,一大早就来处理系统BUG,简直是坑爹(好在没让我元旦赶过来该BUG),队友挖的坑, ...