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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 2e5 + 10;
ll T[maxn], L[maxn], R[maxn], C[maxn], p[maxn], dp[maxn];
vector<pair<int,ll>> g[maxn];
void dijkstra(int v){
memset(dp, -1, sizeof dp);
set<pair<ll,int>> s;
dp[v] = 0;
s.insert({dp[v], v});
while (!s.empty()){
v = (*s.begin()).second;
s.erase(s.begin());
for (auto [u, w] : g[v]){
if (dp[u] == -1 or dp[u] > dp[v] + w){
s.erase({dp[u],u});
dp[u] = dp[v] + w;
s.insert({dp[u],u});
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
ll n, m;
cin >> n >> m;
for (ll i = 1; i <= m; i++)
cin >> T[i] >> L[i] >> R[i] >> C[i];
if (*max_element(T+1, T+m+1) == 1){
for (ll i = 1; i <= m; i++)
p[i] = i;
sort(p+1, p+m+1, [](ll a, ll b){ return R[a] < R[b]; });
vector<ll> S;
for (ll iit = 1; iit <= m; iit++){
ll i = p[iit];
if (L[i] == 1)
dp[i] = C[i];
else
dp[i] = -1;
if (S.empty()){
if (dp[i] != -1)
S.push_back(i);
continue;
}
ll lo = -1, hi = S.size();
while (hi - lo > 1){
ll mid = (lo + hi) >> 1;
if (R[S[mid]]+1 < L[i])
lo = mid;
else
hi = mid;
}
if (hi == S.size())
continue;
if (dp[i] == -1 or dp[i] > dp[S[hi]] + C[i])
dp[i] = dp[S[hi]] + C[i];
while (!S.empty() and dp[S.back()] >= dp[i])
S.pop_back();
S.push_back(i);
}
ll answer = -1;
for (ll i = 1; i <= m; i++)
if (R[i] == n and dp[i] != -1 and (answer == -1 or answer > dp[i]))
answer = dp[i];
cout << answer << endl;
return 0;
}
for (ll i = 1; i <= m; i++)
p[i] = i;
for (int i = 1; i <= m; i++){
if (L[i] == 1)
g[0].push_back({i,C[i]});
if (R[i] == n)
g[i].push_back({m+1, 0});
for (int j = 1; j <= m; j++){
if (abs(T[i]-T[j]) <= R[j] - L[i] + 1){
g[j].push_back({i, C[i]});
}
}
}
dijkstra(0);
cout << dp[m+1] << endl;
}
Compilation message (stderr)
treatment.cpp: In function 'void dijkstra(int)':
treatment.cpp:17:13: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
for (auto [u, w] : g[v]){
^
treatment.cpp: In function 'int main()':
treatment.cpp:57:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (hi == S.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... |