Taotao Picks Apples

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

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.
 

给你长度为n的序列, 然后从位置1,寻找单调栈的最大长度

然后修改 v[pos] = val, 再求 单调栈的最大长度

但是队友说是单调栈 ,我就用线段树维护了个单调栈,但是我维护的好像有些SB,就是每次左区间的最大值+(用单调栈跑一次最大长度)

然后完美的T了 ,然后就没管这道题了

后来看了一个人的题解,是这样子分析的

对于每一个区间, 贡献只能从左区间  + 右区间的部分选择

然后 考虑: 两种情况 ,如果 右区间的最大值 <= 左区间最大值,那么右区间肯定没有贡献,为0

然后考虑 :如果右区间的最大值 >  左区间最大值,那么问题可以递归 右区间的左儿子 和 右儿子的情况

这样的复杂度 大概是T*m*logn*logn (单点更新val值一个log  然后每次 合并区间的时候又要一个log)

#include <bits/stdc++.h>
using namespace std; const int N = 1e5+;
#define ls rt<<1
#define rs rt<<1|1 int mx[N<<],cnt[N<<]; int query(int rt,int l,int r,int v) {
if(l==r) return mx[rt] > v;
if(mx[rt] <= v) return ;
int m = (l+r)>>;
if(mx[ls] <= v) return query(rs,m+,r,v);
else return cnt[rt]-cnt[ls]+query(ls,l,m,v);
}
int n,m,v[N];
void build(int rt,int l,int r) {
mx[rt] = cnt[rt] =;
if(l == r) {
mx[rt]=v[l]; cnt[rt]=;
return ;
}
int m=(l+r)>>;
build(ls,l,m);
build(rs,m+,r);
mx[rt]=max(mx[ls], mx[rs]);
cnt[rt] = cnt[ls] + query(rs,m+,r,mx[ls]);
} void update(int rt,int l,int r,int pos,int val) {
if(l==r && l == pos) {
mx[rt]=val;
cnt[rt] = ;
return ;
}
int m = (l+r)>>;
if(pos <= m)
update(ls,l,m,pos,val);
else
update(rs,m+,r,pos,val);
mx[rt]=max(mx[ls], mx[rs]);
cnt[rt] = cnt[ls] + query(rs,m+,r,mx[ls]);
} int main() {
//freopen("in.txt","r",stdin);
int T; scanf("%d",&T);
while (T--) {
scanf("%d %d", &n, &m);
for(int i=;i<=n;i++)
scanf("%d",&v[i]);
build(,,n);
while (m--) {
int pos, val;
scanf("%d %d",&pos,&val);
//pos位置 更新成val
update(,,n,pos,val);
printf("%d\n",cnt[]);
//还原
update(,,n,pos,v[pos]);
}
}
return ;
}
 

最新文章

  1. Django1.9 Python3.4出现Error loading MySQLdb
  2. 一个小应用的dbcp和c3p0配置实例
  3. BZOJ 1050: [HAOI2006]旅行comf( 并查集 )
  4. 关于window service2008系统iis部署访问证书,内部错误
  5. VIM基础知识整理(附思维导图)
  6. python pip安装方法
  7. NodeJS FTP模块使用
  8. 用css实现正方形div
  9. Python appium搭建app自动化测试环境
  10. Window上安装—Docker 笔记
  11. XamarinAndroid组件教程设置动画的设置插值器
  12. Kubernetes Service Account如何生成Token
  13. Spring Boot(九):定时任务
  14. 垃圾回收相关(深入理解Java虚拟机中的内容)
  15. PHP empty、isset、isnull的区别
  16. Java遇到的问题、错误——持续更新
  17. STL源码分析-list
  18. sql server数据库查询超时报错
  19. JVM内存模型以及堆分配参数
  20. java NIO之SelectedKey

热门文章

  1. Java-idea-设置类头注释和方法注释
  2. 给JSON中put的value=null时,这对key=value会被隐藏掉。
  3. POJ:2528(Mayor&#39;s posters)离散化成段更新+简单哈希
  4. [LeetCode] 252. Meeting Rooms_Easy tag: Sort
  5. #C++初学记录(sort函数)
  6. IN的另类写法
  7. yii2--windows下composer安装
  8. tomcat 的最大连接数设置
  9. DeepMind已将AlphaGo引入多领域 Al泡沫严重
  10. nginx日志过滤相同IP方法