【CF1132G】Greedy Subsequences(線段樹)
阿新 • • 發佈:2019-04-05
++ names space turn ret include tps stream ostream
【CF1132G】Greedy Subsequences(線段樹)
題面
CF
題解
首先發現選完一個數之後選擇下一個數一定是確定的。
對於每個數預處理出左側第一個比他大的數\(L\),那麽這個數加入進來之後\([L+1,i]\)的答案都會增加一,拿線段樹維護一下就行了。
#include<iostream> #include<cstdio> using namespace std; #define MAX 1000100 inline int read() { int x=0;bool t=false;char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-')ch=getchar(); if(ch=='-')t=true,ch=getchar(); while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar(); return t?-x:x; } int a[MAX],n,K,St[MAX],top,L[MAX]; #define lson (now<<1) #define rson (now<<1|1) int t[MAX<<2],tag[MAX<<2]; void puttag(int now,int w){t[now]+=w;tag[now]+=w;} void pushdown(int now) { if(!tag[now])return; puttag(lson,tag[now]);puttag(rson,tag[now]); tag[now]=0; } void Modify(int now,int l,int r,int L,int R,int w) { if(L<=l&&r<=R){puttag(now,w);return;} int mid=(l+r)>>1;pushdown(now); if(L<=mid)Modify(lson,l,mid,L,R,w); if(R>mid)Modify(rson,mid+1,r,L,R,w); t[now]=max(t[lson],t[rson]); } int Query(int now,int l,int r,int L,int R) { if(L<=l&&r<=R)return t[now]; int mid=(l+r)>>1,ret=0;pushdown(now); if(L<=mid)ret=max(ret,Query(lson,l,mid,L,R)); if(R>mid)ret=max(ret,Query(rson,mid+1,r,L,R)); return ret; } int main() { n=read();K=read(); for(int i=1;i<=n;++i)a[i]=read(); for(int i=1;i<=n;++i) { while(top&&a[St[top]]<a[i])--top; L[i]=St[top];St[++top]=i; } for(int i=1;i<=n;++i) { Modify(1,1,n,L[i]+1,i,1); if(i>=K)printf("%d ",Query(1,1,n,i-K+1,i)); } return 0; }
【CF1132G】Greedy Subsequences(線段樹)