Cod sursa(job #3307332)

Utilizator EricDimiCismaru Eric-Dimitrie EricDimi Data 20 august 2025 12:07:44
Problema Subsir crescator maximal Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <bits/stdc++.h>
 
using namespace std;
 
ifstream fin("scmax.in");
ofstream fout("scmax.out");
 
int max(int x, int y) {
	return x > y ? x : y;
}
 
int next_power_2(int n) {
	return (1 << (32 - __builtin_clz(n - 1)));
}
 
struct SegmentTree {
	vector<int> tree;
	int TREE_SIZE;
	
	SegmentTree(int n) {
		TREE_SIZE = next_power_2(n);
		tree = vector<int> (TREE_SIZE << 1, 0);
	}
	
	void update(int pos, int val) {
		pos += TREE_SIZE;
		tree[pos] = max(tree[pos], val);
		while (pos > 1) {
			pos >>= 1;
			tree[pos] = max(tree[pos << 1], tree[pos << 1 | 1]);
		}
	}
	
	int query(int left, int right) {
		int res = 0;
		left += TREE_SIZE;
		right += TREE_SIZE;
		while (left <= right) {
			if (left & 1)
				res = max(res, tree[left++]);
			if (!(right & 1))
				res = max(res, tree[right--]);
			left >>= 1;
			right >>= 1;
		}
		return res;
	}
};
 
int main() {
	int n;
	fin >> n;
	
	vector<int> x(n);
	vector<int> aux(n);
	for (int i = 0; i < n; i++) {
		fin >> x[i];
		aux[i] = x[i];
	}
	
	sort(aux.begin(), aux.end());
	aux.erase(unique(aux.begin(), aux.end()), aux.end());
	
	auto idx = [&](int val) {
		return lower_bound(aux.begin(), aux.end(), val) - aux.begin();
	};
	
	for (int i = 0; i < n; i++)
		x[i] = idx(x[i]);
	
	SegmentTree st(n);
	int res = 0, len;
	
	for (int i = 0; i < n; i++) {
		len = st.query(0, x[i] - 1) + 1;
		st.update(x[i], len);
		res = max(res, len);
	}
	
	fout << res << "\n";
	
	
	fin.close();
	fout.close();
	return 0;
}