Solution -「CF 1132G」Greedy Subsequences
\(\mathcal{Description}\)
Link.
定义 \(\{a\}\) 最长贪心严格上升子序列(LGIS) \(\{b\}\) 为满足以下两点的最长序列:
- \(\{b\}\) 是 \(\{a\}\) 的子序列。
- \(\{b\}\) 中任意相邻两项对应 \(\{a\}\) 中 \(a_i,a_j\),则 \(a_i<a_j\) 且不存在 \(i<k<j\),s.t. \(a_i<a_k\)。
求给定序列 \(\{a_n\}\) 的所有长度为 \(k\) 的子区间 LGIS 长度之和。
\(1\le k\le n\le10^6\)。
\(\mathcal{Solution}\)
很套路地建立树模型,对于 \(i\),连向最小地使得 \(a_i<a_j\) 的 \(j\),那么 \(n\) 个结点构成一片森林。再根据 LGIS 的定义,一个结点若存在于区间,则以其子树内任意一点开头的 LGIS 的长度都会 \(+1\)。故只需要在 DFN 上维护线段树即可动态更新每个区间的答案。
还有呢,联想到这道题,令 \(i\) 的 DFN 为 \(n-i+1\) 即可,树并不需要建出来 owo!
\(\mathcal{Code}\)
/* Clearink */
#include <cstdio>
inline int rint () {
int x = 0; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () );
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x;
}
template<typename Tp>
inline void wint ( Tp x ) {
if ( x < 0 ) putchar ( '-' ), x = -x;
if ( 9 < x ) wint ( x / 10 );
putchar ( x % 10 ^ '0' );
}
inline int imax ( const int a, const int b ) { return a < b ? b : a; }
const int MAXN = 1e6;
int n, m, a[MAXN + 5], dfn[MAXN + 5];
int top, stk[MAXN + 5], siz[MAXN + 5];
struct SegmentTree {
int mx[MAXN << 2], tag[MAXN << 2];
inline void pushdn ( const int rt ) {
int& t = tag[rt];
if ( !t ) return ;
mx[rt << 1] += t, tag[rt << 1] += t;
mx[rt << 1 | 1] += t, tag[rt << 1 | 1] += t;
t = 0;
}
inline void pushup ( const int rt ) {
mx[rt] = imax ( mx[rt << 1], mx[rt << 1 | 1] );
}
inline void add ( const int rt, const int l, const int r,
const int al, const int ar ) {
if ( al <= l && r <= ar ) return ++mx[rt], ++tag[rt], void ();
int mid = l + r >> 1; pushdn ( rt );
if ( al <= mid ) add ( rt << 1, l, mid, al, ar );
if ( mid < ar ) add ( rt << 1 | 1, mid + 1, r, al, ar );
pushup ( rt );
}
inline int qmax ( const int rt, const int l, const int r,
const int ql, const int qr ) {
if ( ql <= l && r <= qr ) return mx[rt];
int mid = l + r >> 1, ret = 0; pushdn ( rt );
if ( ql <= mid ) ret = imax ( ret, qmax ( rt << 1, l, mid, ql, qr ) );
if ( mid < qr ) ret = imax ( ret, qmax ( rt << 1 | 1, mid + 1, r, ql, qr ) );
return ret;
}
} sgt;
int main () {
n = rint (), m = rint ();
for ( int i = 1; i <= n; ++i ) a[i] = rint (), dfn[i] = n - i + 1;
for ( int i = 1; i <= n; ++i ) {
for ( siz[i] = 1; top && a[stk[top]] < a[i]; siz[i] += siz[stk[top--]] );
stk[++top] = i;
}
for ( int i = 1; i < m; ++i ) sgt.add ( 1, 1, n, dfn[i], dfn[i] + siz[i] - 1 );
for ( int i = m; i <= n; ++i ) {
sgt.add ( 1, 1, n, dfn[i], dfn[i] + siz[i] - 1 );
wint ( sgt.qmax ( 1, 1, n, dfn[i], dfn[i - m + 1] ) );
putchar ( i ^ n ? ' ' : '\n' );
}
return 0;
}
Solution -「CF 1132G」Greedy Subsequences的更多相关文章
- Solution -「CF 1342E」Placing Rooks
\(\mathcal{Description}\) Link. 在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...
- Solution -「CF 1622F」Quadratic Set
\(\mathscr{Description}\) Link. 求 \(S\subseteq\{1,2,\dots,n\}\),使得 \(\prod_{i\in S}i\) 是完全平方数,并最 ...
- Solution -「CF 923F」Public Service
\(\mathscr{Description}\) Link. 给定两棵含 \(n\) 个结点的树 \(T_1=(V_1,E_1),T_2=(V_2,E_2)\),求一个双射 \(\varph ...
- Solution -「CF 923E」Perpetual Subtraction
\(\mathcal{Description}\) Link. 有一个整数 \(x\in[0,n]\),初始时以 \(p_i\) 的概率取值 \(i\).进行 \(m\) 轮变换,每次均匀随机 ...
- Solution -「CF 1586F」Defender of Childhood Dreams
\(\mathcal{Description}\) Link. 定义有向图 \(G=(V,E)\),\(|V|=n\),\(\lang u,v\rang \in E \Leftrightarr ...
- Solution -「CF 1237E」Balanced Binary Search Trees
\(\mathcal{Description}\) Link. 定义棵点权为 \(1\sim n\) 的二叉搜索树 \(T\) 是 好树,当且仅当: 除去最深的所有叶子后,\(T\) 是满的: ...
- Solution -「CF 623E」Transforming Sequence
题目 题意简述 link. 有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...
- Solution -「CF 1023F」Mobile Phone Network
\(\mathcal{Description}\) Link. 有一个 \(n\) 个结点的图,并给定 \(m_1\) 条无向带权黑边,\(m_2\) 条无向无权白边.你需要为每条白边指定边权 ...
- Solution -「CF 599E」Sandy and Nuts
\(\mathcal{Description}\) Link. 指定一棵大小为 \(n\),以 \(1\) 为根的有根树的 \(m\) 对邻接关系与 \(q\) 组 \(\text{LCA}\ ...
随机推荐
- git branch --set-upstream-to 本地关联远程分支
最近使用git pull的时候多次碰见下面的情况: There is no tracking information for the current branch. Please specify wh ...
- 403 Invalid CORS request 跨域问题 invalid+cors+request什么意思
5.跨域问题 跨域:浏览器对于javascript的同源策略的限制 . 以下情况都属于跨域: 跨域原因说明 示例 域名不同 www.jd.com 与 www.taobao.com 域名相同,端口不同 ...
- 【Java】Eclipse常用快捷键
Eclipse常用快捷键 * 1.补全代码的声明:alt + / * 2.快速修复: ctrl + 1 * 3.批量导包:ctrl + shift + o * 4.使用单行注释:ctrl + / * ...
- .NET 云原生架构师训练营(权限系统 RGCA 架构设计)--学习笔记
目录 项目核心内容 实战目标 RGCA 四步架构法 项目核心内容 无代码埋点实现对所有 API Action 访问控制管理 对 EF Core 实体新增.删除.字段级读写控制管理 与 Identity ...
- http 的get 与 post 的区别
1.原理区别 一般在浏览器中输入网址访问资源都是通过GET方式:在FORM提交中,可以通过Method指定提交方式为GET或者POST,默认为GET提交 Http定义了与服务器交互的不同方法,最基本的 ...
- 访问者模式(Visitor模式)
模式的定义与特点 访问者(Visitor)模式的定义:将作用于某种数据结构中的各元素的操作分离出来封装成独立的类,使其在不改变数据结构的前提下可以添加作用于这些元素的新的操作,为数据结构中的每个元素提 ...
- Django db使用MySQL连接池
Django db使用MySQL连接池 Sep 25 2016 Django db模块本身不支持MySQL连接池,只有一个配置CONN_MAX_AGE连接最大存活时间,如果WSGI服务器使用了线程池技 ...
- golang中的配置管理库viper
viper简介 Viper是适用于Go应用程序的完整配置解决方案.它旨在在应用程序中工作,并且可以处理所有类型的配置需求和格式.它支持: 设置默认值 从JSON,TOML,YAML,HCL,envfi ...
- vue中清除路由缓存
beforeRouteLeave (to, from, next) { if (to.name === 'pageA') { /* pageA是需要跳转的路由 */ // console.log('返 ...
- 如何使用iconfont的CDN
如何使用iconfont的CDN iconfont作为阿里的图标库,在开发过成功用的已经是非常广泛了,但iconfont并不需要将图标下载后使用,而是可以直接用cdn引入使用,至于使用流程,请看下文. ...