//
// Mateusz Pietrowcow
//
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using ii = pair<int,int>;
using uii = pair<uint,uint>;
using pll = pair<ll,ll>;
using pull = pair<ull,ull>;
using ld = long double;
using pld = pair<ld,ld>;
#define int128 __int128_t
#define uint128 __uint128_t
#define pbds __gnu_pbds
#define cxx __gnu_cxx
#define MOD 1000000007
#define MODLL 1000000007LL
#define MODULL 1000000007ULL
// best primes in the galaxy
#define MOD1 1000000007LL
#define MOD2 1000000403LL
#define MOD3 1000000787LL
#define MOD4 1000001447LL
#define INF 1'000'000'000
#define INFLL 1'000'000'000'000'000'000LL
#define INFULL 1'000'000'000'000'000'000ULL
#define timeNow() (chrono::high_resolution_clock::now().time_since_epoch().count())
#define all(x) x.begin(),x.end()
#define len(x) (int)(x.size())
#define lenl(x) (ll)(x.size())
#define inRange(x,a,b) (a <= x && x <= b)
#define fir first
#define sec second
#define self(x,y,z) y = x(y,z)
template <typename X, typename Y, typename Z>
using gp_hashmap = pbds::gp_hash_table<X,Y,Z,equal_to<X>,pbds::direct_mask_range_hashing<>,pbds::linear_probe_fn<>,pbds::hash_standard_resize_policy<pbds::hash_exponential_size_policy<>,pbds::hash_load_check_resize_trigger<>,true>>;
template <typename X, typename Y, typename Z>
using gp_hashmap_safe = pbds::cc_hash_table<X,Y,Z,equal_to<X>,pbds::direct_mod_range_hashing<>,pbds::hash_standard_resize_policy<pbds::hash_prime_size_policy,pbds::hash_load_check_resize_trigger<>,true>>;
template <typename X, typename Y, typename Z>
using cc_hashmap = pbds::cc_hash_table<X,Y,Z,equal_to<X>,pbds::direct_mask_range_hashing<>,pbds::hash_standard_resize_policy<pbds::hash_exponential_size_policy<>,pbds::hash_load_check_resize_trigger<>,true>>;
template <typename X, typename Y, typename Z>
using cc_hashmap_safe = pbds::cc_hash_table<X,Y,Z,equal_to<X>,pbds::direct_mod_range_hashing<>,pbds::hash_standard_resize_policy<pbds::hash_prime_size_policy,pbds::hash_load_check_resize_trigger<>,true>>;
template <typename X, typename Y, typename Z>
using indexed_set = pbds::tree<X, Y, Z, pbds::rb_tree_tag, pbds::tree_order_statistics_node_update>;
struct chash
{
size_t operator()(const int& a) const
{
const static int rng = (int)timeNow() ^ (int)(ull)(make_unique<char>().get());
return a ^ rng;
}
};
namespace tree
{
const int base = 1<<19;
ll tree[base * 2];
void update(int v, ll x)
{
v += base;
if (tree[v] >= x) return;
tree[v] = x;
v /= 2;
while (v != 0)
{
tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
v /= 2;
}
}
ll query(int a, int b)
{
a += base - 1;
b += base + 1;
ll r = 0;
while (a / 2 != b / 2)
{
if (a % 2 == 0) self(max, r, tree[a + 1]);
if (b % 2 == 1) self(max, r, tree[b - 1]);
a /= 2;
b /= 2;
}
return r;
}
}
namespace sol
{
const int N = 5e5+5;
vector<int> t[N];
int n, c, m;
void read()
{
cin >> n >> c;
for (int i = 0; i < n; i++)
{
int a, w;
cin >> a >> w;
t[a].push_back(w);
self(max, m, a);
}
}
void solve()
{
ll best = 0;
for (int i = 1; i <= m; i++)
{
vector<ll> ans;
ans.reserve(len(t[i]));
for (auto j : t[i])
{
ll np = tree::query(j, j),
p = max(tree::query(0, j - 1), tree::query(j + 1, tree::base - 1)) - c;
ans.push_back(max(np, p) + i);
}
for (int j = 0; j < len(ans); j++)
{
self(max, best, ans[j]);
tree::update(t[i][j], ans[j]);
}
}
cout << best << '\n';
}
};
int main()
{
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
sol::read();
sol::solve();
}
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | // // Mateusz Pietrowcow // #include <bits/stdc++.h> #include <bits/extc++.h> using namespace std; using ll = long long; using uint = unsigned int; using ull = unsigned long long; using ii = pair<int,int>; using uii = pair<uint,uint>; using pll = pair<ll,ll>; using pull = pair<ull,ull>; using ld = long double; using pld = pair<ld,ld>; #define int128 __int128_t #define uint128 __uint128_t #define pbds __gnu_pbds #define cxx __gnu_cxx #define MOD 1000000007 #define MODLL 1000000007LL #define MODULL 1000000007ULL // best primes in the galaxy #define MOD1 1000000007LL #define MOD2 1000000403LL #define MOD3 1000000787LL #define MOD4 1000001447LL #define INF 1'000'000'000 #define INFLL 1'000'000'000'000'000'000LL #define INFULL 1'000'000'000'000'000'000ULL #define timeNow() (chrono::high_resolution_clock::now().time_since_epoch().count()) #define all(x) x.begin(),x.end() #define len(x) (int)(x.size()) #define lenl(x) (ll)(x.size()) #define inRange(x,a,b) (a <= x && x <= b) #define fir first #define sec second #define self(x,y,z) y = x(y,z) template <typename X, typename Y, typename Z> using gp_hashmap = pbds::gp_hash_table<X,Y,Z,equal_to<X>,pbds::direct_mask_range_hashing<>,pbds::linear_probe_fn<>,pbds::hash_standard_resize_policy<pbds::hash_exponential_size_policy<>,pbds::hash_load_check_resize_trigger<>,true>>; template <typename X, typename Y, typename Z> using gp_hashmap_safe = pbds::cc_hash_table<X,Y,Z,equal_to<X>,pbds::direct_mod_range_hashing<>,pbds::hash_standard_resize_policy<pbds::hash_prime_size_policy,pbds::hash_load_check_resize_trigger<>,true>>; template <typename X, typename Y, typename Z> using cc_hashmap = pbds::cc_hash_table<X,Y,Z,equal_to<X>,pbds::direct_mask_range_hashing<>,pbds::hash_standard_resize_policy<pbds::hash_exponential_size_policy<>,pbds::hash_load_check_resize_trigger<>,true>>; template <typename X, typename Y, typename Z> using cc_hashmap_safe = pbds::cc_hash_table<X,Y,Z,equal_to<X>,pbds::direct_mod_range_hashing<>,pbds::hash_standard_resize_policy<pbds::hash_prime_size_policy,pbds::hash_load_check_resize_trigger<>,true>>; template <typename X, typename Y, typename Z> using indexed_set = pbds::tree<X, Y, Z, pbds::rb_tree_tag, pbds::tree_order_statistics_node_update>; struct chash { size_t operator()(const int& a) const { const static int rng = (int)timeNow() ^ (int)(ull)(make_unique<char>().get()); return a ^ rng; } }; namespace tree { const int base = 1<<19; ll tree[base * 2]; void update(int v, ll x) { v += base; if (tree[v] >= x) return; tree[v] = x; v /= 2; while (v != 0) { tree[v] = max(tree[v * 2], tree[v * 2 + 1]); v /= 2; } } ll query(int a, int b) { a += base - 1; b += base + 1; ll r = 0; while (a / 2 != b / 2) { if (a % 2 == 0) self(max, r, tree[a + 1]); if (b % 2 == 1) self(max, r, tree[b - 1]); a /= 2; b /= 2; } return r; } } namespace sol { const int N = 5e5+5; vector<int> t[N]; int n, c, m; void read() { cin >> n >> c; for (int i = 0; i < n; i++) { int a, w; cin >> a >> w; t[a].push_back(w); self(max, m, a); } } void solve() { ll best = 0; for (int i = 1; i <= m; i++) { vector<ll> ans; ans.reserve(len(t[i])); for (auto j : t[i]) { ll np = tree::query(j, j), p = max(tree::query(0, j - 1), tree::query(j + 1, tree::base - 1)) - c; ans.push_back(max(np, p) + i); } for (int j = 0; j < len(ans); j++) { self(max, best, ans[j]); tree::update(t[i][j], ans[j]); } } cout << best << '\n'; } }; int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); sol::read(); sol::solve(); } |
English