Submission #1302420

#TimeUsernameProblemLanguageResultExecution timeMemory
1302420ArtTowers (NOI22_towers)C++20
0 / 100
24 ms18772 KiB
///     - Art -
#pragma GCC optimize("O3,unroll-loops") // O2
#include <bits/stdc++.h>

#define el              cout << '\n'

#define MASK(x)         (1 << (x))

#define FOR(i, a, b)    for (int i = (a), _b = (b); i <= _b; ++i)
#define REV(i, b, a)    for (int i = (b), _a = (a); i >= _a; --i)
#define REP(i, c)       for (int i = 0, _c = (c); i < _c; ++i)

const int N = 1e5 + 7;

using namespace std;

int n, m, s, r;
int y[N], x[N];
string op[N];

namespace subtask_2 {

    bool vis[N];
    int a[2 * N];

    struct SparseTable {
        int op(int &a, int &b, bool type) {
            if (type == 0) {
                return a & b;
            }
            return a | b;
        }

        int n, LOG;
        bool tp;
        vector<vector<int>> st;

        SparseTable(int _n = 0, int _tp = 0) {
            n = _n; tp = _tp;
            LOG = __lg(2 * _n);
            st.assign(_n + 7, vector<int>(LOG + 2, 0));

            REP (i, n) {
                st[i][0] = a[i];
            }
//            cout << LOG << ' ' << n, el;
            FOR (j, 1, LOG) REP (i, _n - MASK(j) + 1) {
                st[i][j] = op(st[i][j - 1], st[i + MASK(j - 1)][j - 1], tp);
            }
        }

        int get(int l, int r) {

            int k = 31 - __builtin_clz(r - l + 1);
            return op(st[l][k], st[r - MASK(k) + 1][k], tp);
        }
    };

    int pre[2 * N];

    void solve() {
        REP (sta, n) if (!vis[sta]) {
            vector<int> cycle;
            for (int idx = sta; !vis[idx]; idx = (idx + s) % n) {
                cycle.emplace_back(idx); vis[idx] = 1;
//                cout << idx << ' ';
            }
            int sz = (int)cycle.size();
            REP (i, 2 * sz) {
                a[i] = x[cycle[i % sz]];
            }
//            cout << sz, el;

            if (op[0] == "xor") {
                pre[0] = a[0];
                FOR (i, 1, 2 * sz - 1) {
                    pre[i] = pre[i - 1] ^ a[i - 1];
                }
                int turn = r / sz;
                int remain = r % sz;
                REP (i, sz) {
                    if (turn & 1) {
                        y[cycle[i]] = x[cycle[i]] ^ pre[2 * sz - 1];
                    }
                    if (remain > 0) {
                        y[cycle[i]] = x[cycle[i]] ^ pre[i + remain + 1] ^ pre[i + 1];
                    }
                }
            }
            else if (op[0] == "and") {
                if (r >= sz) {
                    int AND = a[0];
                    FOR (i, 1, sz - 1) {
                        AND &= a[i];
                    }
                    for (int &i : cycle) {
                        y[i] = AND;
                    }
                }
                else {
                    SparseTable st(sz, 0);
                    REP (i, sz) {
                        y[cycle[i]] = st.get(i, i + r);
                    }
                }
            }
            else {
                if (r >= sz) {
                    int OR = a[0];
                    FOR (i, 1, sz - 1) {
                        OR |= a[i];
                    }
                    for (int &i : cycle) {
                        y[i] = OR;
                    }
                }
                else {
                    SparseTable st(sz, 1);
                    REP (i, sz) {
                        y[cycle[i]] = st.get(i, i + r);
                    }
                }
            }
        }

        REP (i, n) {
            cout << y[i] << ' ';
        } el;
    }
}

int main() {

    #define name "andorxor"
    if (fopen(name".inp", "r")) {
        freopen(name".inp", "r", stdin);
        freopen(name".out", "w", stdout);
    }

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m >> s >> r;
    REP (i, n) {
        int c; cin >> c;
        y[i] = x[i] = c;
    }
    REP (i, m) {
        cin >> op[i];
    }

    if (m == 1) {
        subtask_2::solve(); return 0;
    }

    int lazy = 0;
    while (r--) {
        REP (j, m) {
            lazy = (lazy + s) % n;
            REP (i, n) {
                int v = x[(i + lazy) % n];
                if (op[j] == "xor") {
                    y[i] ^= v;
                }
                else if (op[j] == "and") {
                    y[i] &= v;
                }
                else {
                    y[i] |= v;
                }
            }
        }
//        REP (i, n) {
//            cout << (i + lazy) % n << ' ';
//        } el;
    }
    REP (i, n) {
        cout << y[i] << ' ';
    } el;

    return 0;
}

/*
5 3 3 10
0 1 0 1 0
xor and or

6 3 2 2
1 4 2 5 3 6
and or xor
*/

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  137 |         freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...