Taotao Picks Apples

题目传送门

解题思路

建立一颗线段树,维护当前区间内的最大值maxx和可摘取的苹果数num。最大值很容易维护,主要是可摘取的苹果数怎么合并。合并左右孩子时,左孩子里可摘取苹果必然还是可以摘取,所以我们讨论右孩子。

如果右孩子的最大值小于左孩子,根据题目条件,右孩子里的苹果都不能摘取了,合并结果就是左孩子的数量,如果右孩子的最大值大于左孩子,那么右孩子里存在可以摘取的苹果,接下来就是一个递归的过程,假设右孩子为t,左孩子的最大值为k,如果t->lchild->maxx < k, 那么t->lchild中不存在可摘取的苹果,递归t->rchild求t->rchild中的数量,如果t->lchild->maxx > k,那么t->lchild和t->rchild中都存在可摘取的苹果,而此时t->rchild已经不受k的影响了,只受t->lchild->maxx的影响,所以可以直接加上t->rchild->num,再加上递归求出的t->lchild中的数量。

代码如下

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll; inline int read(){
int res = 0, w = 0; char ch = 0;
while(!isdigit(ch)){
w |= ch == '-', ch = getchar();
}
while(isdigit(ch)){
res = (res << 3) + (res << 1) + (ch ^ 48);
ch = getchar();
}
return w ? -res : res;
} const int N = 100005;
int v[N];
struct node{
int l, r;
int maxx, num;
}tree[N << 2]; int get(int k, int x) //递归过程
{
if(tree[k].num == 1){
return tree[k].maxx > x;
}
if(tree[2*k].maxx > x)
return get(2*k, x) + (tree[k].num - tree[2*k].num);
else if(tree[2*k].maxx == x)
return tree[k].num - tree[2*k].num;
else
return get(2*k+1, x);
} void pushup(int k) //合并
{
tree[k].maxx = max(tree[2*k].maxx, tree[2*k+1].maxx);
tree[k].num = tree[2*k].num;
if(tree[2*k+1].maxx > tree[2*k].maxx)
tree[k].num += get(2*k+1, tree[2*k].maxx);
} void build(int k, int l, int r)
{
tree[k].l = l, tree[k].r = r;
if(l == r){
tree[k].maxx = v[l];
tree[k].num = 1;
return;
}
int mid = (l + r) / 2;
build(2*k, l, mid);
build(2*k+1, mid+1, r);
pushup(k);
} void update(int k, int p, int q)
{
if(tree[k].l == tree[k].r){
tree[k].maxx = q;
return;
}
int mid = (tree[k].l + tree[k].r) / 2;
if(p <= mid)
update(2*k, p, q);
else
update(2*k+1, p, q);
pushup(k);
} inline int query()
{
return tree[1].num;
} int main()
{
int t;
cin >> t;
while(t --){
int n, m;
n = read(), m = read();
for(int i = 1; i <= n; i ++)
v[i] = read();
build(1, 1, n);
for(int i = 1; i <= m; i ++){
int p, q;
p = read(), q = read();
update(1, p, q);
printf("%d\n", query());
update(1, p, v[p]);
}
}
return 0;
}

hdu6406 Taotao Picks Apples(线段树)的更多相关文章

  1. hdu 6406 Taotao Picks Apples 线段树 单点更新

    Taotao Picks Apples Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Ot ...

  2. HDU 6406 Taotao Picks Apples 线段树维护

    题意:给个T,T组数据: 每组给个n,m:n个数,m个操作: (对序列的操作是,一开始假设你手上东西是-INF,到i=1时拿起1,之后遍历,遇到比手头上的数量大的数时替换(拿到手的算拿走),问最后拿走 ...

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

    [HDU 6406]Taotao Picks Apples 多校赛的时候多写了一行代码就WA了……找了正解对拍,在比赛结束后17分钟AC了

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

    Problem Description There is an apple tree in front of Taotao's house. When autumn comes, n apples o ...

  5. [乱搞]hdu 6406 Taotao picks apples 笛卡尔树+倍增

    题目链接 Problem Description There is an apple tree in front of Taotao's house. When autumn comes, n app ...

  6. HDU 6406 Taotao Picks Apples & FJUT3592 做完其他题后才能做的题(线段树)题解

    题意(FJUT翻译HDU): 钱陶陶家门前有一棵苹果树. 秋天来了,树上的n个苹果成熟了,淘淘会去采摘这些苹果. 到园子里摘苹果时,淘淘将这些苹果从第一个苹果扫到最后一个. 如果当前的苹果是第一个苹果 ...

  7. 多校1010 Taotao Picks Apples

    >>点击进入原题<< 思路:题解很有意思,适合线段树进阶 #include<cstdio> #include<cmath> #include<cs ...

  8. 多校 1010 Taotao Picks Apples(补题)

    >>点击进入原题<< 思路:题解很有意思,适合线段树进阶 考虑每次修改不叠加,因此我们可以从如何对原序列进行预处理着手.通过观察可以发现,将原序列从任意位置断开,我们可以通过分 ...

  9. 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到当前点被摘掉的桃子的 ...

随机推荐

  1. 麻省理工的 Picture 语言:代码瘦身的秘诀

    直击现场 如今,机器学习算法已经进入了主流的计算机,而麻省理工学院正在研究一款让每日的编程变得更加简单的技术. MIT 研究者将在六月发布一款新的叫做 Picture 的编程语言,当计算机在视频或者图 ...

  2. Delphi指针运用理解

    现在是面向对象漫天飞的年代了,大家都在在谈面向对象编程.Java对指针“避而不谈”,C#虽然支持指针运用,但是也淡化处理. 然而,指针还是好完全掌握为妙,省得在开发过程碰钉子,至于对指针的运用在于开发 ...

  3. csdn token

    http://download.csdn.net/download/pp_haitun/9614126 http://dl.download.csdn.net/down11/20160826/28b9 ...

  4. never下的easysql

    什么是EasySql 在我们早期写的代码中,想实现组装灵活的sql语句与参数,我们可以去翻阅早期自己写的代码 var @sb = new StringBuilder(); sb.Append(&quo ...

  5. Junit4使用详解二:Junit4运行流程

    1.新建一个测试用例,把下面的四个方法勾选以便查看效果 2.我们在各个方法里面写上输出语句 3.运行之后我们可以发现,它的执行顺序是这样的 注:junit4中的运行流程 1.@BeforeClass修 ...

  6. Spark学习之路(十四)—— Spark Streaming 基本操作

    一.案例引入 这里先引入一个基本的案例来演示流的创建:获取指定端口上的数据并进行词频统计.项目依赖和代码实现如下: <dependency> <groupId>org.apac ...

  7. Null作为参数的时候,Java编译器如何调用函数?

    public class TestNull { public void method(Object o){ System.out.println("Object Version") ...

  8. node中的session的使用

    Session不是一个天生就有的技术,它的使用需要依赖cookie. session依赖cookie,当一个浏览器禁用cookie的时候,登陆效果消失: 或者用户清除了cookie,登陆也消失,ses ...

  9. C语言类型转换

    int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. itoa():将整型值转 ...

  10. nginx实现最简单的直播

    系统环境 [root@yunwei-test live]# cat /etc/redhat-release CentOS Linux release (Core) [root@yunwei-test ...