Submission #680391

# Submission time Handle Problem Language Result Execution time Memory
680391 2023-01-10T17:21:45 Z bashkort Crossing (JOI21_crossing) C++17
0 / 100
241 ms 1044 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

string cross(string &a, string &b) {
    string res(a.size(), 'a');
    for (int i = 0; i < a.size(); ++i) {
        if (a[i] == b[i]) {
            res[i] = a[i];
        } else {
            res[i] = 'J' ^ 'O' ^ 'I' ^ a[i] ^ b[i];
        }
    }
    return res;
}

constexpr int MOD = 1e9 + 9;
constexpr int P = 239;

constexpr int N = 2e5 + 1;

struct Info {
    int h, power, sum;

    Info() = default;
    Info(int a, int b) : h(a), power(b) {}
};

vector<Info> t;
vector<int> tag;
int sz;

void apply(int x, int y) {
    if (y != -1) {
        t[x].h = 1LL * t[x].sum * y % MOD;
        tag[x] = y;
    }
}

void push(int x) {
    apply(x << 1, tag[x]);
    apply(x << 1 | 1, tag[x]);
    tag[x] = -1;
}

void pull(int x) {
    t[x].h = (t[x << 1].h + 1LL * t[x << 1].power * P % MOD * t[x << 1 | 1].h) % MOD;
    t[x].power = 1LL * t[x << 1].power * t[x << 1 | 1].power % MOD * P % MOD;
    t[x].sum = (t[x << 1].sum + 1LL * t[x << 1].power * P % MOD * t[x << 1 | 1].sum) % MOD;
}

int to_int(char c) {
    return c == 'J' ? 1 : 1 + (c == 'O');
}

void init(int n, string T) {
    sz = 1 << __lg(n) + bool(n & (n - 1));
    tag.assign(sz << 1, -1), t.resize(sz << 1);

    for (int i = 0; i < n; ++i) {
        t[i + sz].sum = t[i + sz].power = 1;
        t[i + sz].h = to_int(T[i]);
    }
    for (int i = sz - 1; i > 0; --i) {
        pull(i);
    }
}

void rangeApply(int l, int r, int tg, int x = 1, int lx = 0, int rx = sz) {
    if (l >= rx || lx >= r) {
        return;
    }
    if (l <= lx && rx <= r) {
        apply(x, tg);
        return;
    }

    push(x);

    int mid = (lx + rx) >> 1;

    rangeApply(l, r, tg, x << 1, lx, mid), rangeApply(l, r, tg, x << 1 | 1, mid, rx);

    pull(x);
}

int hashed(string &s) {
    int ans = 0;
    for (int i = int(s.size()) - 1; i >= 0; --i) {
        ans = (1LL * ans * P + to_int(s[i])) % MOD;
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    string A, B, C;
    cin >> A >> B >> C;

    unordered_set<string> every{A, B, C};

    queue<string> qu;
    qu.push(A), qu.push(B), qu.push(C);

    while (!qu.empty()) {
        string s = qu.front();
        qu.pop();

        for (string to : every) {
            string res = cross(s, to);

            if (!every.count(res)) {
                qu.push(res);
                every.insert(res);
            }
        }
    }

    unordered_set<int> st;
    for (string s : every) {
        st.insert(hashed(s));
    }

    int q;
    cin >> q;

    string T;
    cin >> T;

    init(n, T);

    cout << (st.count(t[1].h) ? "Yes\n" : "No\n");

    while (q--) {
        int l, r;
        char c;
        cin >> l >> r >> c;

        rangeApply(l - 1, r, to_int(c));
        for (int i = l - 1; i < r; ++i) {
            T[i] = c;
        }

        assert(t[1].h == hashed(T));
        assert(every.count(T) == st.count(t[1].h));
        cout << (st.count(t[1].h) ? "Yes\n" : "No\n");
    }

    return 0;
}

Compilation message

Main.cpp: In function 'std::string cross(std::string&, std::string&)':
Main.cpp:8:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 |     for (int i = 0; i < a.size(); ++i) {
      |                     ~~^~~~~~~~~~
Main.cpp: In function 'void init(int, std::string)':
Main.cpp:58:23: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   58 |     sz = 1 << __lg(n) + bool(n & (n - 1));
      |               ~~~~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 160 ms 764 KB Output is correct
2 Correct 202 ms 876 KB Output is correct
3 Correct 241 ms 1044 KB Output is correct
4 Runtime error 2 ms 468 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 160 ms 764 KB Output is correct
2 Correct 202 ms 876 KB Output is correct
3 Correct 241 ms 1044 KB Output is correct
4 Runtime error 2 ms 468 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 160 ms 764 KB Output is correct
2 Correct 202 ms 876 KB Output is correct
3 Correct 241 ms 1044 KB Output is correct
4 Runtime error 2 ms 468 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 160 ms 764 KB Output is correct
2 Correct 202 ms 876 KB Output is correct
3 Correct 241 ms 1044 KB Output is correct
4 Runtime error 2 ms 468 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -