HDU 6406 Taotao Picks Apples(前缀和+二分)

Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1282 Accepted Submission(s): 396

Problem Description

There is an apple tree in front of Taotao’s house. When autumn comes, n apples on the tree ripen, and Taotao will go to pick these apples.
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?

Input

The first line of input is a single line of integer T (1≤T≤10), the number of test cases.
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.

Output

For each query, display the answer in a single line.

Sample Input

1
5 3
1 2 3 4 4
1 5
5 5
2 3

Sample Output

1
5
3Hint
For the first query, the heights of the apples were 5, 2, 3, 4, 4, so Taotao would only pick the first apple.For the second query, the heights of the apples were 1, 2, 3, 4, 5, so Taotao would pick all these five apples.For the third query, the heights of the apples were 1, 3, 3, 4, 4, so Taotao would pick the first, the second and the fourth apples.

题意

  给一个序列,每次贪心选取比前一个更大的数。每次询问修改一个数,求修改后的序列的能选出多少个数。询问不叠加。

解题思路

  这个题标程里面给的是ST+二分,也能用线段树写,但我想不到线段树如何维护区间。比赛的时候拿到就想到前缀和+二分了,复杂度是O(nlogn),写了几次wa了几次,心态有点崩,后来找到了一种类型的样例,发现没多少时间了,草草的去跟队友怼第一题,所幸怼过了。这题到最后都没写出来。

  比赛的时候一直想不通,对于该拿的点,如果把它变得特别小,那后面的原本不能拿的可能就会变成可以拿的,那我最开始预处理的前缀数组就用不上了。比赛结束后看了一个大佬的博客http://www.bubuko.com/infodetail-2729068.html,他的思路跟我的一样,他在处理我说的那种情况的时候很巧妙,用了一个不定长数组,存下除去第i个元素,其后剩余元素与其前面的元素构成的一层“阶梯”的元素,细节看代码吧,那一部分借鉴了大佬的代码。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+50;int arr[maxn],aaa[maxn],bbb[maxn],different[maxn];
//arr数组保存的是原序列
//aaa数组存的是max(arr[1,i])
//bbb数组存的是前缀
//different数组存的是原本该拿走的值
vector<int> vec[maxn];
//该数组里本质上是除去第i个元素,其后剩余元素与其前面的元素构成的一层“阶梯”int main()
{
#ifdef DEBUGfreopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
#endif // DEBUGint t,tmp,cnt,next,flag,pos;scanf("%d",&t);while(t--){int n,m;scanf("%d%d",&n,&m);for(int i=1; i<=n; i++) scanf("%d",&arr[i]);if(n==1) //处理n=1的情况{for(int i=0; i<m; i++) printf("1\n");continue;}for(int i=0; i<=n; i++) vec[i].clear();//清空tmp=-1,cnt=0,next=-1,flag=0;aaa[0]=0;for(int i=1; i<=n; i++){if(arr[i]>tmp){next=-1;tmp=arr[i];different[cnt++]=tmp;pos=i;flag=0;}if(!flag) flag=1;else{if(arr[i]>next&&arr[i]>different[cnt-2])//除去能摘的i个元素剩余的能摘的就入数组{//所以这里需要与different[cnt-2]比较,不会REnext=arr[i];vec[pos].push_back(next);}}aaa[i]=tmp;bbb[i]=cnt;}while(m--){int u,v,sum=0;scanf("%d%d",&u,&v);if(v>arr[u]){if(v<=aaa[u-1]) sum=bbb[n];else{sum+=1+bbb[u-1];//前半段不受影响int id=upper_bound(different,different+cnt,v)-different;if(id!=cnt) sum+=cnt-id;}}else if(v<arr[u]){sum+=bbb[u-1];if(v>aaa[u-1]) sum++;if(arr[u]==aaa[u])//对于原先该摘的点{int id=upper_bound(vec[u].begin(),vec[u].end(),v)-vec[u].begin();//看对这一维的影响,找到位置sum+=vec[u].size()-id;int idd=upper_bound(different,different+cnt,arr[u])-different;if(idd!=cnt) sum+=cnt-idd;}else sum=bbb[n];}else sum=bbb[n];printf("%d\n",sum);}}return 0;
}

