Submission #744305

# Submission time Handle Problem Language Result Execution time Memory
744305 2023-05-18T11:15:06 Z GrindMachine Crossing (JOI21_crossing) C++17
100 / 100
806 ms 27664 KB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*



*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

vector<int> ps = {5791, 8237, 3067, 6991, 3527};
vector<int> mods = {791139137, 433567597, 271149607, 561259969, 222708581};
ll pows[N][2], pow_sum[N][2];

void precalc() {
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

    shuffle(all(ps), rng);
    shuffle(all(mods), rng);

    rep(j, 2) {
        pows[0][j] = pow_sum[0][j] = 1;
        rep1(i, N - 1) {
            pows[i][j] = pows[i - 1][j] * ps[j] % mods[j];
            pow_sum[i][j] = (pow_sum[i - 1][j] + pows[i][j]) % mods[j];
        }
    }
}

struct Hash {
    ll a[2];

    Hash() {
        memset(a, 0, sizeof a);
    }

    Hash(ll x, ll y) {
        a[0] = x, a[1] = y;
    }

    Hash operator+(char ch) {
        Hash b;
        ll x = ch - 'a' + 1;

        rep(j, 2) {
            b.a[j] = (a[j] * ps[j] + x) % mods[j];
        }

        return b;
    }

    Hash operator+(ll x) {
        Hash b;

        rep(j, 2) {
            b.a[j] = (a[j] * ps[j] + x) % mods[j];
        }

        return b;
    }

    Hash operator+(Hash h) {
        Hash b;
        rep(j, 2) {
            b.a[j] = (a[j] + h.a[j]) % mods[j];
        }

        return b;
    }

    Hash operator-(Hash h) {
        Hash b;
        rep(j, 2) {
            b.a[j] = (a[j] - h.a[j] + mods[j]) % mods[j];
        }

        return b;
    }

    Hash poww(ll k) {
        Hash b;
        rep(j, 2) {
            b.a[j] = a[j] * pows[k][j] % mods[j];
        }

        return b;
    }

    bool operator==(Hash h) {
        rep(j, 2) {
            if (a[j] != h.a[j]) {
                return false;
            }
        }

        return true;
    }

    bool operator<(const Hash &h) const {
        rep(j, 2) {
            if (a[j] != h.a[j]) {
                return a[j] < h.a[j];
            }
        }

        return false;
    }
};

void print(Hash hsh) {
    cout << "[ ";
    rep(j, 2) cout << hsh.a[j] << " ";
    cout << "]";
}

template<typename T>
struct lazysegtree {
    /*=======================================================*/

    struct data {
        Hash hsh;
        ll cnt = 0;

        data() {
            cnt = 0;
        }
    };

    struct lazy {
        char ch;

        lazy() {
            ch = '?';
        }
    };

    data d_neutral = data();
    lazy l_neutral = lazy();

    void merge(data &curr, data &left, data &right) {
        curr.hsh = left.hsh.poww(right.cnt) + right.hsh;
        curr.cnt = left.cnt + right.cnt;
    }

    void create(int x, int lx, int rx, T ch) {
        ll v = 0;
        if (ch == 'J') v = 1;
        else if (ch == 'O') v = 2;
        else v = 3;

        tr[x].hsh = Hash(v, v);
        tr[x].cnt = 1;
    }

    void modify(int x, int lx, int rx, T v) {
        lz[x].ch = v;
    }

    void propagate(int x, int lx, int rx) {
        char ch = lz[x].ch;
        if (ch == '?') return;

        ll v = 0;
        if (ch == 'J') v = 1;
        else if (ch == 'O') v = 2;
        else v = 3;

        ll guys = tr[x].cnt;

        ll hsh1 = v * pow_sum[guys - 1][0] % mods[0];
        ll hsh2 = v * pow_sum[guys - 1][1] % mods[1];
        tr[x].hsh = Hash(hsh1, hsh2);

        if (rx - lx > 1) {
            lz[2 * x + 1].ch = ch;
            lz[2 * x + 2].ch = ch;
        }

        lz[x] = l_neutral;
    }

    /*=======================================================*/

    int siz = 1;
    vector<data> tr;
    vector<lazy> lz;

    lazysegtree() {

    }

    lazysegtree(int n) {
        while (siz < n) siz *= 2;
        tr.assign(2 * siz, d_neutral);
        lz.assign(2 * siz, l_neutral);
    }

    void build(string &a, int n, int x, int lx, int rx) {
        if (rx - lx == 1) {
            if (lx < n) {
                create(x, lx, rx, a[lx]);
            }

            return;
        }

        int mid = (lx + rx) / 2;

        build(a, n, 2 * x + 1, lx, mid);
        build(a, n, 2 * x + 2, mid, rx);

        merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
    }

