hdu 6406 Taotao Picks Apples (线段树)
Problem Description
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
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
Sample Input
5 3
Sample Output
5
3
Hint
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.
Solution
拿到题最开始想用前缀和维护答案,但是没有理清思路,后来就乱了。看到一篇大神的线段树直接维护答案的题解,简单理了一下思路
对于线段树,维护区间最大值以及区间从左往右上升长度(即答案),我们注意到一个区间的答案=左区间答案数+右区间在左区间最大值基础上的贡献
对于一个查询query(rt,v)表示将区间t之前的一个数改为v后的区间rt的对答案的贡献,
若rt->maxx<v直接返回0,总的区间最大值小于v,这个区间不可能贡献答案
否则若rt->lson->maxx>v 此时只需关注左区间,因为右区间此时的贡献值和v不相关,注意这时的返回答案应该是tr[rt].cnt-tr[lson].cnt+query(lson,v),而不是tr[rson].cnt+query(lson,v)
这两者是不等价的,因为一个cnt表示的是当前区间从左到右的上升长度,而tr[rson].cnt仅仅关注的是右区间,没有和整体直接挂钩
再如果tr->lson->maxx<=v,此时左区间对答案无贡献,但右区间存在,查询右区间
大概就是这样,线段树维护答案很灵活感觉,唉本人垃圾是想不到了
贴一段大概理解之后写的ac代码吧
#include <bits/stdc++.h>
#define lson rt << 1
#define rson rt << 1 | 1
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pa = pair<int, int>;
using ld = long double;
int n, m, k;
const int maxn = 1e5 + ;
const int mod = ;
int pre[maxn];
template <class T>
inline T read(T &ret)
{
int f = ;
ret = ;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -;
ch = getchar();
}
while (isdigit(ch))
{
ret = (ret << ) + (ret << ) + ch - '';
ch = getchar();
}
ret *= f;
return ret;
}
template <class T>
inline void write(T n)
{
if (n < )
{
putchar('-');
n = -n;
}
if (n >= )
{
write(n / );
}
putchar(n % + '');
}
int a[maxn];
struct node
{
int l, r, cnt, maxx;
} tr[maxn << ];
int query(int rt, int v)
{
int l = tr[rt].l;
int r = tr[rt].r;
if (l == r)
return tr[rt].maxx > v;
if (tr[rt].maxx < v)
return ;
int mid = l + r >> ;
if (tr[lson].maxx > v) //疑惑,为什么写出tr[rson].cnt+query(lson,v)答案不对
//啊左右区间和大区间并不相等,大区间=左区间+右区间贡献
return tr[rt].cnt - tr[lson].cnt + query(lson, v);
else
return query(rson, v);
}
void pushup(int rt)
{
tr[rt].maxx = max(tr[lson].maxx, tr[rson].maxx);
tr[rt].cnt = tr[lson].cnt + query(rson, tr[lson].maxx);
}
void build(int rt, int l, int r)
{
tr[rt].l = l;
tr[rt].r = r;
if (l == r)
{
tr[rt].cnt = ;
tr[rt].maxx = a[l];
return;
}
int mid = l + r >> ;
build(lson, l, mid);
build(rson, mid + , r);
pushup(rt);
}
void update(int rt, int L, int v)
{
int l = tr[rt].l;
int r = tr[rt].r;
if (l == r && l == L)
{
tr[rt].maxx = v;
return;
}
int mid = l + r >> ;
if (L <= mid)
update(lson, L, v);
else
update(rson, L, v);
pushup(rt);
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int t;
cin >> t;
while (t--)
{
cin >> n >> m;
for (int i = ; i <= n; i++)
cin >> a[i];
build(, , n);
while (m--)
{
int p, q;
cin >> p >> q;
update(, p, q);
cout << tr[].cnt << "\n";
update(, p, a[p]);
}
}
return ;
}
hdu 6406 Taotao Picks Apples (线段树)的更多相关文章
- 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 线段树维护
题意:给个T,T组数据: 每组给个n,m:n个数,m个操作: (对序列的操作是,一开始假设你手上东西是-INF,到i=1时拿起1,之后遍历,遇到比手头上的数量大的数时替换(拿到手的算拿走),问最后拿走 ...
- [乱搞]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 & FJUT3592 做完其他题后才能做的题(线段树)题解
题意(FJUT翻译HDU): 钱陶陶家门前有一棵苹果树. 秋天来了,树上的n个苹果成熟了,淘淘会去采摘这些苹果. 到园子里摘苹果时,淘淘将这些苹果从第一个苹果扫到最后一个. 如果当前的苹果是第一个苹果 ...
- 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 (RMQ+dp+二分)
题意:N个高度为hi的果子,摘果子的个数是从位置1开始从左到右的严格递增子序列的个数.有M次操作,每次操作对初始序列修改位置p的果子高度为q.每次操作后输出修改后能摘到得数目. 分析:将序列分为左.右 ...
- hdu6406 Taotao Picks Apples(线段树)
Taotao Picks Apples 题目传送门 解题思路 建立一颗线段树,维护当前区间内的最大值maxx和可摘取的苹果数num.最大值很容易维护,主要是可摘取的苹果数怎么合并.合并左右孩子时,左孩 ...
- 【杂题总汇】HDU-6406 Taotao Picks Apples
[HDU 6406]Taotao Picks Apples 多校赛的时候多写了一行代码就WA了……找了正解对拍,在比赛结束后17分钟AC了
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
随机推荐
- 两张图示轻松看懂 UML 类图
一个类如何表示 第一格为类名 第二格为类中字段属性 格式:权限 属性名:类型 [ = 默认值 ] 权限:private.public .protected.default,它们分别对应 -.+.#.~ ...
- Enum的简单扩展
1 添加一个描述的Attribute public enum MessageResult { [System.ComponentModel.Description("未通过")] ...
- 工具资源系列之给 windows 虚拟机装个 mac
众说周知,Mac 很好但也很贵,对一般大众而言,漂亮简洁高颜值,对软件开发者而言,方便省心有点贵. 好到什么程度? 内置大量常用的开发工具,省去了初学者安装配置环境的麻烦,版本控制工具 svn 默认已 ...
- 哈工大计算机网络Week0-概述
目录 L01什么是计算机网络 计算机网络 Internet L02什么是网络协议? 定义 内容 三要素 L03计算机网络结构 网络边缘 接入网络 数字用户线路DSL 电缆网络 无线接入网络 网络核心( ...
- Coder必须自废的两样神功
Coder必须自废的两样神功 大理段氏以一阳指神功驰名天下.奉六脉宝经,立天龙佛院:凭借数百载基业威名,与嵩山少林.终南全真分庭抗礼:乃宋代中华武术三大派系之一. 二指禅是一种鲜为人知的秘传功法,通过 ...
- 彻底透析SpringBoot jar可执行原理
文章篇幅较长,但是包含了SpringBoot 可执行jar包从头到尾的原理,请读者耐心观看.同时文章是基于SpringBoot-2.1.3进行分析.涉及的知识点主要包括Maven的生命周期以及自定 ...
- Spring Cloud Alibaba | Nacos集群部署
目录 Spring Cloud Alibaba | Nacos集群部署 1. Nacos支持三种部署模式 2. 集群模式下部署Nacos 2.1 架构图 2.2 下载源码或者安装包 2.3 配置集群配 ...
- android surfaView surfaHolder video 播放
主文件 package cn.com.sxp;import android.app.Activity;import android.media.AudioManager;import android. ...
- 重新认识 async/await 语法糖
提起.Net中的 async/await,相信很多.neter 第一反应都会是异步编程,其本质是语法糖,但继续追查下去,既然是语法糖,那么经过编译之后,真正的代码是什么样的,如何执行的?带着这些疑问, ...
- 【弱化版】【P3371 【模板】单源最短路径(弱化版)】-C++
→原题传送门← 看到题目描述我就知道,这道题不能用SPFA[手动补滑稽] 那么我这道题目采用的是dijkstra算法不了解的去补一下知识哈. dij的模板: #include<bits/stdc ...