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 ;
}
 

hdu 6406 Taotao Picks Apples 线段树 单点更新的更多相关文章

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

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

  2. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

  3. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  4. HDU 1166 敌兵布阵(线段树单点更新,板子题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  5. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  6. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

  7. HDU 1166 敌兵布阵(线段树单点更新,区间查询)

    描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...

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

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

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

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

随机推荐

  1. 在Java中谈尾递归--尾递归和垃圾回收的比较

    我不是故意在JAVA中谈尾递归的,因为在JAVA中谈尾递归真的是要绕好几个弯,只是我确实只有JAVA学得比较好,虽然确实C是在学校学过还考了90+,真学得没自学的JAVA好 不过也是因为要绕几个弯,所 ...

  2. SQL Server 镜像证书过期处理

    转自:https://www.cnblogs.com/trams/archive/2012/01/13/2321637.html SQL Server 镜像证书过期处理 今天镜像中的主服务器进行维护重 ...

  3. java-JProfiler(二)-进行本地JVM的性能监控-tomcat

    监视本地的Tomcat, 看似是本地,其实JProfiler GUI在一个单独的JVM里启动,他与被监视的目标jvm之间通过socket通讯,目的为了不干扰目标JVM.所以监视本地Tomcat与监视远 ...

  4. 走进APICloud的世界 (1)

    APICloud是什么东东?它是一个云端一体平台.啥意思?它利用HTML5跨平台技术同时满足android和ios的APP开发.相比APP传统开发而言,节约了不少成本,而且性能还可以和原生APP性能比 ...

  5. xgb, lgb, Keras, LR(二分类、多分类代码)

    preprocess # 通用的预处理框架 import pandas as pd import numpy as np import scipy as sp # 文件读取 def read_csv_ ...

  6. selenium webdriver窗口切换(下)

    多窗口切换有时候需要在不同的窗口切换,从而操作不同的窗口上的元素.在selenium1.0 中这个问题比较难处理.但WebDriver 提供了switcTo.window()方法可以切换到任意的窗口. ...

  7. iOS开发--UILabel根据内容自动调整高度

    写法一:对象方法,传入:字体/最大尺寸. 即可得到宽高, 最大尺寸主要限制宽度,如果是一行就给个{MAXFLOAT,MAXFLOAT};如果是多行就限制X值,Y值随便给 - (CGSize)sizeW ...

  8. volatile的语义与实现

    1.volatile关键字的两层语义 一旦一个共享变量(类的成员变量.类的静态成员变量)被volatile修饰之后,那么就具备了两层语义: 1)保证了不同线程对这个变量进行操作时的可见性,即一个线程修 ...

  9. JSTL—标签

    什么是JSTL标签? Jsp标准标签库(JSP Standerd Tag Library) JSTL的优点是什么? 1) 提供一组标准的标签 2)可用于编写动态功能 使用JSTL的步骤? 1)引入ja ...

  10. Amaze UI JS 气泡弹出

    http://amazeui.org/javascript/popover?_ver=2.x