Codeforces 1132G Greedy Subsequences [线段树]
看到题解那么少就来发一篇吧……
思路
看完题目一脸懵逼,感觉无从下手。
莫名其妙地想到笛卡尔树,但笛卡尔树好像并没有太大作用。
考虑把笛卡尔树改一下:每个点的父亲设为它的右边第一个大于它的位置。
这时突然发现一个很好的性质:搞答案时每次从右边加入一个点\(x\)时,以\(x\)的子树中的一个点为起点的长度全都加一。
那么按dfs序建线段树维护区间和、区间加、单点修改为-INF(从左边删点)即可。
代码
#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define templ template<typename T>
#define sz 1001010
typedef long long ll;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ inline void read(T& t)
{
t=0;char f=0,ch=getchar();double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
t=(f?-t:t);
}
template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
char __sr[1<<21],__z[20];int __C=-1,__Z=0;
inline void __Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
inline void print(register int x)
{
if (__C>1<<20) __Ot(); if (x<0) __sr[++__C]='-',x=-x;
while (__z[++__Z]=x%10+48,x/=10);
while (__sr[++__C]=__z[__Z],--__Z);__sr[++__C]='\n';
}
void file()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
}
inline void chktime()
{
#ifndef ONLINE_JUDGE
cout<<(clock()-t)/1000.0<<'\n';
#endif
}
#ifdef mod
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
ll inv(ll x){return ksm(x,mod-2);}
#else
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
#endif
// inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;
int n,m;
int a[sz];
struct hh{int t,nxt;}edge[sz<<1];
int head[sz],ecnt;
void make_edge(int f,int t)
{
edge[++ecnt]=(hh){t,head[f]};
head[f]=ecnt;
edge[++ecnt]=(hh){f,head[t]};
head[t]=ecnt;
}
int dfn[sz],low[sz],cnt;
void dfs(int x,int fa)
{
dfn[x]=++cnt;
go(x) if (edge[i].t!=fa) dfs(edge[i].t,x);
low[x]=cnt;
}
void build()
{
stack<int>s;
rep(i,1,n)
{
while (!s.empty()&&a[s.top()]<a[i]) make_edge(s.top(),i),s.pop();
s.push(i);
}
while (!s.empty()) make_edge(s.top(),n+1),s.pop();
}
int mx[sz<<2];
int tag[sz<<2];
#define ls k<<1
#define rs k<<1|1
#define lson ls,l,mid
#define rson rs,mid+1,r
void Add(int k,int w){mx[k]+=w;tag[k]+=w;}
void pushdown(int k){Add(ls,tag[k]);Add(rs,tag[k]);tag[k]=0;}
void pushup(int k){mx[k]=max(mx[ls],mx[rs]);}
void add(int k,int l,int r,int x,int y)
{
if (x<=l&&r<=y) return Add(k,1);
int mid=(l+r)>>1;
pushdown(k);
if (x<=mid) add(lson,x,y);
if (y>mid) add(rson,x,y);
pushup(k);
}
void modify(int k,int l,int r,int x)
{
if (l==r) return void(mx[k]=-1e9);
int mid=(l+r)>>1;
pushdown(k);
if (x<=mid) modify(lson,x);
else modify(rson,x);
pushup(k);
}
int main()
{
file();
read(n,m);
rep(i,1,n) read(a[i]);
build();
dfs(n+1,0);
rep(i,1,n) --dfn[i],--low[i];
rep(i,1,n)
{
add(1,1,n,dfn[i],low[i]);
if (i>m) modify(1,1,n,dfn[i-m]);
if (i>=m) printf("%d ",mx[1]);
}
return 0;
}
Codeforces 1132G Greedy Subsequences [线段树]的更多相关文章
- [Codeforces1132G]Greedy Subsequences——线段树+单调栈
题目链接: Codeforces1132G 题目大意:给定一个序列$a$,定义它的最长贪心严格上升子序列为$b$满足若$a_{i}$在$b$中则$a_{i}$之后第一个比它大的也在$b$中.给出一个数 ...
- Codeforces 1132G(dfs序+线段树)
题面 传送门 分析 对于每一个数a[i],找到它后面第一个大于它的数a[p],由p向i连边,最终我们就会得到一个森林,且p是i的父亲.为了方便操作,我们再增加一个虚拟节点n+1,把森林变成树. 由于序 ...
- cf1132G. Greedy Subsequences(线段树)
题意 题目链接 Sol 昨天没想到真是有点可惜了. 我们考虑每个点作为最大值的贡献,首先预处理出每个位置\(i\)左边第一个比他大的数\(l\),显然\([l + 1, i]\)内的数的后继要么是\( ...
- Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- [Codeforces 1199D]Welfare State(线段树)
[Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...
- [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
- Codeforces 1132G(关系转化树+dfn+线段树)
要点 显然要滑动修改维护. 像通常的数列next关系一样建边(单调栈预处理),因为贪心所以是树,然后发现增删只会影响区间内的子(or父,看你连边方向行事)节点,于是使用dfs序建线段树. 为了正确地修 ...
随机推荐
- 数据库设计理论与实践·<五>常见疑难杂症
- Linux之增加系统调用[内核编译]
声明:如需引用或者摘抄本博文源码或者其文章的,请在显著处注明,来源于本博文/作者,以示尊重劳动成果,助力开源精神.也欢迎大家一起探讨,交流,以共同进步- 0.0 由于操作系统实验的缘故,有一个实验需要 ...
- HDU-1018 BigNumber(斯特林近似)
题目链接 斯特林近似求数位长度经典题,更新板子顺手切了 #include <cstdio> #include <cmath> #include <cstring> ...
- python之第三方模块安装
1. 直接打开cmd窗口运行 pip install xxx #可联网情况下使用,联网下载 xxx表示要安装的模块名称 pip问题及解决方法: 1. 配置环境变量,将如下两个路径都加到系统path ...
- Django REST Framework API Guide 02
本节大纲 1.Generic Views 2.ViewSets 1.Generic Views CBV的主要的一个优点就是极大的允许了对于代码的从用.自然,rest framework取其优势,提供 ...
- python web cgi
知识详解: cgi:通用网关接口,网络脚本的解析 python cgi 自带有cgi轻量级服务器,我们通过cgi命令可以开启该服务器 python2 python -m CGIHTTPServer p ...
- compileSdkVersion,minSdkVersion 和 targetSdkVersion
compileSdkVersion(Eclipse中叫做build target) 1.在eclipse中位于项目根目录中的project.properties文件中 2.在studio中位于项目中的 ...
- java final、finally、finalize
- ListView嵌套GridView,显示不全解决办法
ListView嵌套GridView时,遇到了GridView只显示一行,其余都显示不出来的问题,最终解决办法如下: 需要自定义GridView,重新绘制高度即可: public class MyGr ...
- 使用SQL*Plus连接数据库
About SQL*Plus SQL*Plus is the primary command-line interface to your Oracle database. You use SQL*P ...