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;
vector<ll> lp, rp, wl, wr;
ll p, re = (1LL << 50);
void update(bool w, ll i, ll x)
{
while (i > 0)
{
if (w)
rp[i] = min(rp[i], x);
else
lp[i] = min(lp[i], x);
i /= 2;
}
return;
}
ll qmin(bool w, ll l, ll r, ll i)
{
if (wl[i] >= r || wr[i] <= l)
return re;
if (wl[i] >= l && wr[i] <= r)
{
if (w)
return rp[i];
else
return lp[i];
}
return min(qmin(w, l, r, 2 * i), qmin(w, l, r, 2 * i + 1));
}
int main()
{
ll m, n, i, j, k, a, b, w = 0, ma;
cin >> m >> n;
vector<vector<ll> > v, vv(m);
vector<ll> d(m), s(3 * m);
for (i = 0; i < m; i++)
{
cin >> j;
v.push_back({j - 1, i, -1});
cin >> j;
v.push_back({j - 1, i, 1});
cin >> j;
v.push_back({j - 1, i, 0});
cin >> d[i];
}
sort(v.begin(), v.end());
if (v[0][0] || v[3 * m - 1][0] != n - 1)
{
cout << -1;
return 0;
}
for (i = 0; i < 3 * m; i++)
{
if (!i)
s[i] = 0;
else if (v[i][0] > v[i - 1][0])
s[i] = s[i - 1] + 1;
else
s[i] = s[i - 1];
w = max(w, s[i]);
}
for (i = 0; i < 3 * m; i++)
v[i] = {v[i][1], s[i], v[i][2]};
sort(v.begin(), v.end());
for (i = 0; i < m; i++)
vv[i] = {v[3 * i][1], v[3 * i + 1][1], v[3 * i + 2][1]};
p = w + 1;
while (p != (p & -p))
p++;
lp.resize(2 * p);
rp.resize(2 * p);
wl.resize(2 * p);
wr.resize(2 * p);
for (i = p; i < 2 * p; i++)
{
lp[i] = rp[i] = re;
wl[i] = i;
wr[i] = i + 1;
}
lp[p] = rp[p + w] = 0;
for (i = p - 1; i > 0; i--)
{
lp[i] = min(lp[2 * i], lp[2 * i + 1]);
rp[i] = min(rp[2 * i], rp[2 * i + 1]);
wl[i] = wl[2 * i];
wr[i] = wr[2 * i + 1];
}
ma = re;
ll ml, mr;
for (i = 0; i < m; i++)
{
update(0, vv[i][1] + p, qmin(0, vv[i][0] + p, vv[i][1] + p, 1) + d[i]);
update(1, vv[i][1] + p, qmin(1, vv[i][1] + 1 + p, vv[i][2] + 1 + p, 1) + d[i]);
ml = lp[vv[i][0] + p];
for (j = w; j > vv[i][0]; j--)
ml = min(ml, lp[j + p]);
mr = rp[vv[i][2] + p];
for (j = 0; j < vv[i][2]; j++)
mr = min(mr, rp[j + p]);
ma = min(ma, ml + mr + d[i]);
}
for (j = 1; j <= w; j++)
rp[j] = min(rp[j + p], rp[j + p - 1]);
for (j = w - 1; j >= 0; j--)
lp[j + p] = min(lp[j + p], lp[j + p + 1]);
for (j = 0; j <= w; j++)
ma = min(ma, lp[j + p] + rp[j + p]);
if (ma < re)
cout << ma;
else
cout << -1;
return 0;
}
Compilation message (stderr)
pinball.cpp: In function 'int main()':
pinball.cpp:33:20: warning: unused variable 'k' [-Wunused-variable]
33 | ll m, n, i, j, k, a, b, w = 0, ma;
| ^
pinball.cpp:33:23: warning: unused variable 'a' [-Wunused-variable]
33 | ll m, n, i, j, k, a, b, w = 0, ma;
| ^
pinball.cpp:33:26: warning: unused variable 'b' [-Wunused-variable]
33 | ll m, n, i, j, k, a, b, w = 0, ma;
| ^
# | 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... |