CodeForces 69 E.Subsegments(权值线段树)

E.Subsegments

Programmer Sasha has recently begun to study data structures. His coach Stas told him to solve the problem of finding a minimum on the segment of the array in , which Sasha coped with. For Sasha not to think that he had learned all, Stas gave him a new task. For each segment of the fixed length Sasha must find the maximum element of those that occur on the given segment exactly once. Help Sasha solve this problem.

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ n) — the number of array elements and the length of the segment.

Then follow n lines: the i-th one contains a single number ai ( - 109 ≤ ai ≤ 109).

Output

Print n–k + 1 numbers, one per line: on the i-th line print of the maximum number of those numbers from the subarray ai ai + 1 … ai + k - 1 that occur in this subarray exactly 1 time. If there are no such numbers in this subarray, print “Nothing”.

Examples
Input

5 3
1
2
2
3
3

Output

1
3
2

Input

6 4
3
3
3
4
4
2

Output

4
Nothing
3

思路:

题目要求只出现一次的数才参与计算,可以用权值线段树的桶记录,如果桶容量不为1则设置为空,为1的时候才非空能被查找,查询的时候如果右子树不为空优先找右子树,这样就可以保证最大。

code:
#include<bits/stdc++.h>
using namespace std;
const int maxm=1e5+5;
int a[maxm];
int xx[maxm];
int cnt[maxm<<2];
int flag[maxm<<2];
void pushup(int node){
    flag[node]=(flag[node*2]|flag[node*2+1]);
}
void add(int x,int val,int l,int r,int node){
    if(l==r){
        cnt[node]+=val;
        flag[node]=(cnt[node]==1);
        return ;
    }
    int mid=(l+r)/2;
    if(x<=mid){
        add(x,val,l,mid,node*2);
    }else{
        add(x,val,mid+1,r,node*2+1);
    }
    pushup(node);
}
int ask(int l,int r,int node){
    if(flag[node]==0)return -1;//如果没有只出现一次的数
    if(l==r)return l;
    int mid=(l+r)/2;
    if(flag[node*2+1]){//优先走右边
        return ask(mid+1,r,node*2+1);
    }else{//否则走左边
        return ask(l,mid,node*2);
    }
}
signed main(){
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        xx[i]=a[i];
    }
    sort(xx+1,xx+1+n);
    int num=unique(xx+1,xx+1+n)-xx-1;
    for(int i=1;i<=n;i++){
        a[i]=lower_bound(xx+1,xx+1+num,a[i])-xx;
    }
    for(int i=1;i<=n;i++){
        add(a[i],1,1,n,1);
        if(i>k){//del
            add(a[i-k],-1,1,n,1);
        }
        if(i>=k){//ask
            int ans=ask(1,n,1);
            if(ans!=-1){
                cout<<xx[ans]<<endl;
            }else{
                cout<<"Nothing"<<endl;
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值