Submission #199048

# Submission time Handle Problem Language Result Execution time Memory
199048 2020-01-28T19:00:24 Z popovicirobert Restore Array (RMI19_restore) C++14
100 / 100
125 ms 1400 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define uint unsigned int


using namespace std;

struct Edge {
    int nod, delta, val;
};

int main() {
#ifdef HOME
    ifstream cin("A.in");
    ofstream cout("A.out");
#endif
    int i, n, m;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n >> m;

    vector < vector <Edge> > g(n + 1);
    for(i = 1; i <= m; i++) {
        int l, r, k, val;
        cin >> l >> r >> k >> val;
        l++, r++;
        g[l - 1].push_back({r, r - l + 1 - k + val, val});
        g[r].push_back({l - 1, r - l + 1 - k + val, val});
    }

    for(i = 0; i < n; i++) {
        g[i].push_back({i + 1, 1, 0});
        g[i + 1].push_back({i, 1, 0});
        g[i].push_back({i + 1, 0, 1});
        g[i + 1].push_back({i, 0, 1});
    }

    vector <int> L(n + 1), R(n + 1);
    for(i = 0; i <= n; i++) {
        L[i] = 0, R[i] = i;
    }

    vector <bool> inq(n + 1);
    queue <int> Q;

    for(i = 0; i <= n; i++) {
        Q.push(i);
        inq[i] = 1;
    }

    auto walk = [&](int l, const Edge &e) {
        int r = e.nod;
        if(l > r) {
            swap(l, r);
        }

        pair <int, int> curl = {L[l], R[l]};
        pair <int, int> curr = {L[r], R[r]};

        if(e.val == 0) {
            L[l] = max(L[l], L[r] - e.delta);
            R[r] = min(R[r], R[l] + e.delta);
        }
        else {
            L[r] = max(L[r], L[l] + e.delta);
            R[l] = min(R[l], R[r] - e.delta);
        }

        if(curl.first != L[l] || curl.second != R[l]) {
            if(inq[l] == 0) {
                inq[l] = 1;
                Q.push(l);
            }
        }

        if(curr.first != L[r] || curr.second != R[r]) {
            if(inq[r] == 0) {
                inq[r] = 1;
                Q.push(r);
            }
        }


    };

    while(Q.size()) {
        int nod = Q.front();
        inq[nod] = 0, Q.pop();

        if(L[nod] > R[nod]) {
            cout << -1;
            return 0;
        }

        for(auto &it : g[nod]) {
            walk(nod, it);
        }
    }

    for(i = 1; i <= n; i++) {
        cout << L[i] - L[i - 1] << " ";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 372 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1272 KB Output is correct
2 Correct 11 ms 1272 KB Output is correct
3 Correct 11 ms 1272 KB Output is correct
4 Correct 12 ms 1272 KB Output is correct
5 Correct 10 ms 1272 KB Output is correct
6 Correct 14 ms 1272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1272 KB Output is correct
2 Correct 11 ms 1272 KB Output is correct
3 Correct 11 ms 1272 KB Output is correct
4 Correct 12 ms 1272 KB Output is correct
5 Correct 10 ms 1272 KB Output is correct
6 Correct 14 ms 1272 KB Output is correct
7 Correct 12 ms 1272 KB Output is correct
8 Correct 12 ms 1272 KB Output is correct
9 Correct 12 ms 1400 KB Output is correct
10 Correct 12 ms 1272 KB Output is correct
11 Correct 11 ms 1272 KB Output is correct
12 Correct 11 ms 1272 KB Output is correct
13 Correct 11 ms 1272 KB Output is correct
14 Correct 48 ms 1272 KB Output is correct
15 Correct 95 ms 1272 KB Output is correct
16 Correct 125 ms 1400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 372 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 11 ms 1272 KB Output is correct
12 Correct 11 ms 1272 KB Output is correct
13 Correct 11 ms 1272 KB Output is correct
14 Correct 12 ms 1272 KB Output is correct
15 Correct 10 ms 1272 KB Output is correct
16 Correct 14 ms 1272 KB Output is correct
17 Correct 12 ms 1272 KB Output is correct
18 Correct 12 ms 1272 KB Output is correct
19 Correct 12 ms 1400 KB Output is correct
20 Correct 12 ms 1272 KB Output is correct
21 Correct 11 ms 1272 KB Output is correct
22 Correct 11 ms 1272 KB Output is correct
23 Correct 11 ms 1272 KB Output is correct
24 Correct 48 ms 1272 KB Output is correct
25 Correct 95 ms 1272 KB Output is correct
26 Correct 125 ms 1400 KB Output is correct
27 Correct 15 ms 1400 KB Output is correct
28 Correct 15 ms 1272 KB Output is correct
29 Correct 16 ms 1272 KB Output is correct
30 Correct 15 ms 1272 KB Output is correct
31 Correct 14 ms 1272 KB Output is correct
32 Correct 15 ms 1272 KB Output is correct
33 Correct 11 ms 1272 KB Output is correct
34 Correct 11 ms 1272 KB Output is correct
35 Correct 15 ms 1272 KB Output is correct
36 Correct 15 ms 1276 KB Output is correct