Files
python/TangDou/AcWing/LIS/896_FenWickTree_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

45 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) (x & -x)
const int N = 1e5 + 10;
int n, a[N];
int b[N], bl; //离散化数组,用于辅助树状数组
int tr[N]; //树状数组
int res; //结果
//计算x值在原序列中的排名
int get(int x) {
return lower_bound(b, b + bl, x) - b + 1;
}
//单点更新x
void update(int i, int x) {
for (; i <= bl; i += lowbit(i)) tr[i] = max(tr[i], x);
}
//求tr[1]~tr[i]的最大值
int query(int i) {
int s = 0;
for (; i; i -= lowbit(i)) s = max(s, tr[i]);
return s;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]), b[i] = a[i];
//离散化,用于获取 x值->排名k 的关系
sort(b, b + n);
// bl:去重后的长度
bl = unique(b, b + n) - b;
for (int i = 0; i < n; i++) {
int k = get(a[i]); //值a[i]的排名k
int t = query(k - 1) + 1;
update(k, t); //第k大的数,会接在第k-1大的数后面,才会获取到更大的连续LIS值
}
//输出
printf("%d\n", query(bl));
return 0;
}