    void build(string &a, int n) {
        build(a, n, 0, 0, siz);
    }

    void rupd(int l, int r, T v, int x, int lx, int rx) {
        propagate(x, lx, rx);

        if (lx >= r or rx <= l) return;
        if (lx >= l and rx <= r) {
            modify(x, lx, rx, v);
            propagate(x, lx, rx);
            return;
        }

        int mid = (lx + rx) / 2;

        rupd(l, r, v, 2 * x + 1, lx, mid);
        rupd(l, r, v, 2 * x + 2, mid, rx);

        merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
    }

    void rupd(int l, int r, T v) {
        rupd(l, r + 1, v, 0, 0, siz);
    }

    data query(int l, int r, int x, int lx, int rx) {
        propagate(x, lx, rx);

        if (lx >= r or rx <= l) return d_neutral;
        if (lx >= l and rx <= r) return tr[x];

        int mid = (lx + rx) / 2;

        data curr;
        data left = query(l, r, 2 * x + 1, lx, mid);
        data right = query(l, r, 2 * x + 2, mid, rx);

        merge(curr, left, right);
        return curr;
    }

    data query(int l, int r) {
        return query(l, r + 1, 0, 0, siz);
    }
};

string get(string s1, string s2) {
    string s3 = "";
    rep(i, sz(s1)) {
        if (s1[i] == s2[i]) {
            s3.pb(s1[i]);
        }
        else {
            string joi = "JOI";
            for (auto ch : joi) {
                if (s1[i] != ch and s2[i] != ch) {
                    s3.pb(ch);
                }
            }
        }
    }

    return s3;
}

set<string> st;
string s1, s2, s3;

void go(string s) {
    if (st.count(s)) return;
    st.insert(s);

    go(get(s, s1));
    go(get(s, s2));
    go(get(s, s3));
}

void solve(int test_case)
{
    precalc();

    ll n; cin >> n;
    cin >> s1 >> s2 >> s3;

    for (auto s : {s1, s2, s3}) {
        go(s);
    }

    assert(sz(st) <= 9);

    set<Hash> hashes;

    trav(s, st) {
        Hash h;
        trav(ch, s) {
            ll v = 0;
            if (ch == 'J') v = 1;
            else if (ch == 'O') v = 2;
            else v = 3;

            h = h + v;
        }

        hashes.insert(h);
    }

    ll q; cin >> q;
    string t; cin >> t;
    t = "$" + t;

    lazysegtree<char> st(n + 5);
    st.build(t, n + 1);

    {
        Hash hsh = st.query(1, n).hsh;
        if (hashes.count(hsh)) yes;
        else no;
    }

    while (q--) {
        ll l, r; cin >> l >> r;
        char ch; cin >> ch;
        st.rupd(l, r, ch);

        Hash hsh = st.query(1, n).hsh;
        if (hashes.count(hsh)) yes;
        else no;
    }
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}

Compilation message

Main.cpp: In function 'std::string get(std::string, std::string)':
Main.cpp:30:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 | #define rep(i, n) for(int i = 0; i < n; ++i)
      |                                    ^
