Taotao Picks Apples

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.

题意:就是要你从第一数开始选,选的数必须严格大于前面的数,问最多可以选多少个。

有m次讯问,每次改变一个高度,但互不影响。

思路:这个我是看了这位大佬的思路,挺清晰的

https://blog.csdn.net/ccsu_cat/article/details/81711440

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long

using namespace std;
const int maxn=1e5+100;
struct node
{
    int l,r,pos,val;
};

node Tree[4*maxn];
int dp1[maxn];
int dp2[maxn];
int a[maxn];
int cur;
void init()
{
    memset(dp1,0,sizeof(dp1));
    memset(dp2,0,sizeof(dp2));
    return;
}
void push_up(int id)
{
    if(Tree[id<<1].val>=Tree[id<<1|1].val)
    {
        Tree[id].val=Tree[id<<1].val;
        Tree[id].pos=Tree[id<<1].pos;
        return ;
    }
    else
    {
       Tree[id].val=Tree[id<<1|1].val;
       Tree[id].pos=Tree[id<<1|1].pos;
    }
}
void bulid(int l,int r,int id)
{
    Tree[id].l=l;
    Tree[id].r=r;
    if(l==r)
    {
        Tree[id].val=a[l];
        Tree[id].pos=l;
        return ;
    }
    int mid=(l+r)/2;
    bulid(l,mid,id<<1);
    bulid(mid+1,r,id<<1|1);
    push_up(id);
    return ;
}
void query(int l,int r,int k,int id)
{
    if(Tree[id].l==Tree[id].r)
    {
        if(Tree[id].val>k)
        {
            cur=min(cur,Tree[id].pos);
        }
        return ;
    }
    if(l<=Tree[id].l&&Tree[id].r<=r)
    {
        if(Tree[id<<1].val>k)
        {
            query(l,r,k,id<<1);
        }
        else if(Tree[id<<1|1].val>k)
        {
            query(l,r,k,id<<1|1);
        }
        return;
    }
    int mm=(Tree[id].l+Tree[id].r)/2;
    if(mm>=l)
    {
        query(l,r,k,id<<1);
    }
    if(mm<r)
    {
        query(l,r,k,id<<1|1);
    }
    return ;
}
void queryy(int l,int r,int id)
{
    if(l<=Tree[id].l&&Tree[id].r<=r)
    {
        if(Tree[id].val>a[cur])
        {
            cur=Tree[id].pos;
        }
        return ;
    }
    int mm=(Tree[id].l+Tree[id].r)/2;
    if(mm>=l)
    {
        queryy(l,r,id<<1);
    }
    if(mm<r)
    {
        queryy(l,r,id<<1|1);
    }
    return ;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        int mx=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]>mx)
            {
                dp1[i]=dp1[i-1]+1;
                mx=max(mx,a[i]);
            }
            else
            {
                dp1[i]=dp1[i-1];
            }
        }
        bulid(1,n,1);
        for(int i=n;i>=1;i--)
        {
            cur=n+1;
            query(i,n,a[i],1);
            if(cur>n)
            {
                cur=0;
            }
            dp2[i]=dp2[cur]+1;
        }
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            int ans=0;
            cur=0;
            queryy(1,x-1,1);
            ans+=dp1[cur];
            if(a[cur]<y)
            {
                ans++;
            }
            else
            {
                y=a[cur];
            }
            cur=n+1;
            query(x+1,n,y,1);
            if(cur<=n)
                ans+=dp2[cur];
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值