// clang-format off
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(auto i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";}
#ifdef DEBUG
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<"\n";}(X)
#else
#define debug(...) {}
#endif
// clang-format on
class RTree
{
int base = 1;
vector<LL> data;
public:
RTree(int n)
{
while (base < n) base <<= 1;
data.resize(base << 1);
}
void set_atp(int v, LL x)
{
for (v += base; v; v >>= 1) data[v] = max(data[v], x);
}
LL max_onr(int l, int r)
{
LL res = 0;
for (l += base, r += base; l <= r; l >>= 1, r >>= 1) {
if (l & 1) res = max(res, data[l++]);
if (!(r & 1)) res = max(res, data[r--]);
}
return res;
}
};
const int MAX_TYPES = 500000;
int
main()
{
cin.tie(0)->sync_with_stdio(0);
LL N, penalty;
cin >> N >> penalty;
vector<pair<int, int>> seq(N);
for (auto& x : seq) cin >> x.first >> x.second;
debug(seq);
RTree tree(MAX_TYPES + 1);
vector<pair<int, LL>> batch;
REP (n, N) {
const auto& [val, type] = seq[n];
auto keep_type = tree.max_onr(type, type) + val;
auto change_type
= max(tree.max_onr(1, type - 1), tree.max_onr(type + 1, MAX_TYPES))
+ val - penalty;
batch.emplace_back(type, max(keep_type, change_type));
// Apply batch
if (n + 1 == N || seq[n + 1].first != seq[n].first) {
for (const auto& b : batch) tree.set_atp(b.first, b.second);
batch.clear();
}
}
cout << tree.max_onr(1, MAX_TYPES) << "\n";
return 0;
}
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | // clang-format off #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(auto i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";} #ifdef DEBUG #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<"\n";}(X) #else #define debug(...) {} #endif // clang-format on class RTree { int base = 1; vector<LL> data; public: RTree(int n) { while (base < n) base <<= 1; data.resize(base << 1); } void set_atp(int v, LL x) { for (v += base; v; v >>= 1) data[v] = max(data[v], x); } LL max_onr(int l, int r) { LL res = 0; for (l += base, r += base; l <= r; l >>= 1, r >>= 1) { if (l & 1) res = max(res, data[l++]); if (!(r & 1)) res = max(res, data[r--]); } return res; } }; const int MAX_TYPES = 500000; int main() { cin.tie(0)->sync_with_stdio(0); LL N, penalty; cin >> N >> penalty; vector<pair<int, int>> seq(N); for (auto& x : seq) cin >> x.first >> x.second; debug(seq); RTree tree(MAX_TYPES + 1); vector<pair<int, LL>> batch; REP (n, N) { const auto& [val, type] = seq[n]; auto keep_type = tree.max_onr(type, type) + val; auto change_type = max(tree.max_onr(1, type - 1), tree.max_onr(type + 1, MAX_TYPES)) + val - penalty; batch.emplace_back(type, max(keep_type, change_type)); // Apply batch if (n + 1 == N || seq[n + 1].first != seq[n].first) { for (const auto& b : batch) tree.set_atp(b.first, b.second); batch.clear(); } } cout << tree.max_onr(1, MAX_TYPES) << "\n"; return 0; } |
English