Submission #649577

# Submission time Handle Problem Language Result Execution time Memory
649577 2022-10-10T16:31:32 Z KoalaMuch Salesman (IOI09_salesman) C++14
100 / 100
1889 ms 52072 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5+4;
long long sm[N*4][2];
vector<pair<long long,long long>> fair[N];
long long old[N];
long long can[N];
void build(long long l,long long r,long long now,long long u,long long d){
    if(l==r){
        sm[now][0] = -4e9+l*d;
        sm[now][1] = -4e9-l*u;
        return ;
    }
    long long mid = (l+r) >> 1;
    build(l,mid,now<<1,u,d),build(mid+1,r,now<<1|1,u,d);
    sm[now][0] = max(sm[now<<1][0],sm[now<<1|1][0]);
    sm[now][1] = max(sm[now<<1][1],sm[now<<1|1][1]);
}
void update(long long l,long long r,long long id,long long now,long long v,long long t,bool rep){
    if(l>id||r<id)  return ;
    if(l==r){
        if(!rep)    sm[now][t] = max(sm[now][t],v);
        else        sm[now][t] = v;
        return ;
    }
    long long mid = (l+r) >> 1;
    update(l,mid,id,now<<1,v,t,rep),update(mid+1,r,id,now<<1|1,v,t,rep);
    sm[now][t] = max(sm[now<<1][t],sm[now<<1|1][t]);
}
long long query(long long l,long long r,long long ll,long long rr,long long now,long long t){
    if(l>rr||r<ll)  return -5e9;
    if(l>=ll&&r<=rr)    return sm[now][t];
    long long mid = (l+r) >> 1;
    return max(query(l,mid,ll,rr,now<<1,t),query(mid+1,r,ll,rr,now<<1|1,t));
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    long long n,u,d,s,day,loc,pro;
    cin >> n >> u >> d >> s;
    for(long long i=0;i<n;i++){
        cin >> day >> loc >> pro;
        fair[day].push_back({loc,pro});
    }
    long long ans = 0;
    build(1,500001,1,u,d);
    update(1,500001,s,1,s*d,0,false);
    update(1,500001,s,1,-s*u,1,false);
    for(long long i=1;i<=500001;i++){
        sort(fair[i].begin(),fair[i].end());
        for(long long j=0;j<fair[i].size();j++){
            loc = fair[i][j].first,pro = fair[i][j].second;
            old[loc] = query(1,500001,loc,loc,1,0)-loc*d;
            long long fromd = query(1,500001,1,loc,1,0)+pro-loc*d,fromu = query(1,500001,loc,500001,1,1)+pro+loc*u;
            long long bestV = max(fromu,fromd);
            can[loc] = bestV;
            //if(loc==80)  cout << fromd << " " << fromu << " " << query(1,500001,loc,500001,1,1) << "\n";
            update(1,500001,loc,1,bestV+d*loc,0,false);
            update(1,500001,loc,1,bestV-u*loc,1,false);
        }
        for(long long j=0;j<fair[i].size();j++){
            loc = fair[i][j].first;
            update(1,500001,loc,1,old[loc]+d*loc,0,true);
            update(1,500001,loc,1,old[loc]-u*loc,1,true);
        }
        for(long long j=fair[i].size()-1;j>=0;j--){
            loc = fair[i][j].first,pro=fair[i][j].second;
            long long fromd = query(1,500001,1,loc,1,0)+pro-loc*d,fromu = query(1,500001,loc,500001,1,1)+pro+loc*u;
            long long bestV = max(fromu,fromd);
            can[loc] = max(can[loc],bestV);
            update(1,500001,loc,1,bestV+d*loc,0,false);
            update(1,500001,loc,1,bestV-u*loc,1,false);
        }
        for(long long j=0;j<fair[i].size();j++){
            loc = fair[i][j].first;
            update(1,500001,loc,1,can[loc]+d*loc,0,false);
            update(1,500001,loc,1,can[loc]-u*loc,1,false);
            if(loc<s)   ans = max(ans,can[loc]-d*(s-loc));
            else        ans = max(ans,can[loc]-u*(loc-s));
        }
        //if(fair[i].size())  cout << ans << "\n";
    }
    cout << ans << "\n";
    return 0;
}

Compilation message

salesman.cpp: In function 'int main()':
salesman.cpp:51:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         for(long long j=0;j<fair[i].size();j++){
      |                           ~^~~~~~~~~~~~~~~
salesman.cpp:61:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |         for(long long j=0;j<fair[i].size();j++){
      |                           ~^~~~~~~~~~~~~~~
salesman.cpp:74:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |         for(long long j=0;j<fair[i].size();j++){
      |                           ~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 28500 KB Output is correct
2 Correct 22 ms 28420 KB Output is correct
3 Correct 20 ms 28488 KB Output is correct
4 Correct 28 ms 28624 KB Output is correct
5 Correct 36 ms 28620 KB Output is correct
6 Correct 94 ms 36924 KB Output is correct
7 Correct 210 ms 37944 KB Output is correct
8 Correct 383 ms 39400 KB Output is correct
9 Correct 569 ms 40996 KB Output is correct
10 Correct 1024 ms 45772 KB Output is correct
11 Correct 1173 ms 45728 KB Output is correct
12 Correct 1514 ms 48844 KB Output is correct
13 Correct 1507 ms 48716 KB Output is correct
14 Correct 1884 ms 52072 KB Output is correct
15 Correct 1688 ms 51952 KB Output is correct
16 Correct 1889 ms 52064 KB Output is correct
17 Correct 19 ms 28472 KB Output is correct
18 Correct 20 ms 28504 KB Output is correct
19 Correct 24 ms 28468 KB Output is correct
20 Correct 31 ms 28628 KB Output is correct
21 Correct 28 ms 28568 KB Output is correct
22 Correct 37 ms 28628 KB Output is correct
23 Correct 33 ms 28628 KB Output is correct
24 Correct 35 ms 28696 KB Output is correct
25 Correct 400 ms 38420 KB Output is correct
26 Correct 721 ms 40424 KB Output is correct
27 Correct 1199 ms 43160 KB Output is correct
28 Correct 1222 ms 42324 KB Output is correct
29 Correct 1862 ms 45516 KB Output is correct
30 Correct 1728 ms 46280 KB Output is correct