답안 #940573

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
940573 2024-03-07T10:51:04 Z Macker Restore Array (RMI19_restore) C++14
100 / 100
106 ms 1368 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
#define all(v) v.begin(), v.end()
#define FOR(i, n) for (int i = 0; i < n; i++)
#define inf LLONG_MAX/2
#define fi first
#define se second

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")

vector<vector<pii>> adj;
vector<int> dist;
int n;

/*
bool SSSP(int s){
    dist.assign(n, inf);
    dist[s] = 0;
    int upd = -1;
    FOR(it, n){
        upd = -1;
        FOR(a, n) for (auto &[b, c] : adj[a]) {
            if(dist[a] == inf) continue;
            if(dist[b] > dist[a] + c){
                dist[b] = dist[a] + c;
                upd = b;
            }
        }
    }
    if(upd != -1) return false;
    else return true;
}
*/

bool spfa(int s) {  /// Shortest Path Faster Algorithm
    vector<bool> inq(n);
    vector<int> cnt(n);
    dist.resize(n);
    for(int i=0; i<n; i++) {
        dist[i] = inf;
        cnt[i] = 0; inq[i] = 0;
    }
    queue<int> q;

    dist[s] = 0;
    q.push(s); inq[s] = 1;
    while (!q.empty()) {
        int v = q.front();
        q.pop(); inq[v] = 0;

        for(auto e : adj[v]) {
            int u = e.fi, w = e.se;
            if(dist[v] + w < dist[u]) {
                dist[u] = dist[v] + w;
                if(dist[u] < 0 ) return false; /// optimization for TLE.
                if(!inq[u]) {
                    q.push(u); inq[u] = 1;
                    cnt[u]++;
                    if(cnt[u]>n) return false;
                }
            }
        }
    }

    return true;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int m; cin >> n >> m;
    adj.resize(n + 2);
    FOR(i, m){
        int l, r, k, val; cin >> l >> r >> k >> val; r++;
        if(val == 0){
            adj[l].push_back({r, (r - l) - k});
        }
        else{
            adj[r].push_back({l, k - r + l - 1});
        }
    }
    FOR(i, n){
        adj[i].push_back({i + 1, 1});
        adj[i + 1].push_back({i, 0});
    }
    n++;
    if(!spfa(0)){
        cout << -1 << endl;
    }
    else{
        FOR(i, n-1){
            if(dist[i] < dist[i + 1]) cout << 1;
            else cout << 0;
            cout << " ";
        }
        cout << endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1116 KB Output is correct
2 Correct 10 ms 1108 KB Output is correct
3 Correct 10 ms 1116 KB Output is correct
4 Correct 9 ms 1116 KB Output is correct
5 Correct 3 ms 1116 KB Output is correct
6 Correct 3 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1116 KB Output is correct
2 Correct 10 ms 1108 KB Output is correct
3 Correct 10 ms 1116 KB Output is correct
4 Correct 9 ms 1116 KB Output is correct
5 Correct 3 ms 1116 KB Output is correct
6 Correct 3 ms 1116 KB Output is correct
7 Correct 12 ms 1116 KB Output is correct
8 Correct 9 ms 1116 KB Output is correct
9 Correct 16 ms 1200 KB Output is correct
10 Correct 8 ms 1116 KB Output is correct
11 Correct 3 ms 1112 KB Output is correct
12 Correct 3 ms 1116 KB Output is correct
13 Correct 9 ms 1116 KB Output is correct
14 Correct 15 ms 1112 KB Output is correct
15 Correct 25 ms 1204 KB Output is correct
16 Correct 106 ms 1180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 10 ms 1116 KB Output is correct
12 Correct 10 ms 1108 KB Output is correct
13 Correct 10 ms 1116 KB Output is correct
14 Correct 9 ms 1116 KB Output is correct
15 Correct 3 ms 1116 KB Output is correct
16 Correct 3 ms 1116 KB Output is correct
17 Correct 12 ms 1116 KB Output is correct
18 Correct 9 ms 1116 KB Output is correct
19 Correct 16 ms 1200 KB Output is correct
20 Correct 8 ms 1116 KB Output is correct
21 Correct 3 ms 1112 KB Output is correct
22 Correct 3 ms 1116 KB Output is correct
23 Correct 9 ms 1116 KB Output is correct
24 Correct 15 ms 1112 KB Output is correct
25 Correct 25 ms 1204 KB Output is correct
26 Correct 106 ms 1180 KB Output is correct
27 Correct 5 ms 1112 KB Output is correct
28 Correct 5 ms 1116 KB Output is correct
29 Correct 5 ms 1116 KB Output is correct
30 Correct 5 ms 1116 KB Output is correct
31 Correct 5 ms 1180 KB Output is correct
32 Correct 5 ms 1116 KB Output is correct
33 Correct 5 ms 1116 KB Output is correct
34 Correct 3 ms 1116 KB Output is correct
35 Correct 6 ms 1368 KB Output is correct
36 Correct 8 ms 1112 KB Output is correct