Codeforces 1132G(dfs序+线段树)
题面
分析
对于每一个数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序+线段树)的更多相关文章
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- Codeforces 396C (DFS序+线段树)
题面 传送门 题目大意: 给定一棵树,每个点都有权值,边的长度均为1,有两种操作 操作1:将节点u的值增加x,并且对于u的子树中的任意一个点v,将它的值增加x-dist(u,v)*k, dist(u, ...
- Codeforces 1110F(DFS序+线段树)
题面 传送门 分析 next_id = 1 id = array of length n filled with -1 visited = array of length n filled with ...
- Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- 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 ...
- 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 ...
- 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 339 Solved: 130[Submit][Status][Discuss] D ...
- BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)
题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...
随机推荐
- php开启xdebug扩展及xdebug通信原理
xdebug调试原理 IDE(如PHPStorm)已经集成了一个遵循BGDP的XDebug插件,当开启它的时候, 会在本地开一个XDebug调试服务,监听在调试器中所设置的端口上,默认是9000,这个 ...
- axios 如何获取下载文件的进度条
exportFun(){ let _that = this const instance = this.axios.create({ onDownl ...
- smbsh - 允许用UNIX命令访问NT文件系统
总览 smbsh 描述 此程序是Samba套件的一部分. smbsh允许你用UNIX命令诸如ls,egrep和rcp等来访问NT文件系统.必须用动态链接的shell以便使smbsh工作正常. 从命令提 ...
- demo board boot mode
demo扩展板 QSPI0_IO0_MIO2--A13--PS-MIO2 QSPI0_IO0_MIO3--A14--PS-MIO3 QSPI0_IO0_MIO4--B11--PS-MIO4 QSPI0 ...
- 【串线篇】Mybatis缓存之一级缓存
1.体会 一级缓存:MyBatis:SqlSesion级别的缓存:默认存在,不需要设置. 机制:只要之前查询过的数据,mybatis就会保存在一个缓存中(Map):下次获取直接从缓存中拿:当前sess ...
- python基础:2.二进制
1.二进制:计算机存储0,1的一种方式,规则是逢2进1. 一个数字在计算机存储的是一个字节,即8个bit,每个bit要么存储0,要么存储1. 0000 0000 (二进制)表示 0(十进制), 000 ...
- 排查Java高CPU占用原因
近期java应用,CPU使用率一直很高,经常达到100%,通过以下步骤完美解决,分享一下. 方法一: 转载:http://www.linuxhot.com/java-cpu-used-high.htm ...
- 人生苦短_我用Python_logging日志操作_011
话不多说,开搞,基础是先使用自带loggin模块,level为warning以上, 进一步是自定义logger,level可自定义 #!/usr/bin/env python # -*- coding ...
- 【QUIC】Quick UDP Internet Connections
QUIC(Quick UDP Internet Connections,快速UDP互联网连接)是Google提出的一种基于UDP改进的通信协议,其目的是降低网络通信的延迟,提供更好的用户互动体验. Q ...
- Yii2 kineditor
用 kineditor实现异步 <table cellspadding=5 width=400> <tr height='150'> <td valign="t ...