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}\ ...
随机推荐
- 第10组 Beta冲刺 (2/5)
1.1基本情况 ·队名:今晚不睡觉 ·组长博客:https://www.cnblogs.com/cpandbb/p/14015412.html ·作业博客:https://edu.cnblogs.co ...
- 在pyqt5中展示pyecharts生成的图像
技术背景 虽然现在很少有人用python去做一些图形化的界面,但是不得不说我们在日常大部分的软件使用中都还是有可视化与交互这样的需求的.因此pyqt5作为一个主流的python的GUI框架地位是非常重 ...
- Android官方文档翻译 十 2.3Styling the Action Bar
Styling the Action Bar 设计菜单栏的样式 This lesson teaches you to 这节课教给你 Use an Android Theme 使用一个Android主题 ...
- 【pwn】学pwn日记——栈学习(持续更新)
[pwn]学pwn日记--栈学习(持续更新) 前言 从8.2开始系统性学习pwn,在此之前,学习了部分汇编指令以及32位c语言程序的堆栈图及函数调用. 学习视频链接:XMCVE 2020 CTF Pw ...
- 触发器中获取sql
CREATE trigger 触发器名 on 表名 for update,delete as set nocount on create table #t(EvebtType varchar(60), ...
- 读书笔记http之第一章
http TCP/IP协议各层: 应用层 决定了向用户提供应用服务时通信的活动. 比如 : FTP(FileTransferProtocol,文件传输协议)和DNS(DomainNameSystem, ...
- vue-cli创建的项目打包成app引入字体图标的问题
将项目在手机端调试时,发现引入的阿里图标显示不出来,需要在引入的iconfont.css文件夹里给url加上https
- vue 快速入门 系列 —— 侦测数据的变化 - [vue api 原理]
其他章节请看: vue 快速入门 系列 侦测数据的变化 - [vue api 原理] 前面(侦测数据的变化 - [基本实现])我们已经介绍了新增属性无法被侦测到,以及通过 delete 删除数据也不会 ...
- Windows如何搭建SSL通信(非Web)
自己研究了会儿,把结论发出来给有需要的人 第一步:准备环境 首先需要一台服务器(这不是废话吗),我这边用的windows2003, 还需要一台客户端,我用的是windwos2008 第二步:服务器环境 ...
- numpy常用函数记录
np.square() 函数返回一个新数组,该数组的元素值为源数组元素的平方. 源阵列保持不变. 示例: import numpy as np a = np.array([[1, 2, 3], [4, ...