#include<bits/stdc++.h>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define Times (1.0 * clock() / CLOCKS_PER_SEC)
#define fi first
#define base 31
#define sz(x) (int)(x).size()
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); freopen(name".bug", "w", stderr); }
#define task "name"
#define tsk ""
const int oo = 1e9 + 7;
const ll loo = (ll)1e18 + 7;
const int MOD = 1e9 + 7;
const int MOD2 = 1e9 + 696969;
const int N = 4e5 + 3;
const int BASE = 10;
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;}
ll n , m , k , d , a , t[N] , b[N] , pre[N] , dp[N] , p[N] , vt[N];
ll st[N << 2];
vector<ll> nen;
void update(int id , int l , int r , int i , ll val){
if(l > i || r < i) return;
if(l == r) return void(maximize(st[id] , val));
int mid = l + r >> 1;
update(id << 1 , l , mid , i , val);
update(id << 1 | 1 , mid + 1 , r , i , val);
st[id] = max(st[id << 1] , st[id << 1 | 1]);
}
ll get(int id , int l , int r , int u , int v){
if(l > v || r < u) return -loo;
if(u <= l && r <= v) return st[id];
int mid = l + r >> 1;
return max(get(id << 1 , l , mid , u , v) , get(id << 1 | 1 , mid + 1 , r , u , v));
}
void Solve(){
cin >> k >> m >> d >> a >> n;
t[0] = k;
p[0] = k % d;
t[n + 1] = m;
p[n + 1] = m % d;
memset(dp , -0x3f , sizeof dp);
dp[0] = 0;
int cnt = 0;
for(int i = 1 ; i <= n ; i++){
cin >> t[i] >> b[i];
p[i] = t[i] % d;
}
for(int i = 0 ; i <= n + 1 ; i++){
nen.pb(p[i]);
}
memset(st , -0x3f , sizeof st);
sort(all(nen));
nen.erase(unique(all(nen)) , nen.end());
for(auto v : nen) vt[v] = ++cnt;
for(int i = 1 ; i <= n + 1 ; i++){
dp[i] = dp[0] - (t[i] / d - t[0] / d + (p[i] > p[0])) * a + b[i];
maximize(dp[i] , get(1 , 1 , n + 30, vt[p[i]] , vt[p[i]]) - (t[i] / d) * a + b[i]);
maximize(dp[i] , get(1 , 1 , n + 30 , 1 , vt[p[i]] - 1) - (t[i] / d + 1) * a + b[i]);
maximize(dp[i] , get(1 , 1 , n + 30 , vt[p[i]] + 1 , n + 30) - (t[i] / d) * a + b[i]);
update(1 , 1 , n + 30 , p[i] , dp[i] + (t[i] / d) * a);
}
cout << dp[n + 1];
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
// file(task);
// file(tsk);
int T = 1;
// cin >> T;
while(T--)
{
Solve();
}
cerr << "Time elapsed: " << Times << " s.\n";
return 0;
}
Compilation message
koala.cpp: In function 'void update(int, int, int, int, long long int)':
koala.cpp:37:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int mid = l + r >> 1;
| ~~^~~
koala.cpp: In function 'long long int get(int, int, int, int, int)':
koala.cpp:46:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
21 ms |
34188 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
62 ms |
19280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
91 ms |
20052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |