#include<bits/stdc++.h>
using namespace std;
int st[1000007];
int top;
int s[1000007],t[1000007];
int mx[4000007];
int sum[4000007];
int head[1000007],to[2000007],nex[2000007];
int n,k;
int a[10000077];
int dfn;
int tot;
void pushup(int rt){
    mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
}
void pushdown(int rt){
    if(sum[rt]){
        sum[rt<<1]+=sum[rt];
        sum[rt<<1|1]+=sum[rt];
        mx[rt<<1]+=sum[rt];
        mx[rt<<1|1]+=sum[rt];
        sum[rt]=0;
    }
}
void change(int rt,int l,int r,int L,int R,int x){//线段树区间更新
    if(L<=l&&r<=R){
        sum[rt]+=x;
        mx[rt]+=x;
        return ;
    }
    pushdown(rt);
    int mid=(l+r)>>1;
    if(L<=mid)
        change(rt<<1,l,mid,L,R,x);
    if(R>mid)
        change(rt<<1|1,mid+1,r,L,R,x);
    pushup(rt);
}
void add(int x,int y){//链式前向星,连边
    nex[++tot]=head[x];//上一条边
    head[x]=tot;//最后一条边
    to[tot]=y;//连向的点
}
void dfs(int x){
    s[x]=++dfn;//这个点子树深度的最小值
    for(int i=head[x];i;i=nex[i])
        dfs(to[i]);
    t[x]=dfn;//这个点子树深度的最大值
}
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        while(top&&a[st[top]]<a[i]){//单调栈
            add(i,st[top]);//连边,前一个是后一个离得最近的比它大的数的位置
            top--;
        }
        st[++top]=i;//进入栈顶
    }
    while(top){
        add(n+1,st[top]);//顶端的数字给一个根节点将它们建成树
        top--;
    }
    dfs(n+1);//从根节点遍历
    for(int i=1;i<=k;i++)
        change(1,1,n+1,s[i],t[i],1);
    printf("%d ",mx[1]);
    for(int i=k+1;i<=n;i++){
        change(1,1,n+1,s[i],t[i],1);//当区间右端点右移时,新加入区间的这个数会对原区间中比这个数小的数的答案+1,也就是将这个数在树上的子树中所有点的答案+1(这些答案+1的所有点中虽然包括区间之前的数但显然这些数的答案不会比区间内数的答案更大,最多只会与最大值相同)
        change(1,1,n+1,s[i-k],t[i-k],-1);//同样当区间左端点右移时,就将这个数在树上的子树中所有点的答案−1来确保区间之前的数的答案不会比区间中数的答案更优
        printf("%d ",mx[1]);
    }
}

Educational Codeforces Round 61 (Rated for Div. 2) G(线段树,单调栈)的更多相关文章

  1. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  2. Educational Codeforces Round 61 (Rated for Div. 2) D,F题解

    D. Stressful Training 题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有n台电脑,每台电脑都有初始电量ai,也有一个 ...

  3. Educational Codeforces Round 61 (Rated for Div. 2) E 多重背包优化

    https://codeforces.com/contest/1132/problem/E 题意 有8种物品,重量是1~8,每种数量是\(cnt[i]\)(1e16),问容量为W(1e18)的背包最多 ...

  4. Educational Codeforces Round 61 (Rated for Div. 2)-C. Painting the Fence 前缀和优化

    题意就是给出多个区间,要求去掉两个区间,使得剩下的区间覆盖范围最大. 当然比赛的时候还是没能做出来,不得不佩服大佬的各种姿势. 当时我想的是用线段树维护区间和,然后用单点判0,维护区间间断个数.然后打 ...

  5. Educational Codeforces Round 61 (Rated for Div. 2)

    A. Regular Bracket Sequence 题意:给出四种括号的数量 ((  )) ()  )( 问是否可以组成合法的序列(只能排序不能插在另外一个的中间) 思路: 条件一:一个或 n个) ...

  6. Educational Codeforces Round 61 (Rated for Div. 2) E. Knapsack

    非常经典的dp题,因为1至8的最大公约数是840,任何一个数的和中840的倍数都是可以放在一起算的, 所以我只需要统计840*8的值(每个数字(1-8)的sum%840的总和),剩下都是840的倍数 ...

  7. Educational Codeforces Round 61 (Rated for Div. 2)F(区间DP,思维,枚举)

    #include<bits/stdc++.h>typedef long long ll;const int inf=0x3f3f3f3f;using namespace std;char ...

  8. Educational Codeforces Round 61 (Rated for Div. 2)D(二分,模拟,思维)

    #include<bits/stdc++.h>using namespace std;typedef long long ll;int n,k;ll a[200007],b[200007] ...

  9. Educational Codeforces Round 58 (Rated for Div. 2) G 线性基

    https://codeforces.com/contest/1101/problem/G 题意 一个有n个数字的数组a[],将区间分成尽可能多段,使得段之间的相互组合异或和不等于零 题解 根据线性基 ...

随机推荐

  1. spring+hibernate ---含AOP--事务--laobai

    biz包: package com.etc.biz; import java.util.List; import org.springframework.orm.hibernate3.support. ...

  2. SpringMVC 接收表单数据的方式

    1.@RequestParam @RequestMapping(value = "/xxxx.do") public void create(@RequestParam(value ...

  3. delphi 天气预报

    天气预报 var astream : tmemorystream; sStream : TStringStream; jv : TJSONValue; begin astream := tmemory ...

  4. 在线HTTP POST/GET接口测试工具

    在线HTTP POST/GET接口测试工具 http://www.atool.org/httptest.php

  5. Uboot详细解析2

    1.第二阶段的主线函数位于u-boot-2010.06/arch/arm/lib/board.c. 第二阶段的功能: <1> 初始化本阶段要使用到的硬件设备. 设置时钟.初始化串口. bo ...

  6. Python函数之返回值、作用域和局部变量

    一.函数返回值 说到返回值,相信大家肯定都认识,没错,就是return. 所谓返回值可以这样理解:函数外部的代码要想获取函数的执行结果,就可以在函数里用return语句把结果返回. 那具体怎么用呢?接 ...

  7. Java 基于spring 暴露接口 供外部调用

    在springmvc的配置文件添加创建如下的bean: <!-- 暴露一个webService连接 --> <bean class="org.springframework ...

  8. c语言实践 1/1+1/2+1/3+1/4+...+1/n

    给定一个n求这个分式的和. int n = 1; float sum = 0; float frac = 0; int i = 1; scanf_s("%d",&n); w ...

  9. Java——操作Excel表格,读取表格内容

    JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Windows操作系统也可以通过 ...

  10. DataTable 设置primarykey 后进行 Merge操作

    1.先查看概念 可以看highplayer博客 http://blog.csdn.net/highplayer/article/details/6613817 2. protected void st ...