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;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
const int MAXM = 3e5;
const ll INF = 1e18;
int N, M;
int L[MAXN+10], R[MAXN+10], P[MAXN+10], D[MAXN+10];
ll tree[MAXM*4+10];
void init(int node, int tl, int tr)
{
tree[node]=INF;
if(tl==tr) return;
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
}
void update(int node, int tl, int tr, int p, ll v)
{
if(tl==tr)
{
tree[node]=min(tree[node], v);
return;
}
int mid=tl+tr>>1;
if(p<=mid) update(node*2, tl, mid, p, v);
else update(node*2+1, mid+1, tr, p, v);
tree[node]=min(tree[node*2], tree[node*2+1]);
}
ll query(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) return tree[node];
if(r<tl || tr<l) return INF;
int mid=tl+tr>>1;
return min(query(node*2, tl, mid, l, r), query(node*2+1, mid+1, tr, l, r));
}
ll dp1[MAXN+10], dp2[MAXN+10];
int main()
{
int i, j;
scanf("%d%d", &N, &M);
vector<int> comp;
comp.push_back(1); comp.push_back(M);
for(i=1; i<=N; i++)
{
scanf("%d%d%d%d", &L[i], &R[i], &P[i], &D[i]);
comp.push_back(L[i]);
comp.push_back(R[i]);
comp.push_back(P[i]);
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
M=comp.size();
for(i=1; i<=N; i++)
{
L[i]=lower_bound(comp.begin(), comp.end(), L[i])-comp.begin()+1;
R[i]=lower_bound(comp.begin(), comp.end(), R[i])-comp.begin()+1;
P[i]=lower_bound(comp.begin(), comp.end(), P[i])-comp.begin()+1;
}
init(1, 1, M);
update(1, 1, M, 1, 0);
for(i=1; i<=N; i++)
{
dp1[i]=query(1, 1, M, L[i], R[i])+D[i];
update(1, 1, M, P[i], dp1[i]);
}
init(1, 1, M);
update(1, 1, M, M, 0);
for(i=1; i<=N; i++)
{
dp2[i]=query(1, 1, M, L[i], R[i])+D[i];
update(1, 1, M, P[i], dp2[i]);
}
ll ans=INF;
for(i=1; i<=N; i++) ans=min(ans, dp1[i]+dp2[i]-D[i]);
if(ans==INF) ans=-1;
printf("%lld\n", ans);
}
Compilation message (stderr)
pinball.cpp: In function 'void init(int, int, int)':
pinball.cpp:20:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
pinball.cpp: In function 'void update(int, int, int, int, ll)':
pinball.cpp:32:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
pinball.cpp: In function 'll query(int, int, int, int, int)':
pinball.cpp:42:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
pinball.cpp: In function 'int main()':
pinball.cpp:50:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
pinball.cpp:52:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
pinball.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &L[i], &R[i], &P[i], &D[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |