题面

传送门

分析

对于每一个数a[i],找到它后面第一个大于它的数a[p],由p向i连边,最终我们就会得到一个森林,且p是i的父亲。为了方便操作,我们再增加一个虚拟节点n+1,把森林变成树。

由于序列不是递增的,不能二分。维护一个单调栈,栈顶元素最小。从n到1依次对每个 数操作,弹出栈里比它小的数。如果栈为空,说明该数是森林中的根节点,向n+1连边。否则栈顶元素就是第一个大于它的数,向它的编号连边即可。

我们发现,对于每个查询区间内的所有数,它对应着树上的某些节点,记为标记节点。如果把标记节点之间的非标记节点去掉,我们就会得到一棵新树,新树上从某个节点到根的一条路径对应着一个满足条件的序列,则最大序列长度等于新树上从叶子节点到根的最长路径。这样,我们就把问题转化为了树上的最长路径。

显然不能对每一个询问建一棵新树。我们发现新树上的路径长度就是原树上的路径经过的标记节点个数,如图(加粗的节点为标记节点)。

所以,我们建立一棵线段树,线段树的叶子节点存储原树上每个节点到根的路径上的标记节点个数,线段树维护最大值。

我们枚举每个长度为k的区间[i,i+k-1],显然从前一个区间转移到当前区间时,只会增加一个标记节点,减少一个标记节点。每增加一个标记节点i,我们就将i的子树内的所有节点的值+1,否则-1。答案即为整颗线段树的最大值

时间复杂度\(O(n\log n)\)

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#define maxn 1000005
using namespace std;
int n,k;
int a[maxn];
struct edge{
int from;
int to;
int next;
}E[maxn<<1];
int head[maxn];
int sz=1;
void add_edge(int u,int v){
sz++;
E[sz].from=u;
E[sz].to=v;
E[sz].next=head[u];
head[u]=sz;
}
int cnt=0;
int lb[maxn],rb[maxn];
void dfs(int x,int fa){
lb[x]=++cnt;
for(int i=head[x];i;i=E[i].next){
int y=E[i].to;
if(y!=fa){
dfs(y,x);
}
}
rb[x]=cnt;
} struct node{
int l;
int r;
int v;
int mark;
}tree[maxn<<2];
void push_up(int pos){
tree[pos].v=max(tree[pos<<1].v,tree[pos<<1|1].v);
}
void build(int l,int r,int pos){
tree[pos].l=l;
tree[pos].r=r;
if(l==r){
return;
}
int mid=(l+r)>>1;
build(l,mid,pos<<1);
build(mid+1,r,pos<<1|1);
push_up(pos);
}
void push_down(int pos){
if(tree[pos].mark){
tree[pos<<1].v+=tree[pos].mark;
tree[pos<<1].mark+=tree[pos].mark;
tree[pos<<1|1].v+=tree[pos].mark;
tree[pos<<1|1].mark+=tree[pos].mark;
tree[pos].mark=0;
}
}
void update(int L,int R,int v,int pos){
if(L<=tree[pos].l&&R>=tree[pos].r){
tree[pos].v+=v;
tree[pos].mark+=v;
return;
}
push_down(pos);
int mid=(tree[pos].l+tree[pos].r)>>1;
if(L<=mid) update(L,R,v,pos<<1);
if(R>mid) update(L,R,v,pos<<1|1);
push_up(pos);
}
int query(int L,int R,int pos){
if(L<=tree[pos].l&&R>=tree[pos].r){
return tree[pos].v;
}
push_down(pos);
int mid=(tree[pos].l+tree[pos].r)>>1;
int ans=0;
if(L<=mid) ans=max(ans,query(L,R,pos<<1));
if(R>mid) ans=max(ans,query(L,R,pos<<1|1));
return ans;
} int nex[maxn];
void init(){
stack<int>s;
for(int i=n;i>=1;i--){
while(!s.empty()&&a[s.top()]<=a[i]) s.pop();
if(!s.empty()){
int p=s.top();
add_edge(p,i);
add_edge(i,p);
}else{
add_edge(n+1,i);
add_edge(i,n+1);
}
s.push(i);
}
dfs(n+1,0);
}
int main(){
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
init();
build(1,n+1,1);
for(int i=1;i<=k;i++){
update(lb[i],rb[i],1,1);
}
for(int i=1;i+k-1<=n;i++){
int r=i+k-1;
printf("%d ",query(1,n+1,1));
update(lb[i],rb[i],-1,1);
update(lb[r+1],rb[r+1],1,1);
}
}

Codeforces 1132G(dfs序+线段树)的更多相关文章

  1. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  2. Codeforces 396C (DFS序+线段树)

    题面 传送门 题目大意: 给定一棵树,每个点都有权值,边的长度均为1,有两种操作 操作1:将节点u的值增加x,并且对于u的子树中的任意一个点v,将它的值增加x-dist(u,v)*k, dist(u, ...

  3. Codeforces 1110F(DFS序+线段树)

    题面 传送门 分析 next_id = 1 id = array of length n filled with -1 visited = array of length n filled with ...

  4. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

  5. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  6. Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  7. CodeForces 877E Danil and a Part-time Job(dfs序+线段树)

    Danil decided to earn some money, so he had found a part-time job. The interview have went well, so ...

  8. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  9. BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)

    题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...

随机推荐

  1. android 完全退出应用程序(经过严格验证)

    今天解决了如何彻底结束Android应用程序的方法.网上有很多的参考方法,什么finish():android.os.Process.killProcess(android.os.Process.my ...

  2. ASP.NET CORE 2.0 模板 (Admin LTE)

    原文:https://www.jianshu.com/p/4916f380be66?utm_campaign=hugo&utm_medium=reader_share&utm_cont ...

  3. 记录混合APP开发遇到的坑!!

    1.在IOS中给body绑定click事件会失效 2.在IOS中<div contenteditable="true"></div>中点击时可以弹出键盘但是 ...

  4. PyInstaller库的使用

    PyInstaller库的使用 PyInstaller库用于将已经写好的py程序,转换成可以跨平台的可执行文件 使用方式 发布主要借助cmd命令行来实现.在当前目录的powershell下,输入 py ...

  5. smbstatus - 报告当前 samba 的联接状态

    总览 smbstatus [-b] [-d] [-L] [-p] [-S] [-s configuration file] [-u username] 描述 此程序是 samba 套件的一部分. sm ...

  6. go语言从例子开始之Example34.速率限制

    速率限制(英) 是一个重要的控制服务资源利用和质量的途径.Go 通过 Go 协程.通道和打点器优美的支持了速率限制. Example: package main import "fmt&qu ...

  7. 关于Insufficient space for shared memory file解决办法

    发现这个目录使用率100%,但是这个只是逻辑卷,具体是由于/tmp目录下,日志文件太多,导致空间被占满了.

  8. mesos,marathon,haproxy on centos7 最完美安装教程

    前言 本教程参考 http://blog.51cto.com/11863547/1903532 http://blog.51cto.com/11863547/1903532 官方文档等... 系统:c ...

  9. 获取配置文件yml的@ConfigurationProperties和@Value的区别

    首先,配置文件的事,我没讲properties,这个写中文的时候,会有乱码,需要去Idea里面设置一下编码格式为UTF-8 还有,我们的类和配置文件直接关联,我用的是ConfigurationProp ...

  10. BZOJ5261 Rhyme

    传送门 广义后缀自动机= =+ 跟ptx大爷的博客学的 戳我传送 我写的第一种 建立Trie树的写法 bfs建立SAM 为什么是bfs呢 我也不知道(GG) 经过我一番抱大腿+询问 各位大爷说的原因是 ...