제출 #651726

#제출 시각아이디문제언어결과실행 시간메모리
651726loctildoreSalesman (IOI09_salesman)C++14
17 / 100
214 ms65536 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
#define all(x) begin(x), end(x)
//#define endl '\n'
struct node {
  int l, r;
  node *lft, *rht;
  int lmax, rmax;
  node (int tl, int tr): l(tl), r(tr), lmax(LLONG_MIN/2), rmax(LLONG_MIN/2) {
    if (l+1==r) {
      lft=rht=NULL;
    }
    else {
      lft=new node(l,(l+r)/2);
      rht=new node((l+r)/2,r);
    }
  }
};
node *root;
int n,u,d,s;
int t,loc,m;
map<int,vector<pair<int,int>>> mp;
void update(node *x, int p, int v) {
  if (x->r<=p||p<x->l) {
    return;
  }
  if (x->l+1==x->r) {
    x->lmax=max(x->lmax,v+x->l*d);
    x->rmax=max(x->rmax,v-x->l*u);
    return;
  }
  update(x->lft,p,v);
  update(x->rht,p,v);
  x->lmax=max(x->lft->lmax,x->rht->lmax);
  x->rmax=max(x->lft->rmax,x->rht->rmax);
}
int qry(node *x, int l, int r, bool tmp) {
  if (r<=x->l||x->r<=l) {
    return LLONG_MIN/2;
  }
  if (l<=x->l&&x->r<=r) {
    return tmp?x->rmax:x->lmax;
  }
  return max(qry(x->lft,l,r,tmp),qry(x->rht,l,r,tmp));
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  //freopen("salesman.in","r",stdin);
  //freopen("salesman.out","w",stdout);
  cin>>n>>u>>d>>s;
  root=new node(0,500069);
  update(root,s,0);
  for (int i = 0; i < n; i++) {
    cin>>t>>loc>>m;
    mp[t].push_back({loc,m});
  }
  for (auto &tmp : mp) {
    sort(all(tmp.s));
    vector<pair<int,int>> vctr;
    for (auto i : tmp.s) {
      tie(loc,m)=i;
      int tmp=qry(root,0,loc,0)-loc*d;
      tmp=max(tmp,qry(root,loc,500069,1)+loc*u);
      vctr.push_back({loc,tmp+m});
    }
    for (auto i : vctr) {
      update(root,i.f,i.s);
    }
  }
  int tmp=qry(root,0,s,0)-s*d;
  tmp=max(tmp,qry(root,s,500069,1)+s*u);
  cout<<tmp<<endl;
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...