Main.cpp:315:5: note: in expansion of macro 'rep'
  315 |     rep(i, sz(s1)) {
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 201 ms 5328 KB Output is correct
2 Correct 204 ms 5524 KB Output is correct
3 Correct 226 ms 5452 KB Output is correct
4 Correct 180 ms 5408 KB Output is correct
5 Correct 183 ms 5452 KB Output is correct
6 Correct 183 ms 5416 KB Output is correct
7 Correct 187 ms 5464 KB Output is correct
8 Correct 199 ms 5492 KB Output is correct
9 Correct 196 ms 5564 KB Output is correct
10 Correct 204 ms 5560 KB Output is correct
11 Correct 189 ms 5536 KB Output is correct
12 Correct 203 ms 5540 KB Output is correct
13 Correct 200 ms 5508 KB Output is correct
14 Correct 190 ms 5616 KB Output is correct
15 Correct 194 ms 5512 KB Output is correct
16 Correct 204 ms 5528 KB Output is correct
17 Correct 193 ms 5560 KB Output is correct
18 Correct 217 ms 5428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 5328 KB Output is correct
2 Correct 204 ms 5524 KB Output is correct
3 Correct 226 ms 5452 KB Output is correct
4 Correct 180 ms 5408 KB Output is correct
5 Correct 183 ms 5452 KB Output is correct
6 Correct 183 ms 5416 KB Output is correct
7 Correct 187 ms 5464 KB Output is correct
8 Correct 199 ms 5492 KB Output is correct
9 Correct 196 ms 5564 KB Output is correct
10 Correct 204 ms 5560 KB Output is correct
11 Correct 189 ms 5536 KB Output is correct
12 Correct 203 ms 5540 KB Output is correct
13 Correct 200 ms 5508 KB Output is correct
14 Correct 190 ms 5616 KB Output is correct
15 Correct 194 ms 5512 KB Output is correct
16 Correct 204 ms 5528 KB Output is correct
17 Correct 193 ms 5560 KB Output is correct
18 Correct 217 ms 5428 KB Output is correct
19 Correct 762 ms 21704 KB Output is correct
20 Correct 693 ms 21656 KB Output is correct
21 Correct 513 ms 21292 KB Output is correct
22 Correct 510 ms 21260 KB Output is correct
23 Correct 380 ms 7292 KB Output is correct
24 Correct 412 ms 7300 KB Output is correct
25 Correct 549 ms 21716 KB Output is correct
26 Correct 566 ms 21736 KB Output is correct
27 Correct 639 ms 21768 KB Output is correct
28 Correct 604 ms 21840 KB Output is correct
29 Correct 572 ms 21936 KB Output is correct
30 Correct 382 ms 7280 KB Output is correct
31 Correct 552 ms 21792 KB Output is correct
32 Correct 497 ms 21556 KB Output is correct
33 Correct 372 ms 7188 KB Output is correct
34 Correct 564 ms 21772 KB Output is correct
35 Correct 467 ms 21028 KB Output is correct
36 Correct 374 ms 7352 KB Output is correct
37 Correct 366 ms 7192 KB Output is correct
38 Correct 657 ms 21552 KB Output is correct
39 Correct 440 ms 21752 KB Output is correct
40 Correct 504 ms 21384 KB Output is correct
41 Correct 660 ms 21976 KB Output is correct
42 Correct 556 ms 21196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 5328 KB Output is correct
2 Correct 204 ms 5524 KB Output is correct
3 Correct 226 ms 5452 KB Output is correct
4 Correct 180 ms 5408 KB Output is correct
5 Correct 183 ms 5452 KB Output is correct
6 Correct 183 ms 5416 KB Output is correct
7 Correct 187 ms 5464 KB Output is correct
8 Correct 199 ms 5492 KB Output is correct
9 Correct 196 ms 5564 KB Output is correct
10 Correct 204 ms 5560 KB Output is correct
11 Correct 189 ms 5536 KB Output is correct
12 Correct 203 ms 5540 KB Output is correct
13 Correct 200 ms 5508 KB Output is correct
14 Correct 190 ms 5616 KB Output is correct
15 Correct 194 ms 5512 KB Output is correct
16 Correct 204 ms 5528 KB Output is correct
17 Correct 193 ms 5560 KB Output is correct
18 Correct 217 ms 5428 KB Output is correct
19 Correct 207 ms 5376 KB Output is correct
20 Correct 216 ms 5492 KB Output is correct
21 Correct 227 ms 5676 KB Output is correct
22 Correct 181 ms 5344 KB Output is correct
23 Correct 197 ms 5580 KB Output is correct
24 Correct 179 ms 5520 KB Output is correct
25 Correct 203 ms 5632 KB Output is correct
26 Correct 180 ms 5412 KB Output is correct
27 Correct 203 ms 5580 KB Output is correct
28 Correct 167 ms 5284 KB Output is correct
29 Correct 194 ms 5536 KB Output is correct
30 Correct 166 ms 5444 KB Output is correct
31 Correct 234 ms 5560 KB Output is correct
32 Correct 201 ms 5524 KB Output is correct
33 Correct 221 ms 5576 KB Output is correct
34 Correct 181 ms 5388 KB Output is correct
35 Correct 194 ms 5572 KB Output is correct
36 Correct 198 ms 5516 KB Output is correct
37 Correct 198 ms 5496 KB Output is correct
38 Correct 207 ms 5464 KB Output is correct
39 Correct 196 ms 5484 KB Output is correct
40 Correct 202 ms 5776 KB Output is correct
41 Correct 196 ms 5484 KB Output is correct
42 Correct 198 ms 5480 KB Output is correct
43 Correct 194 ms 5452 KB Output is correct
44 Correct 201 ms 5548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 5328 KB Output is correct
2 Correct 204 ms 5524 KB Output is correct
3 Correct 226 ms 5452 KB Output is correct
4 Correct 180 ms 5408 KB Output is correct
5 Correct 183 ms 5452 KB Output is correct
6 Correct 183 ms 5416 KB Output is correct
7 Correct 187 ms 5464 KB Output is correct
8 Correct 199 ms 5492 KB Output is correct
9 Correct 196 ms 5564 KB Output is correct
10 Correct 204 ms 5560 KB Output is correct
11 Correct 189 ms 5536 KB Output is correct
12 Correct 203 ms 5540 KB Output is correct
13 Correct 200 ms 5508 KB Output is correct
14 Correct 190 ms 5616 KB Output is correct
15 Correct 194 ms 5512 KB Output is correct
16 Correct 204 ms 5528 KB Output is correct
17 Correct 193 ms 5560 KB Output is correct
18 Correct 217 ms 5428 KB Output is correct
19 Correct 762 ms 21704 KB Output is correct
20 Correct 693 ms 21656 KB Output is correct
21 Correct 513 ms 21292 KB Output is correct
22 Correct 510 ms 21260 KB Output is correct
23 Correct 380 ms 7292 KB Output is correct
24 Correct 412 ms 7300 KB Output is correct
25 Correct 549 ms 21716 KB Output is correct
26 Correct 566 ms 21736 KB Output is correct
27 Correct 639 ms 21768 KB Output is correct
28 Correct 604 ms 21840 KB Output is correct
29 Correct 572 ms 21936 KB Output is correct
30 Correct 382 ms 7280 KB Output is correct
31 Correct 552 ms 21792 KB Output is correct
32 Correct 497 ms 21556 KB Output is correct
33 Correct 372 ms 7188 KB Output is correct
34 Correct 564 ms 21772 KB Output is correct
35 Correct 467 ms 21028 KB Output is correct
36 Correct 374 ms 7352 KB Output is correct
37 Correct 366 ms 7192 KB Output is correct
38 Correct 657 ms 21552 KB Output is correct
39 Correct 440 ms 21752 KB Output is correct
40 Correct 504 ms 21384 KB Output is correct
41 Correct 660 ms 21976 KB Output is correct
42 Correct 556 ms 21196 KB Output is correct
43 Correct 207 ms 5376 KB Output is correct
44 Correct 216 ms 5492 KB Output is correct
45 Correct 227 ms 5676 KB Output is correct
46 Correct 181 ms 5344 KB Output is correct
47 Correct 197 ms 5580 KB Output is correct
48 Correct 179 ms 5520 KB Output is correct
49 Correct 203 ms 5632 KB Output is correct
50 Correct 180 ms 5412 KB Output is correct
51 Correct 203 ms 5580 KB Output is correct
52 Correct 167 ms 5284 KB Output is correct
53 Correct 194 ms 5536 KB Output is correct
54 Correct 166 ms 5444 KB Output is correct
55 Correct 234 ms 5560 KB Output is correct
56 Correct 201 ms 5524 KB Output is correct
57 Correct 221 ms 5576 KB Output is correct
58 Correct 181 ms 5388 KB Output is correct
59 Correct 194 ms 5572 KB Output is correct
60 Correct 198 ms 5516 KB Output is correct
61 Correct 198 ms 5496 KB Output is correct
62 Correct 207 ms 5464 KB Output is correct
63 Correct 196 ms 5484 KB Output is correct
64 Correct 202 ms 5776 KB Output is correct
65 Correct 196 ms 5484 KB Output is correct
66 Correct 198 ms 5480 KB Output is correct
67 Correct 194 ms 5452 KB Output is correct
68 Correct 201 ms 5548 KB Output is correct
69 Correct 681 ms 25948 KB Output is correct
70 Correct 800 ms 27428 KB Output is correct
71 Correct 375 ms 7416 KB Output is correct
72 Correct 377 ms 7376 KB Output is correct
73 Correct 383 ms 7368 KB Output is correct
74 Correct 500 ms 22504 KB Output is correct
75 Correct 342 ms 7328 KB Output is correct
76 Correct 543 ms 22392 KB Output is correct
77 Correct 522 ms 21788 KB Output is correct
78 Correct 382 ms 7296 KB Output is correct
79 Correct 355 ms 7320 KB Output is correct
80 Correct 565 ms 25292 KB Output is correct
81 Correct 370 ms 7636 KB Output is correct
82 Correct 691 ms 27484 KB Output is correct
83 Correct 653 ms 27200 KB Output is correct
84 Correct 386 ms 7684 KB Output is correct
85 Correct 376 ms 7568 KB Output is correct
86 Correct 591 ms 25568 KB Output is correct
87 Correct 656 ms 27484 KB Output is correct
88 Correct 388 ms 7676 KB Output is correct
89 Correct 592 ms 26152 KB Output is correct
90 Correct 708 ms 27304 KB Output is correct
91 Correct 400 ms 7652 KB Output is correct
92 Correct 579 ms 25440 KB Output is correct
93 Correct 366 ms 7552 KB Output is correct
94 Correct 380 ms 7652 KB Output is correct
95 Correct 379 ms 7588 KB Output is correct
96 Correct 631 ms 21672 KB Output is correct
97 Correct 515 ms 27336 KB Output is correct
98 Correct 594 ms 25068 KB Output is correct
99 Correct 806 ms 27664 KB Output is correct
100 Correct 685 ms 26740 KB Output is correct