zoukankan      html  css  js  c++  java
  • 2020ICPC·小米 网络选拔赛第一场 E-Phone Network 线段树

    Phone Network

    题意

    给一个长度为(n)的数组(A_1,A_2,dots,A_n),和一个数字(m),询问(i)分别为(1,2,dots,m)时,(A)(1 sim i)每个数字至少出现一次的最短的区间的长度为多少。

    分析

    Code

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<iomanip>
    #include<sstream>
    #include<cstdio>
    #include<string>
    #include<vector>
    #include<bitset>
    #include<queue>
    #include<cmath>
    #include<stack>
    #include<set>
    #include<map>
    #define rep(i,x,n) for(int i=x;i<=n;i++)
    #define per(i,n,x) for(int i=n;i>=x;i--)
    #define sz(a) int(a.size())
    #define rson mid+1,r,p<<1|1
    #define pii pair<int,int>
    #define lson l,mid,p<<1
    #define ll long long
    #define pb push_back
    #define mp make_pair
    #define se second
    #define fi first
    using namespace std;
    const double eps=1e-8;
    const int mod=1e9+7;
    const int N=2e5+10;
    const int inf=1e9;
    int n,m;
    int a[N],tr[N<<2],mn[N<<2],tag[N<<2];
    vector<int>g[N];
    void pd(int l,int r,int p,int k){
        if(k==0) return;
        tr[p]=k;
        mn[p]=k-r+1;
        tag[p]=k;
    }
    void up(int dl,int dr,int l,int r,int p,int k){
        if(l==dl&&r==dr){
            tr[p]=k;
            mn[p]=k-r+1;
            tag[p]=k;
            return;
        }
        int mid=l+r>>1;
        pd(lson,tag[p]);pd(rson,tag[p]);tag[p]=0;
        if(dr<=mid) up(dl,dr,lson,k);
        else if(dl>mid) up(dl,dr,rson,k);
        else up(dl,mid,lson,k),up(mid+1,dr,rson,k);
        tr[p]=min(tr[p<<1],tr[p<<1|1]);
        mn[p]=min(mn[p<<1],mn[p<<1|1]);
    }
    int qy(int l,int r,int p,int k){
        if(l==r){
            if(tr[p]<=k) return r;
            return 0;
        }
        int mid=l+r>>1;
        pd(lson,tag[p]);pd(rson,tag[p]);tag[p]=0;
        if(tr[p<<1|1]<=k) return qy(rson,k);
        else return qy(lson,k);
    }
    int main(){
        //ios::sync_with_stdio(false);
        //freopen("in","r",stdin);
        scanf("%d%d",&n,&m);
        rep(i,1,n){
            scanf("%d",&a[i]);
            g[a[i]].pb(i);
        }
        rep(i,1,m){
            int pre=0;
            for(int x:g[i]){
                int l=pre+1,r=qy(1,n,1,x);
                r=min(r,x);
                if(l<=r) up(l,r,1,n,1,x);
                pre=x;
            }
            if(pre+1<=n) up(pre+1,n,1,n,1,inf);
            printf("%d ",mn[1]);
        }
        puts("");
        return 0;
    }
    
  • 相关阅读:
    [转载]要死的人都后悔些什么
    mysql优化之explain备忘笔记
    memcached 命令操作详解
    linux netstat 命令详解
    linux awk 命令详解
    定时任务FluentScheduler 学习笔记 .net
    webAPI文件上传时文件过大404错误的问题
    用递归形成树结构数据
    webAPP 图片上传
    webAPP踩坑记录
  • 原文地址:https://www.cnblogs.com/xyq0220/p/13882253.html
Copyright © 2011-2022 走看看