Submission #680405

# Submission time Handle Problem Language Result Execution time Memory
680405 2023-01-10T17:39:45 Z bashkort Crossing (JOI21_crossing) C++17
100 / 100
742 ms 19404 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[]{1000'000'000 + 7, 228228227};
constexpr int P = 239, hsz = size(MOD);

struct Info {
    array<int, hsz> h{}, power{}, sum{};

    Info() = default;
};

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

void apply(int x, int y) {
    if (y != -1) {
        for (int i = 0; i < hsz; ++i) {
            t[x].h[i] = 1LL * t[x].sum[i] * y % MOD[i];
        }
        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) {
    for (int i = 0; i < hsz; ++i) {
        t[x].h[i] = (t[x << 1].h[i] + 1LL * t[x << 1].power[i] * P % MOD[i] * t[x << 1 | 1].h[i]) % MOD[i];
        t[x].power[i] = 1LL * t[x << 1].power[i] * t[x << 1 | 1].power[i] % MOD[i] * P % MOD[i];
        t[x].sum[i] = (t[x << 1].sum[i] + 1LL * t[x << 1].power[i] * P % MOD[i] * t[x << 1 | 1].sum[i]) % MOD[i];
    }
}

int to_int(char c) {
    return c == 'J' ? 0 : 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) {
        for (int j = 0; j < hsz; ++j) {
            t[i + sz].sum[j] = t[i + sz].power[j] = 1;
            t[i + sz].h[j] = 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);
}

array<int, hsz> hashed(string &s) {
    array<int, hsz> ans{};
    for (int i = int(s.size()) - 1; i >= 0; --i) {
        for (int j = 0; j < hsz; ++j) {
            ans[j] = (1LL * ans[j] * P + to_int(s[i])) % MOD[j];
        }
    }
    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);
            }
        }
    }

    set<array<int, hsz>> 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));

//        assert(t[1].h == hashed(T));
        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:59:23: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   59 |     sz = 1 << __lg(n) + bool(n & (n - 1));
      |               ~~~~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 168 ms 868 KB Output is correct
