이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<pair<int, int>, null_type, less<pair<int, int>>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
#define endl "\n"
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd  pair<double,double>
#define vdd  vector<pdd>
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
////////////////////Only Clear Code//////////////////////////
void usaco_problem(){
    freopen("milkvisits.in", "r", stdin);
    freopen("milkvisits.out", "w", stdout);
}
void init(){
    #ifndef ONLINE_JUDGE
 
freopen("input.txt", "r", stdin);
 
freopen("output.txt", "w", stdout);
 
#endif // ONLINE_JUDGE
}
const int mx = 3e5+5;
const int LOG = 25;
const ll inf = 1e15;
const ll mod = 1e9+7;
ll dp1[mx], dp2[mx];
int a[mx], b[mx], c[mx], d[mx]; 
int n1;
ll segtree[mx*4], segtree1[mx*4];
map<int, int> mp;
ll query(int node, int qlow, int qhigh, int l, int r){
    if(l >= qlow && r <= qhigh){
        return segtree[node];
    }
    if(r < qlow || l > qhigh){
        return inf;
    }
    int md = (l+r)/2;
    return min(query(node*2, qlow, qhigh, l, md), query(node*2+1, qlow, qhigh, md+1, r));
}
ll query1(int node, int qlow, int qhigh, int l, int r){
    if(l >= qlow && r <= qhigh){
        return segtree1[node];
    }
    if(r < qlow || l > qhigh){
        return inf;
    }
    int md = (l+r)/2;
    return min(query1(node*2, qlow, qhigh, l, md), query1(node*2+1, qlow, qhigh, md+1, r));
}
void update(int ind, ll v, ll u){
    segtree[ind+n1] = min(v, segtree[ind+n1]);
    segtree1[ind+n1] = min(u, segtree1[ind+n1]);
    for(int j = (ind+n1)/2; j >= 1;j/=2){
        segtree[j] = min(segtree[j*2], segtree[j*2+1]);
        segtree1[j] = min(segtree1[j*2], segtree1[j*2+1]);    
    }
}
int cnt(int n){
    int c = 0;
    while(n > 0){
        if(n&1)c++;
        n/=2;
    }
    return c;
}
void run_case(){
    int n, m;cin >> n >> m;
    for(int i = 0;i < n;i++){
        dp1[i] = dp2[i] = inf;
    }
    set<int> s;
    for(int i = 0;i < n;i++){
        cin >> a[i] >> b[i] >> c[i] >> d[i];
        s.insert(a[i]);
        s.insert(b[i]);
        s.insert(c[i]);
    }
    n1 = s.size();
    while(cnt(n1) != 1){
        n1++;
    }
    for(int i = 1;i < 2*n1;i++){
        segtree[i] = segtree1[i] = inf;
    }
    int k = 0;
    for(int x : s){
        mp[x] = k++;
    }
    ll ans = inf;
    for(int i = 0;i < n;i++){
        if(a[i] == 1)dp1[i] = 0;
        if(b[i] == m)dp2[i] = 0;
        dp1[i] = min(dp1[i],query(1, mp[a[i]], mp[b[i]], 0, n1-1));
        dp2[i] = min(dp2[i],query1(1, mp[a[i]], mp[b[i]], 0, n1-1));
        dp1[i] += d[i];
        dp2[i] += d[i];
        update(mp[c[i]], dp1[i], dp2[i]);
        ans = min(ans, dp1[i]+dp2[i]-d[i]);
    }
    cout << (ans >= inf ? -1 : ans) << endl;
}
int main(){
    speed;
    int t;
    //cin >> t;
    t = 1;
    while(t--){
        run_case();
    }
}
/*
    NEVER GIVE UP!
    DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
    Your Guide when stuck:
    - Continue keyword only after reading the whole input
    - Don't use memset with testcases
    - Check for corner cases(n=1, n=0)
    - Check where you declare n(Be careful of declaring it globally and in main)
*/
컴파일 시 표준 에러 (stderr) 메시지
pinball.cpp: In function 'void usaco_problem()':
pinball.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     freopen("milkvisits.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |     freopen("milkvisits.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp: In function 'void init()':
pinball.cpp:40:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 | freopen("input.txt", "r", stdin);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:42:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 | freopen("output.txt", "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... |