zoukankan      html  css  js  c++  java
  • Codeforces VK Cup 2015

    D. Closest Equals

    Time Limit: 1 Sec  Memory Limit: 256 MB

    题目连接

    http://www.lydsy.com/JudgeOnline/problem.php?id=3224

    Description

    You are given sequence a1, a2, ..., an and m queries lj, rj (1 ≤ lj ≤ rj ≤ n). For each query you need to print the minimum distance between such pair of elements ax and ay (x ≠ y), that:

    • both indexes of the elements lie within range [lj, rj], that is, lj ≤ x, y ≤ rj;
    • the values of the elements are equal, that is ax = ay.

    The text above understands distance as |x - y|.

    Input

    The first line of the input contains a pair of integers n, m (1 ≤ n, m ≤ 5·105) — the length of the sequence and the number of queries, correspondingly.

    The second line contains the sequence of integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).

    Next m lines contain the queries, one per line. Each query is given by a pair of numbers lj, rj (1 ≤ lj ≤ rj ≤ n) — the indexes of the query range limits.

    Output

    Print m integers — the answers to each query. If there is no valid match for some query, please print -1 as an answer to this query.

    Sample Input

    5 3
    1 1 2 3 2
    1 5
    2 4
    3 5

    Sample Output

    1
    -1
    2

    HINT

    题意

     查询区间相同数的最小距离

    题解:

    用一个map记录前面的位置,然后离线搞一搞
    用心去体会,我也不好说……

    单点更新,区间查询最小值

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 500001
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=0x3f3f3f3f;
    /*
    
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    */
    //**************************************************************************************
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    
    struct node{
        int l,r,v;
    }a[maxn*4];
    struct ques
    {
        int l,r,an,v;
    }qu[maxn];
    void build(int x,int l,int r)
    {
        a[x].l=l,a[x].r=r;
        a[x].v=maxn;
        if(l==r)
            return;
        int mid=(l+r)>>1;
        build(x<<1,l,mid);
        build(x<<1|1,mid+1,r);
    }
    void pushup(int x)
    {
        a[x].v=min(a[x<<1].v,a[x<<1|1].v);
    }
    void update(int x,int pos,int val)
    {
        if(a[x].l==a[x].r)
        {
            a[x].v=val;
            return;
        }
        int mid=(a[x].l+a[x].r)>>1;
        if(pos<=mid)
            update(x<<1,pos,val);
        else
            update(x<<1|1,pos,val);
        pushup(x);
    }
    int mi;
    void query(int x,int l,int r)
    {
        if(l<=a[x].r&&r>=a[x].r)
        {
            mi=min(mi,a[x].v);
            return;
        }
        int mid=(a[x].l+a[x].r)>>1;
        if(l<=mid)
            query(x<<1,l,r);
        if(r>mid)
            query(x<<1|1,l,r);
    }
    map<int,int>mp;
    int d[maxn];
    bool cmp(ques a,ques b)
    {
        return a.l>b.l;
    }
    int ans[maxn];
    int main()
    {
        int n=read(),m=read();
        build(1,1,n);
        for(int i=1;i<=n;i++)
            d[i]=read();
        for(int i=1;i<=m;i++)
        {
            qu[i].l=read(),qu[i].r=read();
            qu[i].v=i;
        }
        sort(qu+1,qu+1+m,cmp);
        int t=1;
        for(int i=n;i;i--)
        {
            if(mp[d[i]])
            {
                update(1,mp[d[i]],mp[d[i]]-i);
            }
            mp[d[i]]=i;
            while(qu[t].l==i)
            {
                mi=maxn;
                query(1,qu[t].l,qu[t].r);
                if(mi==maxn)
                    mi=-1;
                ans[qu[t].v]=mi;
                t++;
            }
        }
        for(int i=1;i<=m;i++)
        {
            if(ans[i]<0)
                printf("-1
    ");
            else
                P(ans[i]);
        }
    }
  • 相关阅读:
    解决 Mac launchpad 启动台 Gitter 图标无法删除的问题
    React 与 React-Native 使用同一个 meteor 后台
    解决 React-Native mac 运行报错 error Failed to build iOS project. We ran "xcodebuild" command but it exited with error code 65. To debug build logs further, consider building your app with Xcode.app, by ope
    一行命令更新所有 npm 依赖包
    swift学习笔记
    IOS语言总结
    focusSNS学习笔记
    别小看锤子,老罗真的很认真
    windowsphone开发页面跳转到另一个dll中的页面
    【令人振奋】【转】微软潘正磊谈DevOps、Visual Studio 2013新功能、.NET未来
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4442451.html
Copyright © 2011-2022 走看看