#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll SZ = 1 << 17, INF = 2E18;
ll P[SZ], B[SZ], dp[SZ];
struct Seg {
ll T[SZ << 1], Z[SZ << 1];
void Push(int n) {
if (n < SZ) {
Z[n * 2] += Z[n];
Z[n * 2 + 1] += Z[n];
}
T[n] += Z[n];
Z[n] = 0;
}
void Update(int L, int R, ll v, int sL = 1, int sR = SZ, int n = 1) {
Push(n);
if (R < sL || sR < L) return;
if (L <= sL && sR <= R) {
Z[n] += v;
Push(n);
return;
}
int mid = (sL + sR) >> 1;
Update(L, R, v, sL, mid, n * 2);
Update(L, R, v, mid + 1, sR, n * 2 + 1);
T[n] = max(T[n * 2], T[n * 2 + 1]);
}
ll Query(int L, int R, int sL = 1, int sR = SZ, int n = 1) {
Push(n);
if (R < sL || sR < L) return -INF;
if (L <= sL && sR <= R) return T[n];
int mid = (sL + sR) >> 1;
return max(Query(L, R, sL, mid, n * 2), Query(L, R, mid + 1, sR, n * 2 + 1));
}
} T;
int Get(vector<int>& S, int n) {
return (lower_bound(S.begin(), S.end(), n) - S.begin());
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
ll SP, EP, D, A, N;
cin >> SP >> EP >> D >> A >> N;
for (int i = 1; i <= N; ++i) cin >> P[i] >> B[i];
P[0] = SP, P[N + 1] = EP;
vector<int> mod;
for (int i = 0; i <= N + 1; ++i) mod.push_back(P[i] % D);
mod.push_back(-1);
sort(mod.begin(), mod.end());
mod.erase(unique(mod.begin(), mod.end()), mod.end());
int init = Get(mod, P[0] % D);
T.Update(1, SZ, -A);
T.Update(init, init, A);
for (int i = 1; i <= N + 1; ++i) {
int cur = Get(mod, P[i] % D);
ll diff = P[i] - P[i - 1], mv = diff / D;
T.Update(1, SZ, -mv * A);
if (diff % D) {
int a = Get(mod, P[i - 1] % D), b = Get(mod, P[i] % D);
if (a < b) T.Update(a, b - 1, -A);
else {
T.Update(a, SZ, -A);
if (1 < b) T.Update(1, b - 1, -A);
}
}
ll pv = T.Query(cur, cur);
dp[i] = T.Query(1, SZ) + B[i];
if (pv < dp[i]) T.Update(cur, cur, -pv + dp[i]);
}
cout << dp[N + 1] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4568 KB |
Output is correct |
6 |
Correct |
2 ms |
4568 KB |
Output is correct |
7 |
Correct |
1 ms |
4564 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
6988 KB |
Output is correct |
2 |
Correct |
96 ms |
6648 KB |
Output is correct |
3 |
Correct |
37 ms |
6096 KB |
Output is correct |
4 |
Correct |
107 ms |
6980 KB |
Output is correct |
5 |
Correct |
58 ms |
5892 KB |
Output is correct |
6 |
Correct |
36 ms |
5464 KB |
Output is correct |
7 |
Correct |
48 ms |
6312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
8132 KB |
Output is correct |
2 |
Correct |
136 ms |
9392 KB |
Output is correct |
3 |
Correct |
107 ms |
9168 KB |
Output is correct |
4 |
Correct |
95 ms |
9308 KB |
Output is correct |
5 |
Correct |
78 ms |
7392 KB |
Output is correct |
6 |
Correct |
59 ms |
6624 KB |
Output is correct |