2 Correct 184 ms 848 KB Output is correct
3 Correct 205 ms 960 KB Output is correct
4 Correct 166 ms 996 KB Output is correct
5 Correct 153 ms 772 KB Output is correct
6 Correct 152 ms 812 KB Output is correct
7 Correct 163 ms 852 KB Output is correct
8 Correct 163 ms 908 KB Output is correct
9 Correct 166 ms 844 KB Output is correct
10 Correct 166 ms 840 KB Output is correct
11 Correct 167 ms 900 KB Output is correct
12 Correct 164 ms 912 KB Output is correct
13 Correct 177 ms 832 KB Output is correct
14 Correct 168 ms 852 KB Output is correct
15 Correct 165 ms 844 KB Output is correct
16 Correct 169 ms 844 KB Output is correct
17 Correct 170 ms 824 KB Output is correct
18 Correct 182 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 168 ms 868 KB Output is correct
2 Correct 184 ms 848 KB Output is correct
3 Correct 205 ms 960 KB Output is correct
4 Correct 166 ms 996 KB Output is correct
5 Correct 153 ms 772 KB Output is correct
6 Correct 152 ms 812 KB Output is correct
7 Correct 163 ms 852 KB Output is correct
8 Correct 163 ms 908 KB Output is correct
9 Correct 166 ms 844 KB Output is correct
10 Correct 166 ms 840 KB Output is correct
11 Correct 167 ms 900 KB Output is correct
12 Correct 164 ms 912 KB Output is correct
13 Correct 177 ms 832 KB Output is correct
14 Correct 168 ms 852 KB Output is correct
15 Correct 165 ms 844 KB Output is correct
16 Correct 169 ms 844 KB Output is correct
17 Correct 170 ms 824 KB Output is correct
18 Correct 182 ms 896 KB Output is correct
19 Correct 698 ms 16720 KB Output is correct
20 Correct 719 ms 16740 KB Output is correct
21 Correct 442 ms 16456 KB Output is correct
22 Correct 443 ms 16404 KB Output is correct
23 Correct 333 ms 1988 KB Output is correct
24 Correct 332 ms 1884 KB Output is correct
25 Correct 470 ms 16508 KB Output is correct
26 Correct 470 ms 16568 KB Output is correct
27 Correct 554 ms 16640 KB Output is correct
28 Correct 586 ms 16660 KB Output is correct
29 Correct 527 ms 16748 KB Output is correct
30 Correct 378 ms 1992 KB Output is correct
31 Correct 553 ms 16552 KB Output is correct
32 Correct 513 ms 16448 KB Output is correct
33 Correct 343 ms 1872 KB Output is correct
34 Correct 534 ms 16572 KB Output is correct
35 Correct 465 ms 16188 KB Output is correct
36 Correct 329 ms 1868 KB Output is correct
37 Correct 339 ms 2004 KB Output is correct
38 Correct 612 ms 16600 KB Output is correct
39 Correct 401 ms 16564 KB Output is correct
40 Correct 439 ms 9272 KB Output is correct
41 Correct 700 ms 16984 KB Output is correct
42 Correct 219 ms 16716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 168 ms 868 KB Output is correct
2 Correct 184 ms 848 KB Output is correct
3 Correct 205 ms 960 KB Output is correct
4 Correct 166 ms 996 KB Output is correct
5 Correct 153 ms 772 KB Output is correct
6 Correct 152 ms 812 KB Output is correct
7 Correct 163 ms 852 KB Output is correct
8 Correct 163 ms 908 KB Output is correct
9 Correct 166 ms 844 KB Output is correct
10 Correct 166 ms 840 KB Output is correct
11 Correct 167 ms 900 KB Output is correct
12 Correct 164 ms 912 KB Output is correct
13 Correct 177 ms 832 KB Output is correct
14 Correct 168 ms 852 KB Output is correct
15 Correct 165 ms 844 KB Output is correct
16 Correct 169 ms 844 KB Output is correct
17 Correct 170 ms 824 KB Output is correct
18 Correct 182 ms 896 KB Output is correct
19 Correct 177 ms 808 KB Output is correct
20 Correct 214 ms 972 KB Output is correct
21 Correct 164 ms 836 KB Output is correct
22 Correct 152 ms 752 KB Output is correct
23 Correct 164 ms 908 KB Output is correct
24 Correct 156 ms 816 KB Output is correct
25 Correct 179 ms 844 KB Output is correct
26 Correct 161 ms 776 KB Output is correct
27 Correct 164 ms 888 KB Output is correct
28 Correct 148 ms 752 KB Output is correct
29 Correct 167 ms 844 KB Output is correct
30 Correct 152 ms 772 KB Output is correct
31 Correct 169 ms 904 KB Output is correct
32 Correct 165 ms 892 KB Output is correct
33 Correct 166 ms 888 KB Output is correct
34 Correct 154 ms 876 KB Output is correct
35 Correct 167 ms 812 KB Output is correct
36 Correct 169 ms 892 KB Output is correct
37 Correct 164 ms 1100 KB Output is correct
38 Correct 164 ms 892 KB Output is correct
39 Correct 166 ms 900 KB Output is correct
40 Correct 166 ms 864 KB Output is correct
41 Correct 164 ms 920 KB Output is correct
42 Correct 164 ms 904 KB Output is correct
43 Correct 160 ms 908 KB Output is correct
44 Correct 169 ms 936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 168 ms 868 KB Output is correct
2 Correct 184 ms 848 KB Output is correct
3 Correct 205 ms 960 KB Output is correct
4 Correct 166 ms 996 KB Output is correct
5 Correct 153 ms 772 KB Output is correct
6 Correct 152 ms 812 KB Output is correct
7 Correct 163 ms 852 KB Output is correct
8 Correct 163 ms 908 KB Output is correct
9 Correct 166 ms 844 KB Output is correct
10 Correct 166 ms 840 KB Output is correct
11 Correct 167 ms 900 KB Output is correct
12 Correct 164 ms 912 KB Output is correct
13 Correct 177 ms 832 KB Output is correct
14 Correct 168 ms 852 KB Output is correct
15 Correct 165 ms 844 KB Output is correct
16 Correct 169 ms 844 KB Output is correct
17 Correct 170 ms 824 KB Output is correct
18 Correct 182 ms 896 KB Output is correct
19 Correct 698 ms 16720 KB Output is correct
20 Correct 719 ms 16740 KB Output is correct
21 Correct 442 ms 16456 KB Output is correct
22 Correct 443 ms 16404 KB Output is correct
23 Correct 333 ms 1988 KB Output is correct
24 Correct 332 ms 1884 KB Output is correct
25 Correct 470 ms 16508 KB Output is correct
26 Correct 470 ms 16568 KB Output is correct
27 Correct 554 ms 16640 KB Output is correct
28 Correct 586 ms 16660 KB Output is correct
29 Correct 527 ms 16748 KB Output is correct
30 Correct 378 ms 1992 KB Output is correct
31 Correct 553 ms 16552 KB Output is correct
32 Correct 513 ms 16448 KB Output is correct
33 Correct 343 ms 1872 KB Output is correct
34 Correct 534 ms 16572 KB Output is correct
35 Correct 465 ms 16188 KB Output is correct
36 Correct 329 ms 1868 KB Output is correct
37 Correct 339 ms 2004 KB Output is correct
38 Correct 612 ms 16600 KB Output is correct
39 Correct 401 ms 16564 KB Output is correct
40 Correct 439 ms 9272 KB Output is correct
41 Correct 700 ms 16984 KB Output is correct
42 Correct 219 ms 16716 KB Output is correct
43 Correct 177 ms 808 KB Output is correct
44 Correct 214 ms 972 KB Output is correct
45 Correct 164 ms 836 KB Output is correct
46 Correct 152 ms 752 KB Output is correct
47 Correct 164 ms 908 KB Output is correct
48 Correct 156 ms 816 KB Output is correct
49 Correct 179 ms 844 KB Output is correct
50 Correct 161 ms 776 KB Output is correct
51 Correct 164 ms 888 KB Output is correct
52 Correct 148 ms 752 KB Output is correct
53 Correct 167 ms 844 KB Output is correct
54 Correct 152 ms 772 KB Output is correct
55 Correct 169 ms 904 KB Output is correct
56 Correct 165 ms 892 KB Output is correct
57 Correct 166 ms 888 KB Output is correct
58 Correct 154 ms 876 KB Output is correct
59 Correct 167 ms 812 KB Output is correct
60 Correct 169 ms 892 KB Output is correct
61 Correct 164 ms 1100 KB Output is correct
62 Correct 164 ms 892 KB Output is correct
63 Correct 166 ms 900 KB Output is correct
64 Correct 166 ms 864 KB Output is correct
65 Correct 164 ms 920 KB Output is correct
66 Correct 164 ms 904 KB Output is correct
67 Correct 160 ms 908 KB Output is correct
68 Correct 169 ms 936 KB Output is correct
69 Correct 621 ms 18320 KB Output is correct
70 Correct 742 ms 19148 KB Output is correct
71 Correct 327 ms 2012 KB Output is correct
72 Correct 322 ms 1940 KB Output is correct
73 Correct 330 ms 1968 KB Output is correct
74 Correct 425 ms 16888 KB Output is correct
75 Correct 332 ms 1856 KB Output is correct
76 Correct 465 ms 16928 KB Output is correct
77 Correct 467 ms 16600 KB Output is correct
78 Correct 322 ms 1812 KB Output is correct
79 Correct 333 ms 1872 KB Output is correct
80 Correct 479 ms 17892 KB Output is correct
81 Correct 331 ms 1928 KB Output is correct
82 Correct 513 ms 19336 KB Output is correct
83 Correct 498 ms 19092 KB Output is correct
84 Correct 336 ms 2176 KB Output is correct
85 Correct 331 ms 1996 KB Output is correct
86 Correct 557 ms 18128 KB Output is correct
87 Correct 580 ms 18996 KB Output is correct
88 Correct 356 ms 2032 KB Output is correct
89 Correct 527 ms 18556 KB Output is correct
90 Correct 572 ms 19200 KB Output is correct
91 Correct 357 ms 1996 KB Output is correct
92 Correct 461 ms 18100 KB Output is correct
93 Correct 327 ms 1944 KB Output is correct
94 Correct 326 ms 1968 KB Output is correct
95 Correct 329 ms 2020 KB Output is correct
96 Correct 605 ms 16796 KB Output is correct
97 Correct 442 ms 19188 KB Output is correct
98 Correct 472 ms 10756 KB Output is correct
99 Correct 742 ms 19404 KB Output is correct
100 Correct 267 ms 19156 KB Output is correct