이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define pll pair<long long,long long>
#define INF 1000000000
#define LINF 1000000000000000000
#define pb push_back
#define MAXN 100010
using namespace std;
ll a[MAXN], b[MAXN], c[MAXN], d[MAXN];
set<ll> com;
map<ll, int> mapa;
ll dpl[MAXN], dpr[MAXN];
ll seg[12*MAXN][2];
void update(int node, int l, int r, int idx, ll x, int sg){
if(l == r){
seg[node][sg] = x;
return;
}
int mid = (l+r)/2;
if(idx <= mid) update(2*node, l, mid, idx, x, sg);
else update(2*node+1, mid+1, r, idx, x, sg);
seg[node][sg] = min(seg[2*node][sg], seg[2*node+1][sg]);
}
ll query(int node, int l, int r, int L, int R, int sg){
if(L <= l && r <= R){
return seg[node][sg];
}
int mid = (l+r)/2;
ll res = LINF;
if(L <= mid) res = min(res, query(2*node, l, mid, L, R, sg));
if(R > mid) res = min(res, query(2*node+1, mid+1, r, L, R, sg));
return res;
}
int main()
{
for(int i = 0; i < 12*MAXN; i++){
seg[i][0] = LINF;
seg[i][1] = LINF;
}
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int m, n;
cin >> m >> n;
for(int i = 1; i <= m; i++){
cin >> a[i] >> b[i] >> c[i] >> d[i];
com.insert(a[i]);com.insert(b[i]);com.insert(c[i]);
}
int br = 1;
for(auto x : com){
mapa[x] = br++;
}
bool ind = false;
for(int i = 1; i <= m; i++){
if(!ind && b[i] == n) n = mapa[b[i]];
a[i] = mapa[a[i]]; b[i] = mapa[b[i]]; c[i] = mapa[c[i]];
}
for(int i = 1; i <= m; i++){
if(a[i] == 1) dpl[i] = d[i];
else{
dpl[i] = LINF;
dpl[i] = query(1, 1, n, a[i], b[i], 0) + d[i];
}
if(b[i] == n) dpr[i] = d[i];
else{
dpr[i] = LINF;
dpr[i] = query(1, 1, n, a[i], b[i], 1) + d[i];
}
update(1, 1, n, c[i], dpl[i], 0);
update(1, 1, n, c[i], dpr[i], 1);
}
/*for(int i = 1; i <= m; i++){
cout << dpl[i] << ", ";
} cout << endl;
for(int i = 1; i <= m; i++){
cout << dpr[i] << ", ";
} cout << endl;*/
ll res = LINF;
for(int i = 1; i <= m; i++){
res = min(res, dpl[i] + dpr[i] - d[i]);
}
if(res >= LINF) cout << -1 << endl;
else cout << res << endl;
return 0;
}
# | 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... |