JZOJ 4920 降雷皇(最长上升子序列、线段树)

本文介绍了一种使用线段树解决最长上升子序列问题的方法,包括最大长度及方案数量的计算。通过线段树维护子序列状态,实现高效查询与更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意

给出一个序列a,求最长上升子序列及其方案数。

n<=100000,a[i]<=100000
时间限制 1s
空间限制 256M

解题思路

当做到第i位时,线段树上的第x位表示1~i-1中,结尾为x的子序列能取到的最大答案。
每次在线段树上查询0~a[i]-1的最大答案及其方案数。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100006
#define fr(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const ll ding=123456789;

int i,n,typ,x,mx,s1,ans1,tr[maxn*4];
ll s2,ans2,c[maxn*4];
void modify(int v,int st,int en,int x,int y,ll z)
{
    if (st==en)
    {
        if (y>tr[v]) tr[v]=y,c[v]=z;
        else if (y==tr[v]) c[v]=(c[v]+z)%ding;
        return;
    }
    int m=(st+en) >> 1;
    if (x<=m) modify(v+v,st,m,x,y,z);
    else modify(v+v+1,m+1,en,x,y,z);
    if (tr[v+v]>tr[v+v+1]) tr[v]=tr[v+v],c[v]=c[v+v];
    else if (tr[v+v]<tr[v+v+1]) tr[v]=tr[v+v+1],c[v]=c[v+v+1];
    else if (tr[v+v]==tr[v+v+1]) tr[v]=tr[v+v],c[v]=(c[v+v]+c[v+v+1])%ding;
    return;
}
void findd(int v,int st,int en,int l,int r)
{
    if (st==l && en==r)
    {
        if (tr[v]>s1) s1=tr[v],s2=c[v];
        else if (tr[v]==s1) s2=(s2+c[v])%ding;
        return;
    }
    int m=(st+en) >> 1;
    if (r<=m) findd(v+v,st,m,l,r);
    else if (l>m) findd(v+v+1,m+1,en,l,r);
    else
    {
        findd(v+v,st,m,l,m);
        findd(v+v+1,m+1,en,m+1,r);
    }
    return;
}
int main()
{
    freopen("hamon.in","r",stdin);
    freopen("hamon.out","w",stdout);
    scanf("%d%d",&n,&typ);
    mx=100000;
    fr(i,1,mx*4) tr[i]=-1 << 30,c[i]=0;
    modify(1,0,mx,0,0,1);
    fr(i,1,n)
    {
        scanf("%d",&x);
        s1=s2=0;
        findd(1,0,mx,0,x-1);
        modify(1,0,mx,x,s1+1,s2);
        if (s1+1==ans1) ans2=(ans2+s2)%ding;
        else if (s1+1>ans1) 
            ans1=s1+1,ans2=s2;
    }
    printf("%d\n",ans1);
    if (typ==1) printf("%lld\n",ans2);
    return 0;
}
### 线段树优化最长上升子序列(LIS)的算法实现 最长上升子序列(Longest Increasing Subsequence, LIS)问题是动态规划中的经典问题,其朴素解法的时间复杂度为 $O(n^2)$。当数据规模较大时,这种解法效率较低。通过线段树或树状数组优化,可以将时间复杂度低到 $O(n \log n)$,从而更高效地处理大规模数据[^2]。 #### 1. 核心思想 线段树优化 LIS 的核心思想是利用线段树维护以不同值结尾的最长上升子序列长度。在遍历数组的过程中,对于当前元素 $x$,查询线段树中所有小于 $x$ 的值所对应的最长子序列长度,然后将该值加一作为当前值的 LIS 长度,并更新线段树对应位置的值。 该方法的查询和更新操作均可以在 $O(\log n)$ 时间内完成,因此整体时间复杂度为 $O(n \log n)$。 #### 2. 线段树结构设计 线段树的每个节点表示一个值域区间,存储该区间内以任意值结尾的最长上升子序列长度。线段树支持以下操作: - **区间最大值查询**:查询小于当前值的所有元素的最长子序列长度。 - **单点更新**:将当前值对应的位置更新为新的最长子序列长度。 #### 3. 实现步骤 1. **离散化处理**:由于元素值可能较大,需要对原始数组进行离散化处理,将其映射到一个较小的值域范围内。 2. **线段树构建**:初始化线段树,所有节点初始值为 0。 3. **遍历数组**:对每个元素 $x$,执行以下操作: - 查询线段树中 $[1, x-1]$ 区间的最大值。 - 当前最长上升子序列长度为查询结果加一。 - 更新线段树中 $x$ 位置的值为当前最长子序列长度。 4. **最终结果**:线段树根节点的值即为最长上升子序列的长度。 #### 4. 代码实现 以下是一个基于线段树优化的最长上升子序列算法实现: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; struct SegmentTree { vector<int> tree; int size; SegmentTree(int n) { size = 1; while (size < n) size <<= 1; tree.assign(2 * size, 0); } void update(int pos, int value) { pos += size; tree[pos] = max(tree[pos], value); while (pos > 1) { pos >>= 1; tree[pos] = max(tree[2 * pos], tree[2 * pos + 1]); } } int query(int l, int r) { int res = 0; for (l += size, r += size; l <= r; l >>= 1, r >>= 1) { if (l % 2 == 1) res = max(res, tree[l++]); if (r % 2 == 0) res = max(res, tree[r--]); } return res; } }; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; // 离散化 vector<int> sorted = a; sort(sorted.begin(), sorted.end()); for (int i = 0; i < n; ++i) { a[i] = lower_bound(sorted.begin(), sorted.end(), a[i]) - sorted.begin() + 1; } SegmentTree st(n); for (int i = 0; i < n; ++i) { int max_len = st.query(1, a[i] - 1); // 查询小于当前值的最大长度 st.update(a[i], max_len + 1); // 更新当前值对应的最长子序列长度 } cout << st.tree[1] << endl; // 输出最长上升子序列长度 return 0; } ``` #### 5. 性能分析 - **时间复杂度**:离散化需要 $O(n \log n)$,线段树的每次查询和更新操作为 $O(\log n)$,因此总时间复杂度为 $O(n \log n)$。 - **空间复杂度**:线段树的空间复杂度为 $O(n)$,其中 $n$ 是离散化后的值域大小。 #### 6. 应用场景 线段树优化的 LIS 算法适用于以下场景: - **大规模数据处理**:如金融数据分析、基因序列分析等,数据规模可能达到 $10^5$ 或更高。 - **实时系统**:需要快速响应的场景,例如实时推荐系统中的最长上升序列计算。 - **在线算法**:适用于数据流处理,可以在数据到来时动态更新最长上升子序列长度。 #### 7. 优化与扩展 - **支持重复元素**:可以修改线段树查询条件,使其支持非严格递增子序列。 - **多维扩展**:可将线段树扩展为二维线段树,处理二维最长上升子序列问题。 - **结合树状数组**:在某些情况下,使用树状数组实现相同功能会更简洁高效,尤其是当只需要单点更新和前缀最大值查询时[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值