#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file "name"
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
const int inf = 1e9;
const ll linf = 1e17;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int n, cnt;
ll res, tmp, k, m, d, mana;
pii a[N];
ll st[4 * N], cost;
int x[N];
void inp()
{
cin >> k >> m >> d >> mana >> n;
a[1] = {k, 0};
for(int i = 2; i <= n + 1; ++i) cin >> a[i].fi >> a[i].se;
n++;
a[++n] = {m, 0};
}
void build(int id, int l, int r) {
if(l == r) {
st[id] = -linf;
return ;
}
int m = (l + r) >> 1;
build(id << 1, l, m);
build(id << 1 | 1, m + 1, r);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
void update(int id, int l, int r, int i, ll v) {
if(i < l || r < i) return ;
if(l == r) {
maximize(st[id], v);
return ;
}
int m = (l + r) >> 1;
update(id << 1, l, m, i, v);
update(id << 1 | 1, m + 1, r, i, v);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
ll get(int id, int l, int r, int u, int v) {
if(v < l || r < u) return -linf;
if(l >= u && r <= v) return st[id];
int m = (l + r) >> 1;
return max(get(id << 1, l, m, u, v), get(id << 1 | 1, m + 1, r, u, v));
}
void solve()
{
for(int i = 1; i <= n; ++i) {
x[i] = a[i].fi % d;
if(a[i].fi % d == 0) x[i] = d;
}
sort(x + 1, x + n + 1);
cnt = unique(x + 1, x + n + 1) - x - 1;
build(1, 1, cnt);
int p = a[1].fi % d;
if(a[1].fi % d == 0) p = d;
int id = lower_bound(x + 1, x + cnt + 1, p) - x;
update(1, 1, cnt, id, (a[1].fi + d - 1) / d * mana);
for(int i = 2; i <= n; ++i) {
int p = a[i].fi % d;
if(a[i].fi % d == 0) p = d;
int id = lower_bound(x + 1, x + cnt + 1, p) - x;
tmp = get(1, 1, cnt, 1, id - 1) - mana;
maximize(tmp, get(1, 1, cnt, id, cnt));
cost = tmp - (a[i].fi + d - 1) / d * mana + a[i].se;
update(1, 1, cnt, id, cost + (a[i].fi + d - 1) / d * mana);
}
cout << cost;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen(file".inp" , "r" , stdin);
// freopen(file".out" , "w" , stdout);
inp();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
2536 KB |
Output is correct |
2 |
Correct |
36 ms |
2396 KB |
Output is correct |
3 |
Correct |
14 ms |
2004 KB |
Output is correct |
4 |
Correct |
36 ms |
2360 KB |
Output is correct |
5 |
Correct |
20 ms |
1876 KB |
Output is correct |
6 |
Correct |
12 ms |
1396 KB |
Output is correct |
7 |
Correct |
15 ms |
2348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
3532 KB |
Output is correct |
2 |
Correct |
77 ms |
4380 KB |
Output is correct |
3 |
Correct |
80 ms |
4428 KB |
Output is correct |
4 |
Correct |
68 ms |
4444 KB |
Output is correct |
5 |
Correct |
49 ms |
2980 KB |
Output is correct |
6 |
Correct |
36 ms |
2720 KB |
Output is correct |