hdu6406 Taotao Picks Apples 多校第8场1010

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

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.

预处理在RMQ+二分,挺好的题目。

dp1,为前缀,dp2为后缀。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1e5+10;
 4 int dmax[N][25],a[N], cnt[N], dp1[N], dp2[N];
 5 int que[N], t, v, l, n, m;
 6 void init(){
 7     for(int i = 1; i <= n; i ++){
 8         dmax[i][0] = a[i];
 9     }
10     for(int j = 1; (1<<j) <= n; j ++){
11         for(int i = 1; i+(1<<j)-1<= n; i ++){
12             dmax[i][j] = max(dmax[i][j-1],dmax[(1<<(j-1))+i][j-1]);
13         }
14     }
15 }
16 int getValue(int l, int r){
17     int k = 0;
18     while(1<<(k+1) <= (r-l+1))k++;
19     return max(dmax[l][k],dmax[r-(1<<k)+1][k]);
20 }
21 int find(int l, int r, int x) {
22     int pos = -1;
23     while(l <= r) {
24         int m = (l+r) >> 1;
25         if(getValue(l, m) > x) {
26             pos = m;
27             r = m-1;
28         } else l = m+1;
29     }
30     return pos;
31 }
32
33 int main() {
34     scanf("%d", &t);
35     while(t--) {
36         scanf("%d%d", &n, &m);
37         int mx = 0;
38         for(int i = 1; i <= n; i ++) {
39             scanf("%d", &a[i]);
40             if(mx < a[i]) {
41                 mx = a[i];
42                 dp1[i] = dp1[i-1]+1;
43             } else dp1[i] = dp1[i-1];
44             cnt[i] = max(a[i], cnt[i-1]);
45         }
46         init();
47         int tail = 0, head = 1;
48         for(int i = n; i >= 1; i --) {
49             while(head <= tail && que[tail] <= a[i]) tail --;
50             que[++tail] = a[i];
51             dp2[i] = (tail- head + 1);
52         }
53         while(m--) {
54             scanf("%d%d", &l, &v);
55             int ans = dp1[l-1];
56             if(v > cnt[l-1]) ++ ans;
57             v = max(v, cnt[l-1]);
58             int pos = find(l+1, n, v);
59             if(pos != -1) ans += dp2[pos];
60             printf("%d\n",ans);
61         }
62     }
63     return 0;
64 }

原文地址:https://www.cnblogs.com/xingkongyihao/p/9489494.html

时间: 2024-08-30 14:45:36

hdu6406 Taotao Picks Apples 多校第8场1010的相关文章

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

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

2014多校第五场1010 || HDU 4920 Matrix multiplication(矩阵乘法优化)

题目链接 题意 : 给你两个n*n的矩阵,然后两个相乘得出结果是多少. 思路 :一开始因为知道会超时所以没敢用最普通的方法做,所以一直在想要怎么处理,没想到鹏哥告诉我们后台数据是随机跑的,所以极端数据是不可能会有的,而我们一开始一直在想极端数据能接受的方法......后来看了鹏哥的做法,就是把是0的地方都跳过就可以了,用矩阵保存前一个非0数的位置是多少.二师兄给我看了一个代码,人家根本没用别的优化,直接将最里层k的循环提到了最外层,然后就AC了,对此我表示无语. 1 #include <cstd

2014多校第六场 1010 || HDU 4930 Fighting the Landlords (模拟)

题目链接 题意 : 玩斗地主,出一把,只要你这一把对方要不了或者你出这一把之后手里没牌了就算你赢. 思路 : 一开始看了第一段以为要出很多次,实际上只问了第一次你能不能赢或者能不能把牌出尽. 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 5 using namespace std ; 6 7 char str1[20],str2[20] ; 8 int hash1[20],hash2[2

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到当前点被摘掉的桃子的数量,然后我们枚举修改p点造成的所有影响,: 1,假如新输入的点比原先的点的值更大,那么我们对修改后p这个点的值和[1,p-1]的最大值关系进行分析,也就是分析前半段的影响:(1)如果p点大于1-p-1的最大值的时候我们直接利用前缀和O(1)得到[1,p-1]有多少个桃子被摘掉,然后加上当前这个.(

【hdu 6406】Taotao Picks Apples

[链接] 我是链接,点我呀:) [题意] 题意相当于问你改变一个位置之后. 从左往右扫描最大值.这个最大值会改变多少次. [题解] 假设我们改变的是i这个位置,下面说的a[i]都是改成q之后的a[i] 我们完全可以直接暴力算出来左边的最大值会改变多少次以及右边的最大值会改变多少次. 那么如何找呢? 首先在1..i-1当中找到那个最大值a[idx1] 这个可以用st表预处理出来. 然后如果a[i]这个位置是更新最大值中的某一次的话ans++. 显然a[i]要满足>a[idx1] 如果不满足的话,那

HDU 6395(2018多校第7场1010)Sequence

不久前做过POJ3070,所以知道这题要用矩阵快速幂优化,但是这个题的递推公式中有一项?p/n?,场上就不会了... 下来才知道要用分块矩阵快速幂,因为?p/n?最多有2√p块,可以对每一块使用快速幂,复杂度(应该)为lgn*√p. 每一块的范围可以在O(1)的时间内求出,范围为x到min(n,p/(p/x)),具体证明lyd的进阶指南上有... 附上代码: #include<cstdio> #include<algorithm> #include<cstring> u

2014多校第十场1004 || HDU 4974 A simple water problem

题目链接 题意 : n支队伍,每场两个队伍表演,有可能两个队伍都得一分,也可能其中一个队伍一分,也可能都是0分,每个队伍将参加的场次得到的分数加起来,给你每个队伍最终得分,让你计算至少表演了几场. 思路 : ans = max(maxx,(sum+1)/2) :其实想想就可以,如果所有得分中最大值没有和的一半大,那就是队伍中一半一半对打,否则的话最大的那个就都包了. 1 #include <cstdio> 2 #include <cstring> 3 #include <st

多校第八场:图论出度

HDU 4948 这题比赛的时候遗憾了,我看了这道题,然后觉得挺简单的. 刚开始一看题上,想到的就是拓扑排序,然后脑子想啊想--感觉就是拓扑排序的逆序,然后发现挺水的-- 因为说了要想发展某个城市的话,就必须有另一个城市作为它发展的前提,即城市u->w这样连边,表示要想发展城市w,前提是u已经是发展过的城市了.那这样的话不是很简单嘛. 即统计出出度最多的就是第一个要发展的城市了,因为u->w这样连边可以看出算出出度最多的依次从大到小排序就行了. 哎呀,不过可惜了,因为看见没人交这题,然后也不敢

多校第六场 HDU 4927 JAVA大数类

题目大意:给定一个长度为n的序列a,每次生成一个新的序列,长度为n-1,新序列b中bi=ai+1?ai,直到序列长度为1.输出最后的数. 思路:这题实在是太晕了,比赛的时候搞了四个小时,从T到WA,唉--对算组合还是不太了解啊,现在对组合算比较什么了-- import java.io.*; import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { Sca