HDU 6406 Taotao Picks Apples(前缀和+二分)相关推荐

  1. hdu - 6406 Taotao Picks Apples(离线+离散+技巧)

    题目链接:Taotao Picks Apples 题目大意:有n个数,m个操作,每个操作x,q,将x位的数字改为q,输出改完数后数组的递增序列有多长(只能从第一个数开始找,并且必须依次找更大的数). ...

  2. hdu 6406 Taotao Picks Apples 线段树

    hdu 6406 多校八第十题,设d1[ i ]为从a1开始到 ai 的最长递增子序列长度,设d2[ i ]为从ai开始到an的最长递增子序列长度,假设要把ap改为q,可以把序列分为1到p-1和p+1 ...

  3. HDU - 6406 Taotao Picks Apples(线段树区间合并)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列 a,再给出 m 次询问,每次询问假设如果设置 a[ pos ] = val 的话,那么此时序列中的最长上升子序列是多少,此时的上升子序列指的是 ...

  4. 多校 hdu 6406 Taotao Picks Apples(线段树)

    设d1[ i ]为从a1开始到 ai 的最长递增子序列长度,设d2[ i ]为从ai开始到an的最长递增子序列长度,假设要把ap改为q,可以把序列分为1到p-1和p+1到n两段,可以用线段树找到1到p ...

  5. Taotao Picks Apples HDU - 6406

    K - Taotao Picks Apples 题目链接:HDU - 6406 题意:树上有一排苹果,每个苹果又同的高度,taotao要摘苹果,而且必须从第一个苹果开始摘,然后没遇到一个高度比之前摘得 ...

  6. 【杂题总汇】HDU-6406 Taotao Picks Apples

    [HDU 6406]Taotao Picks Apples 多校赛的时候多写了一行代码就WA了--找了正解对拍,在比赛结束后17分钟AC了? ◇ 题目 +传送门+ <手写翻译> 有n个苹果 ...

  7. 【hdu 6406】Taotao Picks Apples

    [链接] 我是链接,点我呀:) [题意] 题意相当于问你改变一个位置之后. 从左往右扫描最大值.这个最大值会改变多少次. [题解] 假设我们改变的是i这个位置,下面说的a[i]都是改成q之后的a[i] ...

  8. 2018 Multi-University Training Contest 8 1010 Taotao Picks Apples【二分】

    http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1010&cid=809 题意:求上升序列(能上升则上升无需最长),有修改 ...

  9. Taotao Picks Apples

    ps:这题的难点,寻找修改的位置之后有多少个苹果能被吃掉,比赛的时候想到的是讨论修改的位置,预处理每个位置的最大值和吃到苹果的数量,记最大值所在的位置为 id, 记修改的位置为 pos,分三种情况讨论 ...

最新文章

  1. 2017年诺贝尔生理学或医学奖揭晓
  2. 大学计算机html,编程基础(C+VB+HTML)(辅)19级计算机
  3. Jquery中将数组转换成Json
  4. 北京内推 | 微软亚洲研究院机器学习组招聘AI for Health实习生
  5. oracle存储过程多条件判断,oracle存储过程条件替空的判断(2)
  6. 文件夹里面照片自动分成子文件夹_Windows居然自带这个功能,自动整理你硬盘里的照片...
  7. 计算机网络线路争用,计算机网络系统集成复习要点
  8. POJ_2513Colored Sticks 字典树+
  9. Silverlight Telerik控件学习:主题Theme切换
  10. IDEA之Git分支以及Stash使用
  11. Yum介绍与常见用法
  12. mvc actionresult返回各种文件
  13. 基于linux环境tcp网络编程(在线英英词典)文档【2】
  14. 数据分析sql面试必会6题经典_面试准备:数据库常见面试题汇总
  15. 开源数据库管理系统现在比商业产品更受欢迎
  16. elasticsearch 常见的概念
  17. 测试过程中印象最深刻的bug?| 万能回答必杀技
  18. element ui表单必填_详解element-ui设置下拉选择切换必填和非必填
  19. Matlab官方在线代码搜索网站,很多开源matlab代码
  20. Android微信支付订单支付失败的问题

热门文章

  1. spring线程池的理解和使用
  2. 单机版本CDH6.3.2搭建教程
  3. height clientHeight scrollHeight offsetHeight的大致区别
  4. 魅族16S“SOC点胶门”事件落幕,买一赔二!
  5. 我的故事:从一段“Hello World“开始
  6. 今天终于考试完笔试了......
  7. PC浏览器开发者模式手机模拟器demo和下载图片demo
  8. 【Hive】毫秒时间戳格式化
  9. 基于有限状态机在Unity3D中实现的简单搓招系统
  10. 计算机制作表格的结论,计算机基础实验报告电子表格处理-20210320050712.docx-原创力文档...