Submission #1007551

# Submission time Handle Problem Language Result Execution time Memory
1007551 2024-06-25T07:25:09 Z huutuan Salesman (IOI09_salesman) C++14
100 / 100
311 ms 28096 KB
#include <bits/stdc++.h>

using namespace std;

const int N=5e5+10;
const int inf=2e9;

struct SegmentTree{
   int n;
   vector<int> t;
   void init(int _n){
      n=_n;
      t.assign(4*n+1, -inf);
   }
   void update(int k, int l, int r, int pos, int val){
      if (l==r){
         t[k]=val;
         return;
      }
      int mid=(l+r)>>1;
      if (pos<=mid) update(k<<1, l, mid, pos, val);
      else update(k<<1|1, mid+1, r, pos, val);
      t[k]=max(t[k<<1], t[k<<1|1]);
   }
   int get(int k, int l, int r, int L, int R){
      if (r<L || R<l) return -inf;
      if (L<=l && r<=R) return t[k];
      int mid=(l+r)>>1;
      return max(get(k<<1, l, mid, L, R), get(k<<1|1, mid+1, r, L, R));
   }
} st1, st2;

int n, s, f[N], f1[N], f2[N], val[N];
int u, d;
vector<pair<int, int>> vv;

void calc(int t, int l, int r){
   for (int ii=l; ii<=r; ++ii){
      int i=vv[ii].second;
      f1[i]=f2[i]=max(st1.get(1, 1, st1.n, 1, i)-d*i, st2.get(1, 1, st2.n, i, st2.n)+u*i);
   }
   f2[vv[l].second]+=val[vv[l].second];
   f1[vv[r].second]+=val[vv[r].second];
   for (int i=l+1; i<=r; ++i) f2[vv[i].second]=max(f2[vv[i].second], f2[vv[i-1].second]-d*(vv[i].second-vv[i-1].second))+val[vv[i].second];
   for (int i=r-1; i>=l; --i) f1[vv[i].second]=max(f1[vv[i].second], f1[vv[i+1].second]-u*(vv[i+1].second-vv[i].second))+val[vv[i].second];
   for (int ii=l; ii<=r; ++ii){
      int i=vv[ii].second;
      f[i]=max(f1[i], f2[i]);
      st1.update(1, 1, st1.n, i, f[i]+d*i);
      st2.update(1, 1, st2.n, i, f[i]-u*i);
   }
}

int32_t main(){
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   cin >> n >> u >> d >> s;
   for (int i=1; i<=n; ++i){
      int t, l, m; cin >> t >> l >> m;
      vv.emplace_back(t, l);
      val[l]=m;
   }
   vv.emplace_back(0, s);
   vv.emplace_back(500001, s);
   sort(vv.begin(), vv.end());
   st1.init(N); st2.init(N);
   for (int i=0; i<N; ++i) f[i]=-inf;
   f[s]=0;
   st1.update(1, 1, st1.n, s, f[s]+d*s);
   st2.update(1, 1, st2.n, s, f[s]-u*s);
   for (int i=1; i<(int)vv.size(); ++i){
      int j=i;
      while (j+1<(int)vv.size() && vv[j+1].first==vv[i].first) ++j;
      calc(vv[i].first, i, j);
      i=j;
   }
   cout << f[s] << '\n';
   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 23640 KB Output is correct
2 Correct 5 ms 23644 KB Output is correct
3 Correct 5 ms 23792 KB Output is correct
4 Correct 6 ms 23680 KB Output is correct
5 Correct 7 ms 23900 KB Output is correct
6 Correct 18 ms 24252 KB Output is correct
7 Correct 39 ms 24284 KB Output is correct
8 Correct 65 ms 24852 KB Output is correct
9 Correct 108 ms 25288 KB Output is correct
10 Correct 160 ms 27192 KB Output is correct
11 Correct 192 ms 26300 KB Output is correct
12 Correct 282 ms 27324 KB Output is correct
13 Correct 256 ms 27068 KB Output is correct
14 Correct 311 ms 27952 KB Output is correct
15 Correct 284 ms 27904 KB Output is correct
16 Correct 299 ms 27832 KB Output is correct
17 Correct 5 ms 23644 KB Output is correct
18 Correct 4 ms 23644 KB Output is correct
19 Correct 5 ms 23644 KB Output is correct
20 Correct 10 ms 23748 KB Output is correct
21 Correct 6 ms 23644 KB Output is correct
22 Correct 6 ms 23896 KB Output is correct
23 Correct 7 ms 23900 KB Output is correct
24 Correct 7 ms 23896 KB Output is correct
25 Correct 65 ms 24856 KB Output is correct
26 Correct 113 ms 25544 KB Output is correct
27 Correct 195 ms 26816 KB Output is correct
28 Correct 200 ms 26908 KB Output is correct
29 Correct 267 ms 27840 KB Output is correct
30 Correct 263 ms 28096 KB Output is correct