洛谷 P2417 [线段树+二分]

题目链接

//线段树维护区间最大值,区间查找时使用二分,分别找到第一个大于等于区间左右两端的年份,通过相等关系具体分析
//m*log(n)
#include<bits/stdc++.h>
using namespace std;
const int MAX=5e4+10;
int Y[MAX],R[MAX];
struct node{
    int l,r,maxn;
}tr[MAX*4];
void build(int now,int l,int r){
    tr[now].l=l,tr[now].r=r;
    if(l==r){
        tr[now].maxn=R[l];
        return;
    }
    int mid=l+r>>1;
    build(now<<1,l,mid);
    build(now<<1|1,mid+1,r);
    tr[now].maxn=max(tr[now<<1].maxn,tr[now<<1|1].maxn);
    return;
}
int query(int now,int l,int r){
    if(l>r) return 0;
    if(tr[now].l>r||tr[now].r<l) return 0;
    if(tr[now].l>=l&&tr[now].r<=r){
        return tr[now].maxn;
    }
    return max(query(now<<1,l,r),query(now<<1|1,l,r));
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&Y[i],&R[i]);
    }
    build(1,1,n);
    int m;
    scanf("%d",&m);
    while(m--){
        int ly,ry;
        scanf("%d%d",&ly,&ry);
        int l=lower_bound(Y+1,Y+n+1,ly)-Y,r=lower_bound(Y+1,Y+n+1,ry)-Y;//二分查找
        if(Y[l]==ly&&Y[r]==ry){//两端均存在,只有这种情况可能会输出true
            int Maxn=query(1,l+1,r-1);//注意查询范围
            if(R[l]<R[r]||Maxn>=R[r]){//右端点值超过左端点,或中间值不严格小于右端点
                printf("false\n");
            }
            else if(ry-ly>r-l){//如果中间有不存在年份输出maybe
                printf("maybe\n");
            }
            else{
                printf("true\n");
            }
        }
        else if(Y[l]==ly){
            int Maxn=query(1,l+1,r-1);//
            if(Maxn>=R[l]){//注意判断条件
                printf("false\n");
            }
            else{
                printf("maybe\n");
            }
        }
        else if(Y[r]==ry){
            int Maxn=query(1,l,r-1);//左端点虚则可以扩大左侧查找范围
            if(Maxn>=R[r]){
                printf("false\n");
            }
            else{
                printf("maybe\n");
            }            
        }
        else{
            printf("maybe\n");//两边均不存在则无法判断
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷P1168题目是关于中位数线段树解法的问题。中位数线段树解法可以通过维护两个堆来实现。一个是大根堆,一个是小根堆。每次插入元素时,根据一定的规则来维护这两个堆,使得大根堆的个数在一定情况下比小根堆多1或者相等。大根堆的最后一个元素即为中位数。具体的规则如下: 1. 如果大根堆和小根堆的个数相等,下一次插入的元素一定插入到大根堆。此时判断小根堆的堆顶是否大于当前元素x,如果是,则将小根堆的堆顶元素插入到大根堆,然后将x压入小根堆;否则直接将x压入大根堆。 2. 如果大根堆和小根堆的个数不相等,按照类似的规则进行操作。 通过以上规则,可以实现在每次插入元素时,维护两个堆的平衡,并且保证大根堆的最后一个元素即为中位数。 这种解法的时间复杂度为O(logN),其中N为序列的长度。 #### 引用[.reference_title] - *1* *2* [中位数(洛谷p1168)(堆/树状数组+二分/线段树+二分)](https://blog.csdn.net/qq_45604735/article/details/114382762)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [洛谷 P1168 中位数(权值线段树,离散化)](https://blog.csdn.net/qq_38232157/article/details/127594230)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值