Add Projects problem
Okay, this one was tough, segtree with dp? At least it's pretty similar to LIS with dp...
This commit is contained in:
parent
2930b4452f
commit
368389b596
160
CSES Problem Set/Projects.cpp
Normal file
160
CSES Problem Set/Projects.cpp
Normal file
@ -0,0 +1,160 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1140 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
class segtree {
|
||||
vl seg;
|
||||
|
||||
ll _query(int i, int l, int r, int ql, int qr) {
|
||||
if (l > qr || r < ql) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (l >= ql && r <= qr) {
|
||||
return seg[i];
|
||||
}
|
||||
|
||||
int mid = (l + r) / 2;
|
||||
return max(_query(i * 2, l, mid, ql, qr), _query(i * 2 + 1, mid + 1, r, ql, qr));
|
||||
}
|
||||
|
||||
public:
|
||||
segtree() = delete;
|
||||
segtree(vl &items): seg(items.size() * 2) {
|
||||
size_t n = items.size();
|
||||
for (size_t i = n; i < n * 2; i++) {
|
||||
seg[i] = items[i - n];
|
||||
}
|
||||
|
||||
for (size_t i = n - 1; i > 0; i--) {
|
||||
seg[i] = max(seg[i * 2], seg[i * 2 + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
void update(ll v, int i) {
|
||||
i += seg.size() / 2;
|
||||
if (seg[i] >= v) {
|
||||
return;
|
||||
}
|
||||
seg[i] = v;
|
||||
i /= 2;
|
||||
for (; i > 0; i /= 2) {
|
||||
seg[i] = max(seg[i * 2], seg[i * 2 + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
ll query(int i) {
|
||||
return _query(1, 0, seg.size() / 2 - 1, 0, i);
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
V<tuple<ll, ll, ll>> fds(n);
|
||||
set<ll> lol;
|
||||
for (auto &[i, e, v] : fds) {
|
||||
cin >> i >> e >> v;
|
||||
lol.insert(e);
|
||||
}
|
||||
|
||||
pair<vl, vl> ends(vl(lol.size(), 0), vl(lol.size()));
|
||||
auto itr = lol.begin();
|
||||
for (size_t i = 0; itr != lol.end(); i++, itr++) {
|
||||
ends.second[i] = *itr;
|
||||
}
|
||||
|
||||
sort(fds.begin(), fds.end());
|
||||
|
||||
ll neutral = 1e9 + 1;
|
||||
while (__builtin_popcount(ends.second.size()) != 1) {
|
||||
ends.first.push_back(-1);
|
||||
ends.second.push_back(neutral++);
|
||||
}
|
||||
|
||||
segtree seg(ends.first);
|
||||
|
||||
for (auto [i, f, v] : fds) {
|
||||
auto itr = lower_bound(ends.second.begin(), ends.second.end(), i);
|
||||
if (itr == ends.second.begin()) {
|
||||
seg.update(v, lower_bound(ends.second.begin(), ends.second.end(), f) - ends.second.begin());
|
||||
continue;
|
||||
}
|
||||
|
||||
ll up = seg.query(itr - ends.second.begin() - 1) + v;
|
||||
seg.update(up, lower_bound(ends.second.begin(), ends.second.end(), f) - ends.second.begin());
|
||||
}
|
||||
|
||||
cout << seg.query(ends.first.size() - 1) << '\n';
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user