【hdu 6406】Taotao Picks Apples

【链接】 我是链接,点我呀:)
【题意】

题意相当于问你改变一个位置之后。
从左往右扫描最大值。这个最大值会改变多少次。

【题解】

假设我们改变的是i这个位置,下面说的a[i]都是改成q之后的a[i]
我们完全可以直接暴力算出来左边的最大值会改变多少次以及右边的最大值会改变多少次。
那么如何找呢?
首先在1..i-1当中找到那个最大值a[idx1]
这个可以用st表预处理出来。
然后如果a[i]这个位置是更新最大值中的某一次的话ans++。
显然a[i]要满足>a[idx1]
如果不满足的话,那么a[i]就不是更新最大值中的某一次,ans不变就好仍然为0
然后我们可以通过一次扫描就能预处理出来到达a[idx1]需要更新多少次最大值。

然后我们可以用ST表。找出来最近的大于key的数字a[idx2]
(只需看看L..mid这一段最大值(用st表获得)是不是大于key,是的话R = mid-1否则L = mid +1,这样写一下二分就能求出a[idx2]了
这里的key,如果a[i]>a[idx1],那么key = a[i]否则key = a[idx1];(因为要满足递增的性质。
(注意千万别以为是upper_bound....upper_bound找的是最小的大于key的数字a[idx2]...比如a={2 4 3}|,set.upper_bound(2)的结果是3而不是离他更近的4.。。。。
之后只要求出a[idx2]开始还要更新多少次最大值就可以了。

这个可以从后往前维护一个单调队列。单调队列的长度就是更新最大值的次数。
ans+=到达idx1更新的次数+idx2到最后更新的次数。
输出ans就好。

【代码】

#include <bits/stdc++.h>
using namespace std;

const int N = 1E5;

int n,m,a[N+10],_size[N+10];
int dl[N+10],h,t,step[N+10],dp[N+10][20];

int ma(int i,int j){
    if (a[i]>=a[j]) return i;
    return j;
}

int get_ma(int l,int r){
    if (l>r) return 0;
    int len  = log2(r-l+1);
    return ma(dp[l][len],dp[r-(1<<len)+1][len]);
}

int get_first_bigger(int q,int L){
    int l = L,r = n,temp = -1;
    while (l <= r){
        int mid = (l+r)>>1;
        if (a[get_ma(L,mid)]>q){
            temp = mid;
            r = mid-1;
        }else l = mid + 1;
    }
    return temp;
}

int main(){
    //freopen("D:\\rush.txt","r",stdin);
    //freopen("D:\\rush_out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while (T--){
        scanf("%d%d",&n,&m);
        for (int i = 1;i <= n;i++) scanf("%d",&a[i]);
        h = 1,t = 0;
        for (int i = n;i >= 1;i--){
            while (h<=t && a[i]>=a[dl[t]]) t--;
            dl[++t] = i;
            _size[i] = t-h+1;
        }
        step[1] = 1;
        int cur = 1;
        for (int i = 2;i <= n;i++)
            if (a[i]>a[cur]){
                step[i] = step[cur]+1;
                cur = i;
            }
        for (int i = 0;i <= n;i++) dp[i][0] = i;
        for (int l = 1;l <= 17;l++){
            for (int i = 1;i <= n;i++){
                if ((i+(1<<l)-1)>n) break;
                dp[i][l] = ma(dp[i][l-1],dp[i+(1<<(l-1))][l-1]);
            }
        }
        for (int i = 1;i <= m;i++){
            int p,q;
            scanf("%d%d",&p,&q);
            int idx = get_ma(1,p-1);
            int key = q,ans = 0;
            if (q<=a[idx]){
                key = a[idx];
                ans = -1;
            }
            int idx2 = get_first_bigger(key,p+1);
            if (idx2==-1){
                ans += step[idx]+1;
            }else{
                ans += step[idx]+1+_size[idx2];
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/AWCXV/p/9486282.html

时间: 2024-10-01 10:14:00

【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/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

【HDU 4940】Destroy Transportation system(数据水/无源无汇带上下界可行流)

Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s represent his enemy’s transportation system as a simple directed graph G with n nodes and m edges. Each node is a city and each directed edge is a directe

【HDU 1009】FatMouse&#39; Trade

题 Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of c

【HDU 5647】DZY Loves Connecting(树DP)

pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 112 Problem Description DZY has an unroote

【2014 Multi-University Training Contest 3 1002】/【HDU 4888】 Redraw Beautiful Drawings

不容易啊,终于可以补第二个题了!! 顺便说一句:模版写残了就不要怪出题人啊 ~ (这残废模版研究了好长时间才找出错) 题目大意: 有一个n*m的矩阵,每一个格子里都将有一个数.给你每一行数字之和和每一列数字之和.求每一个位置能填0~k之间的哪个数.如果有多种可能输出"Not Unique",如果没有解输出"Impossible",如果一组解则将其输出. 解题思路: 最大流: 不可能的条件:是行之和和列之和不想等或者建图后的最大流与他们不想等. 多组的条件是:在最大流

【HDU 1839】 Delay Constrained Maximum Capacity Path(二分+最短路)

[HDU 1839] Delay Constrained Maximum Capacity Path(二分+最短路) Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 481 Problem

【HDU 5828】Rikka with Sequence(线段树)

[HDU 5828]Rikka with Sequence(线段树) Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2311    Accepted Submission(s): 391 Problem Description As we know, Rikka is poor at math.

【HDU 4352】 XHXJ&#39;s LIS (数位DP+状态压缩+LIS)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2422    Accepted Submission(s): 990 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

【HDU 5811】Colosseo(拓扑+输入优化)

[HDU 5811]Colosseo(拓扑+输入优化) Colosseo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 446    Accepted Submission(s): 98 Problem Description Mr. Chopsticks keeps N monsters, numbered from 1 to N.