Submission #332271

# Submission time Handle Problem Language Result Execution time Memory
332271 2020-12-01T20:21:15 Z 2fat2code Restore Array (RMI19_restore) C++17
100 / 100
479 ms 1512 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define fr first
#define sc second
#define int long long
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)

// restore array rmi 2019

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int nmax = 5005;

int n, m, dist[nmax];

struct item{
    int l, r, val;
};

vector <item> edges;

void add_edge(int l, int r, int k, int val){
    if(val == 0){
        int curr = (r - l + 1) - k;
        edges.push_back({l - 1, r, curr});
    }
    else{
        edges.push_back({r, l - 1,-(r - l + 1 - (k - 1))});
    }
}

bool bellman_ford(){
    for(int i=1;i<=n+1;i++){
        for(auto it : edges){
            if(dist[it.r] > dist[it.l] + it.val){
                dist[it.r] = dist[it.l] + it.val;
            }
        }
    }
    for(auto it : edges){
        if(dist[it.r] > dist[it.l] + it.val){
            return false;
        }
    }
    return true;
}

int32_t main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
   // freopen("input.in", "r", stdin);
    cin >> n >> m;
    for(int i=1;i<=n;i++){
        dist[i] = 1e18;
    }
    for(int i=0;i<n;i++){
        edges.push_back({i, i+1, 1});
    }
    for(int i=1;i<=n;i++){
        edges.push_back({i, i - 1, 0});
    }
    for(int i=1;i<=m;i++){
        int l, r, k, val;
        cin >> l >> r >> k >> val;
        ++l; ++r;
        add_edge(l, r, k, val);
    }
    if(bellman_ford() == false){
        rc(-1);
    }
    else{
        for(int i=1;i<=n;i++){
            cout << dist[i] - dist[i - 1] << ' ';
        }
        return 0;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 140 ms 1256 KB Output is correct
2 Correct 142 ms 1256 KB Output is correct
3 Correct 148 ms 1256 KB Output is correct
4 Correct 142 ms 1256 KB Output is correct
5 Correct 460 ms 1384 KB Output is correct
6 Correct 462 ms 1384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 140 ms 1256 KB Output is correct
2 Correct 142 ms 1256 KB Output is correct
3 Correct 148 ms 1256 KB Output is correct
4 Correct 142 ms 1256 KB Output is correct
5 Correct 460 ms 1384 KB Output is correct
6 Correct 462 ms 1384 KB Output is correct
7 Correct 143 ms 1384 KB Output is correct
8 Correct 148 ms 1384 KB Output is correct
9 Correct 177 ms 1512 KB Output is correct
10 Correct 139 ms 1384 KB Output is correct
11 Correct 445 ms 1256 KB Output is correct
12 Correct 461 ms 1256 KB Output is correct
13 Correct 137 ms 1384 KB Output is correct
14 Correct 145 ms 1384 KB Output is correct
15 Correct 160 ms 1512 KB Output is correct
16 Correct 160 ms 1384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 140 ms 1256 KB Output is correct
12 Correct 142 ms 1256 KB Output is correct
13 Correct 148 ms 1256 KB Output is correct
14 Correct 142 ms 1256 KB Output is correct
15 Correct 460 ms 1384 KB Output is correct
16 Correct 462 ms 1384 KB Output is correct
17 Correct 143 ms 1384 KB Output is correct
18 Correct 148 ms 1384 KB Output is correct
19 Correct 177 ms 1512 KB Output is correct
20 Correct 139 ms 1384 KB Output is correct
21 Correct 445 ms 1256 KB Output is correct
22 Correct 461 ms 1256 KB Output is correct
23 Correct 137 ms 1384 KB Output is correct
24 Correct 145 ms 1384 KB Output is correct
25 Correct 160 ms 1512 KB Output is correct
26 Correct 160 ms 1384 KB Output is correct
27 Correct 140 ms 1384 KB Output is correct
28 Correct 142 ms 1384 KB Output is correct
29 Correct 150 ms 1512 KB Output is correct
30 Correct 144 ms 1384 KB Output is correct
31 Correct 137 ms 1384 KB Output is correct
32 Correct 157 ms 1436 KB Output is correct
33 Correct 479 ms 1384 KB Output is correct
34 Correct 460 ms 1384 KB Output is correct
35 Correct 141 ms 1384 KB Output is correct
36 Correct 142 ms 1384 KB Output is correct