Submission #1056333

# Submission time Handle Problem Language Result Execution time Memory
1056333 2024-08-13T08:54:30 Z underwaterkillerwhale Restore Array (RMI19_restore) C++17
0 / 100
5 ms 9308 KB
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(),v.end()

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }

const int N = 1e5 + 7;
const int Mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll BASE = 137;
const int szBL = 320;

struct Query {
    int l, r, k, val;
};

int n, Q;
int a[N];
Query qr[N];

namespace sub3 {
    struct nQuery {
        int r, k;
    };
    int trace[N][2];
    vector<nQuery> vec[N];

    bool check () {
        rep (i, 1, Q) if (qr[i].k != 1 && qr[i].k != qr[i].r - qr[i].l + 1) return 0;
        return 1;
    }
    void solution() {
        rep (i, 1, n) a[i] = 3;
        rep (i, 1, Q) {
            if (qr[i].k == qr[i].r - qr[i].l + 1) {
                if (qr[i].val == 0) {
                    rep (i, qr[i].l, qr[i].r) a[i] = 0;
                }
                else {
                    vec[qr[i].l].pb({qr[i].r, 1});
                }
            }
            else {
                if (qr[i].val == 1) {
                    rep (i, qr[i].l, qr[i].r) if (a[i] == 0) { cout << -1 <<"\n"; return; } else a[i] = 1;
                }
                else {
                    vec[qr[i].l].pb({qr[i].r, 0});
                }
            }
        }
        memset(trace, -1, sizeof trace);
        trace[0][0] = trace[0][1] = 0;
        rep (i, 1, n)
        rep (c, 0, 1) {
            reb (j, i, 1) {
                if (a[j] != 3 && a[j] != c) break;
                int ok = 1;
                iter (&id, vec[j]) {
                    if (id.r > i) continue;
                    if (id.k != c) {
                        ok = 0;
                        break;
                    }
                }
                if (!ok) break;
                if (trace[j - 1][c ^ 1] != -1) {
                    trace[i][c] = j - 1;
                    break;
                }
            }
        }
        int c = -1;
        if (trace[n][0] != -1) c = 0;
        if (trace[n][1] != -1) c = 1;
        if (c == -1) {
            cout << -1 <<"\n";
            return;
        }
        int pos = n;
        while (pos > 0) {
            assert(pos != -1);
            rep (i, trace[pos][c] + 1, pos) {
                a[i] = c;
            }
            pos = trace[pos][c];
            c ^= 1;
        }
        rep (i, 1, n) cout <<a[i] <<" ";
    }

}

void solution () {
    cin >> n >> Q;
    rep (i, 1, Q) {
        cin >>qr[i].l >> qr[i].r >> qr[i].k >> qr[i].val;
        ++qr[i].l;
        ++qr[i].r;
    }
    if (sub3 :: check())
        sub3 :: solution();
    else {
        rep (i, 1, n) a[i] = 3;
        sort (qr + 1, qr + 1 + Q, [] (Query A, Query B) { return A.r < B.r || (A.r == B.r && A.l > B.l); });
        rep (q, 1, Q) {
            Query &cur = qr[q];
            int wanted = cur.val, num;
            if (wanted == 1) num = cur.r - cur.l + 1 - cur.k + 1;
            else num = cur.k;
            rep (i, cur.l, cur.r) {
                if (num == 0) break;
                if (a[i] == 3) a[i] = wanted, --num;
                else {
                    if (a[i] == wanted) --num;
                }
            }
            if (num > 0) {
                cout << -1 <<"\n";
                return;
            }
        }

        rep (i, 1, n) if (a[i] == 3) a[i] = 0;
        rep (i, 1, n) {
            cout << a[i] <<" ";
        }
    }
}

#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
//    file("c");
    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
no bug challenge +2
2 + 8 * 2 - 9
4 5
0 1 1 0
0 2 3 1
2 2 1 0
0 0 1 0
1 2 2 1
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4952 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Incorrect 1 ms 4700 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 9308 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 9308 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4952 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Incorrect 1 ms 4700 KB Output isn't correct
9 Halted 0 ms 0 KB -