# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
504004 | 79brue | Salesman (IOI09_salesman) | C++14 | 1663 ms | 65540 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Market{
int day; int loc; int w;
bool operator<(const Market &r)const{
return day == r.day ? loc < r.loc : day < r.day;
}
};
stack<pair<int*, int> > stk;
struct segTree{
int tree[1048578];
void init(int i, int l, int r){
if(l==r){
tree[i] = -1e9;
return;
}
int m = (l+r)>>1;
init(i*2, l, m), init(i*2+1, m+1, r);
tree[i] = -1e9;
}
void update(int i, int l, int r, int x, int v){
if(l==r){
stk.push(make_pair(&tree[i], tree[i]));
tree[i] = max(tree[i], v);
return;
}
int m = (l+r)>>1;
if(x<=m) update(i*2, l, m, x, v);
else update(i*2+1, m+1, r, x, v);
stk.push(make_pair(&tree[i], tree[i]));
tree[i] = max(tree[i*2], tree[i*2+1]);
}
int query(int i, int l, int r, int s, int e){
if(r<s || e<l) return -1e9;
if(s<=l && r<=e) return tree[i];
int m = (l+r)>>1;
return max(query(i*2, l, m, s, e), query(i*2+1, m+1, r, s, e));
}
} treeU, treeD;
int n; int u, d; int s;
Market arr[500002];
void reset(){
while(!stk.empty()) stk.pop();
}
void rollback(){
while(!stk.empty()){
*(stk.top().first) = stk.top().second;
stk.pop();
}
}
int main(){
scanf("%d %d %d %d", &n, &u, &d, &s);
for(int i=1; i<=n; i++){
scanf("%d %d %d", &arr[i].day, &arr[i].loc, &arr[i].w);
}
sort(arr+1, arr+n+1);
const int MAX = 500001;
treeU.init(1, 1, MAX), treeD.init(1, 1, MAX);
treeU.update(1, 1, MAX, s, -u*s);
treeD.update(1, 1, MAX, s, d*s);
for(int i=1; i<=n; i++){
int j = i;
while(arr[j+1].day == arr[i].day) j++;
reset();
if(i==j){
int val = max(treeU.query(1, 1, MAX, arr[i].loc, MAX) + arr[i].loc * u,
treeD.query(1, 1, MAX, 1, arr[i].loc) - arr[i].loc * d) + arr[i].w;
treeU.update(1, 1, MAX, arr[i].loc, val-u*arr[i].loc);
treeD.update(1, 1, MAX, arr[i].loc, val+d*arr[i].loc);
continue;
}
vector<pair<int, int> > vec;
for(int x=i; x<=j; x++){
int val = max(treeU.query(1, 1, MAX, arr[x].loc, MAX) + arr[x].loc * u,
treeD.query(1, 1, MAX, 1, arr[x].loc) - arr[x].loc * d) + arr[x].w;
treeU.update(1, 1, MAX, arr[x].loc, val-u*arr[x].loc);
treeD.update(1, 1, MAX, arr[x].loc, val+d*arr[x].loc);
vec.push_back(make_pair(arr[x].loc, val));
}
rollback();
for(int x=j; x>=i; x--){
int val = max(treeU.query(1, 1, MAX, arr[x].loc, MAX) + arr[x].loc * u,
treeD.query(1, 1, MAX, 1, arr[x].loc) - arr[x].loc * d) + arr[x].w;
treeU.update(1, 1, MAX, arr[x].loc, val-u*arr[x].loc);
treeD.update(1, 1, MAX, arr[x].loc, val+d*arr[x].loc);
}
for(auto p: vec){
treeU.update(1, 1, MAX, p.first, p.second-u*p.first);
treeD.update(1, 1, MAX, p.first, p.second+d*p.first);
}
i=j;
}
int ans = max(treeU.query(1, 1, MAX, s, MAX) + s*u,
treeD.query(1, 1, MAX, 1, s) - s*d);
printf("%lld", ans);
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |