This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "railroad.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;
int sum[500001],p[500001];
int f(int i){
return (p[i]==i?i:p[i]=f(p[i]));
}
int unite(int i, int j){
i=f(i);
j=f(j);
if (i==j)
return 0;
p[j]=i;
return 1;
}
long long plan_roller_coaster(vector <int32_t> s, vector <int32_t> t){
int n=s.size();
vector <int> ve;
vector <pair <int, int>> ve2;
for (int i=0;i<n;i++){
ve.push_back(s[i]);
ve.push_back(t[i]);
}
sort(ve.begin(),ve.end());
ve.resize(unique(ve.begin(),ve.end())-ve.begin());
int res=0;
iota(p,p+ve.size()+2,0);
for (int i=0;i<n;i++){
s[i]=lower_bound(ve.begin(),ve.end(),s[i])-ve.begin()+1;
t[i]=lower_bound(ve.begin(),ve.end(),t[i])-ve.begin()+1;
sum[s[i]]++;
sum[t[i]]--;
unite(s[i],t[i]);
}
sum[1]--;
unite(1,ve.size()+1);
for (int i=1;i<=ve.size();i++){
sum[i]+=sum[i-1];
if (sum[i])
unite(i,i+1);
if (i<ve.size()){
res+=max(sum[i]*(ve[i]-ve[i-1]),0LL);
ve2.push_back({ve[i]-ve[i-1],i});
}
}
sort(ve2.begin(),ve2.end());
for (auto [w,i]:ve2)
res+=w*unite(i,i+1);
return res;
}
Compilation message (stderr)
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:38:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int i=1;i<=ve.size();i++){
| ~^~~~~~~~~~~
railroad.cpp:42:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | if (i<ve.size()){
| ~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |