이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 1;
const ll INF = 1e18;
int k, m, d, a, n;
pair<int, int> t[N];
struct segTree{
int n, a;
vector <int> vals;
vector <ll> maxV;
segTree(const vector <int> &_vals, int _a){
n = _vals.size();
a = _a;
vals = _vals;
maxV.assign(n * 4 + 1, -INF);
}
void update(int id, int l, int r, int u, int v, ll val){
if (v < l || r < u)
return;
if (u <= l && r <= v){
maxV[id] = max(maxV[id], val);
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, u, v, val);
update(id << 1 | 1, mid + 1, r, u, v, val);
}
ll get(int id, int l, int r, int pos){
if (pos < l || r < pos)
return -INF;
ll res = maxV[id];
if (l == r)
return res;
int mid = (l + r) >> 1;
res = max(res, get(id << 1, l, mid, pos));
res = max(res, get(id << 1 | 1, mid + 1, r, pos));
return res;
}
void update(int rem, ll val){
int pos = lower_bound(vals.begin(), vals.end(), rem) - vals.begin() + 1;
update(1, 1, n, 1, pos, val);
update(1, 1, n, pos, n, val - a);
}
ll get(int rem){
int pos = lower_bound(vals.begin(), vals.end(), rem) - vals.begin() + 1;
return get(1, 1, n, pos);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> k >> m >> d >> a >> n;
for(int i = 1; i <= n; i++)
cin >> t[i].first >> t[i].second;
m -= k;
for(int i = 1; i <= n; i++)
t[i].first -= k;
k = 0;
sort(t + 1, t + 1 + n);
vector <int> vals;
for(int i = 1; i <= n; i++)
vals.push_back(t[i].first % d);
vals.push_back(k % d);
vals.push_back(m % d);
sort(vals.begin(), vals.end());
vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
segTree ST(vals, a);
ST.update(k % d, 0);
for(int i = 1; i <= n; i++){
int rem = t[i].first % d;
ll val = ST.get(rem) + t[i].second;
ST.update(rem, val);
}
ll ans = ST.get(m % d) - m / d * a;
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |