-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlis.cpp
More file actions
32 lines (32 loc) · 738 Bytes
/
lis.cpp
File metadata and controls
32 lines (32 loc) · 738 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
template<class cmp> struct LS{
vector<int> v;
vector<pair<int, int> > his;
void add(int x){
int p = upper_bound(v.begin(), v.end(), x, cmp()) - v.begin();
if(p == v.size()){
his.push_back({-1, 0});
v.push_back(x);
}
else{
his.push_back({p, v[p]});
v[p] = x;
}
}
void swap(LS<cmp> &od){ od.v.swap(v); }
void merge(LS<cmp> &od){
if(od.v.size() > v.size())
v.swap(od.v);
for(int i = 0; i < od.v.size(); i++)
if(cmp()(od.v[i], v[i])) v[i] = od.v[i];
}
void undo(){
assert(his.size());
if(his.back().first == -1)
v.pop_back();
else
v[his.back().first] = his.back().second;
his.pop_back();
}
};
typedef LS<less<int> > Lis;
typedef LS<greater<int> > Lds;