Submission #199048

#TimeUsernameProblemLanguageResultExecution timeMemory
199048popovicirobertRestore Array (RMI19_restore)C++14
100 / 100
125 ms1400 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...