码迷,mamicode.com
首页 > 其他好文 > 详细

2019 Multi-University Training Contest 2

时间:2019-09-23 22:32:31      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:lap   span   contest   ide   turn   upd   play   +=   push   

 

Longest Subarray

技术图片
#include<bits/stdc++.h>
using namespace std;

const int maxn=1e5+10;
vector<int> v[maxn];
int n,k,c;
struct note
{
    int left,right,maxx,lazy,ans;
    void up(int val)
    {
        maxx+=val;
        lazy+=val;
    }
} tree[maxn*4];
void pushup(int id)
{
    tree[id].maxx=max(tree[id<<1].maxx,tree[id<<1|1].maxx);
    if(tree[id].maxx==tree[id<<1].maxx) tree[id].ans=tree[id<<1].ans;
    else tree[id].ans=tree[id<<1|1].ans;
}
void pushdown(int id)
{
    if(tree[id].lazy)
    {
        tree[id<<1].up(tree[id].lazy);
        tree[id<<1|1].up(tree[id].lazy);
        tree[id].lazy=0;
    }
}

void build(int id,int l,int r)
{
    tree[id].left=tree[id].ans=l;
    tree[id].right=r;
    tree[id].maxx=tree[id].lazy=0;
    if(l==r)
        return;
    int mid=(l+r)/2;
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
    pushup(id);
}
int query(int id,int l,int r)
{
    if(tree[id].maxx!=c)
        return -1;
    if(l<=tree[id].left&&tree[id].right<=r)
        return tree[id].ans;
    pushdown(id);
    int mid=(tree[id].left+tree[id].right)/2;
    if(r<=mid) return query(id<<1,l,r);
    else if(l>mid) return query(id<<1|1,l,r);
    else
    {
        int tmp;
        tmp=query(id<<1,l,mid);
        if(tmp!=-1) return tmp;
        else return query(id<<1|1,mid+1,r);
    }
}
void update(int id,int l,int r,int val)
{
    if(l<=tree[id].left&&tree[id].right<=r)
    {
        tree[id].up(val);
        return;
    }
    pushdown(id);
    int mid=(tree[id].left+tree[id].right)/2;
    if(l<=mid) update(id<<1,l,r,val);
    if(r>mid) update(id<<1|1,l,r,val);
    pushup(id);
}


int main()
{
    while(~scanf("%d%d%d",&n,&c,&k))
    {
        for(int i=1; i<=c; i++)
        {
            v[i].clear();
            v[i].push_back(0);
        }
        build(1,1,n);
        int ans=0;
        for(int i=1; i<=n; i++)
        {
            int x;
            scanf("%d",&x);
            update(1,i,i,c-1);
            if(v[x].back()<i-1) update(1,v[x].back()+1,i-1,-1);
            v[x].push_back(i);
            if(v[x].size()>=k+1)
            {
                int pos=v[x].size()-k-1;
                update(1,v[x][pos]+1,v[x][pos+1],1);
            }
            int tmp=query(1,1,i);
            if(tmp!=-1)
                ans=max(ans,i-tmp+1);
        }
        printf("%d\n",ans);
    }
}
View Code

 

2019 Multi-University Training Contest 2

标签:lap   span   contest   ide   turn   upd   play   +=   push   

原文地址:https://www.cnblogs.com/dongdong25800/p/11575187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!