当前位置: 首页 > news >正文

ABC393F

比较简单的 LIS 模板题。

回顾使用树状数组求解 LIS 问题的过程:用树状数组的下标表示 \(a\) 的值域,存储 \(f\) 的最大值。求解时在 \([1,a_i-1]\) 上取最大值,并更新到 \(a_i\) 对应的位置。

类似地,在本题中,询问前 \(R\) 个数且值不超过 \(X\) 的 LIS 长度,发现正好能用树状数组存储的信息解决。具体地,先把所有询问离线下来,并对 \(a\) 离散化,从前往后求 LIS。假设当前到了第 \(i\) 位,那么先用当前 \(a_i\) 更新树状数组,再解决所有 \(R=i\) 的询问,答案即为 \([1,X]\) 上的最大值(只需保证最大的一项即最后一项不超过 \(X\) 即可)。最后再一遍输出答案即可。

总时间复杂度 \(O( ( n + m )\log n)\)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>using namespace std;struct Q
{int x,id;
};vector<Q> q[1000001];
int n,m,ans[1000001],a[1000001];
int f[1000001],tp;
int ls[1000001],tot;
int t[1000001];int lowbit( int x )
{return x & ( -x );
}void upd( int x , int y )
{for( int i = x ; i <= tot ; i += lowbit( i ) )t[i] = max( t[i] , y );return;
}int que( int x )
{int mx = 0;for( int i = x ; i >= 1 ; i -= lowbit( i ) )mx = max( mx , t[i] );return mx;
}bool cmp( Q x , Q y )
{return x.x < y.x;
}int main()
{int r,x;cin >> n >> m;for( int i = 1 ; i <= n ; i ++ )cin >> a[i],ls[++ tot] = a[i];for( int i = 1 ; i <= m ; i ++ ){cin >> r >> x;ls[++ tot] = x;q[r].push_back( { x , i } );}sort( ls + 1 , ls + tot + 1 );tot = unique( ls + 1 , ls + tot + 1 ) - ls - 1;for( int i = 1 ; i <= n ; i ++ ){a[i] = lower_bound( ls + 1 , ls + tot + 1 , a[i] ) - ls;for( auto &p : q[i] )p.x = lower_bound( ls + 1 , ls + tot + 1 , p.x ) - ls;}for( int i = 1 ; i <= n ; i ++ )sort( q[i].begin() , q[i].end() , cmp );for( int i = 1 ; i <= n ; i ++ ){	upd( a[i] , que( a[i] - 1 ) + 1 );for( auto p : q[i] )ans[p.id] = que( p.x );}for( int i = 1 ; i <= m ; i ++)cout << ans[i] << '\n';return 0;
}
http://www.agseo.cn/news/277/

相关文章:

  • ABC393E
  • ABC393D
  • ZR 25 noip D1T2 题解 | 最短路
  • NOIP2024 退役记
  • LG11311
  • CF1746F
  • ABC389F
  • LG10641
  • P11068
  • scp拷贝文件报错
  • ABC150 C-F
  • 【游戏设计】五子棋设计思路
  • LG10516
  • 11.1 定义类和对象
  • C#.NET EFCore.BulkExtensions 扩展详解
  • 2025AI赋能HR新纪元,中国AI HR主流厂商大盘点
  • Linux作业及状态转换
  • C++小白修仙记_LeetCode刷题_队列
  • 设备驱动程序和设备独立性软件的区别
  • Fastjson 1.2.47 远程代码执行
  • 树状数组板子
  • 私有化部署Dify构建企业AI平台教程
  • 树状数组板子2
  • 网络流——OI复健
  • 2025“钉耙编程”中国大学生算法设计暑期联赛(3)
  • Symfony学习笔记 - Symfony Documentation - Getting Started(下)
  • MySQL事务
  • 线段树板子
  • 双列圆锥滚子轴承载荷分布计算程序
  • NOIP 集训日记