【题解】Closest Equals

Closest Equals

https://ac.nowcoder.com/acm/problem/110867

题:https://ac.nowcoder.com/acm/problem/110867
题意:给定n个数序列,m个询问[l,r]问l~r中距离最短的且a[x]==ay,输出最短距离(n,m<=5e5)
分析:

  • 同一种数的话只需要和其相邻比较;
  • 其次,思考怎么这个pair会在选定的范围内;
  • 考虑离线处理查询,以r作为排序基准,那么只需要在某对pair的左边处添加这对pair的贡献,然后线段树搜查询范围查询[l,r]最小值即可。
  • 时间复杂度:mlogn+nlogn
  • (主要思考:区间贡献询问到且不影响区间查询)
    Code:
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define MP make_pair
#define lson root<<1,l,midd
#define rson root<<1|1,midd+1,r
typedef long long ll;
const int mod=1e9+7;
const int M=5e5+5;
const int inf=0x3f3f3f3f;
const ll INF=1e18;

int a[M],ans[M];
struct node{
    int l,r,id;
}q[M];
unordered_map<int,int>lastpos;
struct SegTree{
    int tr[M<<2];
    void push_up(int root){
        tr[root]=min(tr[root<<1],tr[root<<1|1]);
    }
    void build(int root,int l,int r){
        if(l==r){
            tr[root]=inf;
            return ;
        }
        int midd=(l+r)>>1;
        build(lson);
        build(rson);
        push_up(root);
    }

    void Modify(int pos,int c,int root,int l,int r){
        if(l==r){
            tr[root]=c;
            return ;
        }
        int midd=(l+r)>>1;
        if(pos<=midd)
            Modify(pos,c,lson);
        else
            Modify(pos,c,rson);
        push_up(root);
    }
    int query(int L,int R,int root,int l,int r){
        if(L<=l&&r<=R){
            return tr[root];
        }
        int midd=(l+r)>>1;
        int res=inf;
        if(L<=midd)
            res=min(res,query(L,R,lson));
        if(R>midd)
            res=min(res,query(L,R,rson));
        return res;
    }
}t1;
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=m;i++)
        scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
    sort(q+1,q+1+m,[&](node A,node B){
        if(A.r==B.r)
            return A.l<B.l;
        return A.r<B.r;
    });

    t1.build(1,1,n);
    int nowi=1;
    for(int i=1;i<=m;i++){
        while(nowi<=q[i].r){
            int tmp=lastpos[a[nowi]];
            lastpos[a[nowi]]=nowi;
            if(tmp!=0)
                t1.Modify(tmp,nowi-tmp,1,1,n);
            nowi++;
        }
        ans[q[i].id]=t1.query(q[i].l,q[i].r,1,1,n);
    }
    for(int i=1;i<=m;i++)
        printf("%d\n",ans[i]==inf?-1:ans[i]);
    return 0;
}
全部评论
starve_to_death是我的博客
点赞 回复 分享
发布于 2020-09-16 12:45

相关推荐

今天 16:40
已编辑
门头沟学院 C++
26学院本太难了,很多公司机筛就给我刷了。机会都难拿到如果是简历存在问题也欢迎拷打————————————————————分割线——————————————————————2026.3.4更新:发完贴之后,时不时投递又收到了不少的笔试/面试邀请。主要是之前投递简历出去之后基本上都是沉默状态,年后好转了不少timeline:2026.01.21&nbsp;文远知行笔试,半年多没刷算法题&nbsp;-&gt;挂&nbsp;(后续HR说春招可以重新安排笔试)2026.2.4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;小鹏汇天&nbsp;技术一面,第二周收到结果&nbsp;-&gt;挂2026.2.12&nbsp;&nbsp;&nbsp;大众Cariad代招&nbsp;技术二面&nbsp;-&gt;Offer2026.2.28&nbsp;&nbsp;&nbsp;多益网络技术面试,由于风评太差,一直在犹豫要不要接面试&nbsp;-&gt;推迟-----------分割线-----------2026.3&nbsp;月前的某一天,临时去电网报名了二批计算机岗位的笔试2026.3.6&nbsp;从上家公司实习离职,氛围最好的一家公司,leader&nbsp;说可以帮忙转正,但是流程太长,而且我们部门据说只有一个&nbsp;hc,更想要研究生,我很有可能是会被签外包公司在这里干活,就离职了。2026.3.9&nbsp;入职新公司,大众Cariad&nbsp;以外部公司的身份进组,项目组签了三年,后续三年应该都可以在这里呆,不知道有没有希望原地跳槽。2026.3.10&nbsp;电网考试居然说我通过资格审查了,短信约我去参加资格审查,请假一天,买了&nbsp;12&nbsp;号晚上的机票回成都2026.3.15&nbsp;参加国家电网计算机类笔试2026.3.17&nbsp;电网出成绩了,感觉很低。觉得已经🈚️了2026.3.18&nbsp;收到电网面试通知,通知&nbsp;3.22-3.25&nbsp;这个时间去面试,我的岗位只招&nbsp;1&nbsp;个人。据说面试只有&nbsp;2-3&nbsp;人,不知道能不能成功----------分割线-----------2026.3.21&nbsp;电网面试结束,感觉回答的还勉勉强强,大概是2个岗位分别招1个人,一共11人面试,实际来了9人2026.3.27&nbsp;出面试成绩,满分100分,早上10:20左右发现面试成绩46,我震惊了,没截图,后面过了十分钟重新看发现面试成绩给我改成58了。但同样震惊。朋友问我是不是把面试官打了,哈哈
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务