/*
Ben Watson
Quang Minh MasterDDDDD
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const string name = "test";
void solve();
signed main()
{
if (fopen((name + ".inp").c_str(), "r"))
{
freopen((name + ".inp").c_str(), "r", stdin);
freopen((name + ".ans").c_str(), "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
return 0;
}
// main program
const ll oo = 0x3f3f3f3f3f3f3f3f;
const int maxm = 1e5 + 1;
int t[maxm], l[maxm], r[maxm], c[maxm];
vector<int> S, T, adj[maxm];
int n, m;
// -----------------SUBTASK 2-----------------
void build(int i, int j)
{
int diff = abs(t[i] - t[j]);
if (diff <= r[i] - l[j] + 1)
adj[i].push_back(j);
}
vector<ll> Dijsktra()
{
vector<ll> dist(m + 1, oo);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
for (int s : S)
pq.push({dist[s] = c[s], s});
while (!pq.empty())
{
int u = pq.top().second;
ll w = pq.top().first;
pq.pop();
if (dist[u] != w)
continue;
for (int v : adj[u])
if (dist[v] > dist[u] + c[v])
pq.push({dist[v] = dist[u] + c[v], v});
}
return dist;
}
// -----------------SUBTASK 1-----------------
struct SegmentTree
{
vector<ll> st;
int n;
SegmentTree(int n) : n(n) { st.resize(4 * n + 1, oo); }
void update(int head, int l, int r, int pos, ll val)
{
if (pos < l || r < pos) return;
if (l == r)
{
st[head] = min(st[head], val);
return;
}
int mid = l + r >> 1;
if (pos <= mid)
update(head << 1, l, mid, pos, val);
else
update(head << 1 | 1, mid + 1, r, pos, val);
st[head] = min(st[head << 1], st[head << 1 | 1]);
}
void update(int pos, ll val) { update(1, 1, n, pos, val); }
ll query(int head, int l, int r, int u, int v)
{
if (l > v || r < u) return oo;
if (u <= l && r <= v) return st[head];
int mid = l + r >> 1;
return min(query(head << 1, l, mid, u, v), query(head << 1 | 1, mid + 1, r, u, v));
}
ll query(int u, int v) { return query(1, 1, n, u, v); }
};
vector<array<int, 4>> proc;
void subtask1()
{
vector<int> v;
for (int i = 1; i <= m; i++)
{
proc.push_back({r[i], l[i], t[i], c[i]});
v.push_back(r[i]);
}
// compress
int m = 0;
map<int, int> mp;
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int x : v)
mp[x] = ++m;
if (v.back() != n)
{
cout << -1 << '\n';
return;
}
SegmentTree st(m);
sort(proc.begin(), proc.end());
for (auto it : proc)
{
int L = it[1], R = it[0], C = it[3];
auto pos = lower_bound(v.begin(), v.end(), L - 1) - v.begin();
pos++;
if (L != 1)
st.update(mp[R], st.query(pos, m) + C);
else
st.update(mp[R], C);
}
ll res = st.query(m, m);
cout << (res == oo ? -1 : res) << '\n';
}
void solve()
{
cin >> n >> m;
bool issub1 = true;
for (int i = 1; i <= m; i++)
{
cin >> t[i] >> l[i] >> r[i] >> c[i];
if (t[i] != 1)
issub1 = false;
if (l[i] == 1)
S.push_back(i);
if (r[i] == n)
T.push_back(i);
}
if (issub1)
{
subtask1();
return;
}
for (int i = 1; i <= m; i++)
for (int j = i + 1; j <= m; j++)
{
build(i, j);
build(j, i);
}
ll res = oo;
vector<ll> dist = Dijsktra();
for (int t : T)
res = min(res, dist[t]);
cout << (res == oo ? -1 : res) << '\n';
}
Compilation message (stderr)
treatment.cpp: In function 'int main()':
treatment.cpp:19:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen((name + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treatment.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen((name + ".ans").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |