【CF1132G】Greedy Subsequences(线段树)

【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;
}

原文地址:https://www.cnblogs.com/cjyyb/p/10657428.html

时间: 2024-11-05 15:56:21

【CF1132G】Greedy Subsequences(线段树)的相关文章

Codeforces 1132G Greedy Subsequences 线段树

Greedy Subsequences 我们先找到每个点的右边第一个比它大的, 然后从大的往它建边, 然后可以发现这是一棵树. 我们令d[ i ] 为 i 号点往上走最多能走几步, 我们能用线段树维护d 的值. 我们加入点 i 的时候, 我们把它的值设为 d[ fa ] + 1, 我们删除 i 的时候, 把 i 这棵子树中的d 都减 1, 每次询问最大值就好啦. #include<bits/stdc++.h> #define LL long long #define LD long doubl

ACM学习历程——HDU2227 Find the nondecreasing subsequences(线段树 &amp;&amp; dp)

Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., sn} ? For example, we assume that S = {1, 2, 3}, and you can find seven nondecreasing subsequences, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.

hdu3698 Let the light guide us dp+线段树优化

http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 821    Accepted Submission(s): 285 Problem Description Plain of despair was

SPOJ 1557. Can you answer these queries II 线段树

Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/problems/GSS2/ Description Being a completist and a simplist, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse

题解 HDU 3698 Let the light guide us Dp + 线段树优化

http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 759    Accepted Submission(s): 253 Problem Description Plain of despair was

BNU 49100超级线段树

超级线段树 Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Font Size:  +   - Type:   None Graph Theory      2-SAT     Articulation/Bridge/Biconnected Component  

线段树求LIS并统计最长子序列个数

以下面的题目为例(题目和代码在最后面),给定一个数列(长度最大为10000),求出最长的先增后减子序列长度及个数.做法是先求出以每一个位置结尾的最长单增子序列长度以及以该位置开头的最长单减子序列长度,然后遍历所有位置找出最大先增后减子序列长度. 以最长单增序列(LIS)为例,由于不仅需要整个序列LIS的长度,还要保存以每个位置为结尾位置的LIS长度.记以a[i]结尾的LIS长度为dp[i],则 dp[i] = max{dp[j] | a[j] < a[i]} + 1 这就是一个RMQ问题,涉及单

Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵

Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries on the string.There are two types of queries:1. Flipping the bits (i.e., changing all 1 to 0 and 0 to 1) between l and r (inclusive).2. Counting the

HDU 6155 Subsequence Count 线段树维护矩阵

Subsequence Count Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others) Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries on the string. There are two types of querie