이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 7;
const ll oo = 1e18 + 69;
int m, n;
struct item {
int l, r, c, cost;
} a[maxn];
vector<int> val;
struct sub2 {
static const int maxn = 200 + 7;
ll dp[maxn][407][407];
void solve() {
for(int i = 0; i <= n; i++) {
for(int l = 0; l < Size(val); l++) {
for(int r = 0; r < Size(val); r++) dp[i][l][r] = oo;
}
}
for(int i = 0; i < n; i++) {
mini(dp[i + 1][a[i].l][a[i].r], a[i].cost);
for(int l = 0; l < Size(val); l++) {
for(int r = l; r < Size(val); r++) {
if(dp[i][l][r] == oo) continue;
if(val[l] <= a[i].c && a[i].c <= val[r]) {
mini(dp[i + 1][min(l, a[i].l)][max(r, a[i].r)], dp[i][l][r] + a[i].cost);
}
mini(dp[i + 1][l][r], dp[i][l][r]);
}
}
}
ll res = dp[n][0][Size(val) - 1];
cout<<(res == oo ? -1:res);
}
} sub2;
struct sub3 {
static const int maxn = 1e3 + 7;
pair<int, ll> dp[maxn][2 * maxn];
void solve() {
for(int i = 0; i <= n; i++) {
for(int r = 0; r < Size(val); r++) dp[i][r] = {Size(val), oo};
}
for(int i = 0; i < n; i++) {
mini(dp[i + 1][a[i].r], make_pair(a[i].l, (ll) a[i].cost));
for(int r = 0; r < Size(val); r++) {
if(dp[i][r].fi == Size(val)) continue;
int l = dp[i][r].fi;
if(val[l] <= a[i].c && a[i].c <= val[r]) {
auto cur = dp[i][r];
mini(cur.fi, a[i].l);
cur.se += a[i].cost;
mini(dp[i + 1][max(r, a[i].r)], cur);
}
mini(dp[i + 1][r], dp[i][r]);
}
}
auto res = dp[n][Size(val) - 1];
cout<<(res.fi != 0 ? -1:res.se);
}
} sub3;
struct ST {
struct node {
ll mn;
friend node operator + (node a, const node &b) {
mini(a.mn, b.mn);
return a;
}
};
node st[4 * maxn];
void init() {
for(int i = 0; i <= 4 * n; i++) st[i].mn = oo;
}
void update(int id, int l, int r, int pos, ll val) {
if(pos < l || pos > r) return;
if(l == r) {
mini(st[id].mn, val);
return;
}
int mid = l + r >> 1;
update(id << 1, l, mid, pos, val);
update(id << 1 | 1, mid + 1, r, pos, val);
st[id] = st[id << 1] + st[id << 1 | 1];
}
node get(int id, int l, int r, int u, int v) {
if(r < u || l > v) return {oo};
if(u <= l && r <= v) return st[id];
int mid = l + r >> 1;
return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
}
} st;
struct sub4 {
ll dpl[maxn], dpr[maxn];
int id(int x) {
return lower_bound(all(val), x) - val.begin() + 1;
}
void solve() {
for(int i = 0; i < n; i++) {
val.pb(a[i].c);
}
sort(all(val));
val.erase(unique(all(val)), val.end());
st.init();
for(int i = 0; i < n; i++) {
dpl[i] = oo;
if(a[i].l == 1) dpl[i] = a[i].cost;
else {
int l = id(a[i].l);
int r = id(a[i].r + 1) - 1;
if(l <= r) mini(dpl[i], st.get(1, 1, n, l, r).mn + a[i].cost);
}
st.update(1, 1, n, id(a[i].c), dpl[i]);
}
st.init();
ll ans = oo;
for(int i = 0; i < n; i++) {
dpr[i] = oo;
if(a[i].r == m) dpr[i] = a[i].cost;
else {
int l = id(a[i].l);
int r = id(a[i].r + 1) - 1;
if(l <= r) mini(dpr[i], st.get(1, 1, n, l, r).mn + a[i].cost);
}
st.update(1, 1, n, id(a[i].c), dpr[i]);
if(dpl[i] != oo && dpr[i] != oo) {
mini(ans, dpl[i] + dpr[i] - a[i].cost);
}
}
cout<<(ans == oo ? -1:ans);
}
} sub4;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n>>m;
for(int i = 0; i < n; i++) cin>>a[i].l>>a[i].r>>a[i].c>>a[i].cost;
// reverse(a, a + n);
// for(int i = 0; i < n; i++) {
// val.pb(a[i].l);
// val.pb(a[i].r);
// }
// val.pb(1);
// val.pb(m);
// sort(all(val));
// val.erase(unique(all(val)), val.end());
// for(int i = 0; i < n; i++) {
// a[i].l = lower_bound(all(val), a[i].l) - val.begin();
// a[i].r = lower_bound(all(val), a[i].r) - val.begin();
// }
// if(n <= 200) {
// sub2.solve();
// return 0;
// }
// if(n <= 1000) {
// sub3.solve();
// return 0;
// }
sub4.solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
pinball.cpp: In member function 'void ST::update(int, int, int, int, long long int)':
pinball.cpp:108:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
108 | int mid = l + r >> 1;
| ~~^~~
pinball.cpp: In member function 'ST::node ST::get(int, int, int, int, int)':
pinball.cpp:117:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
117 | int mid = l + r >> 1;
| ~~^~~
# | 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... |