Submission #1052223

# Submission time Handle Problem Language Result Execution time Memory
1052223 2024-08-10T12:34:06 Z crafticat Crossing (JOI21_crossing) C++17
100 / 100
833 ms 41508 KB
#include <bits/stdc++.h>

using namespace std;

#include <bits/stdc++.h>

#include <utility>
using namespace std;

using ll = long long;
using pi = pair<int,int>;
using pl = pair<ll,ll>;

#define temp(T) template<class T>
temp(T) using V = vector<T>;
using vi = V<int>;
using vl = V<ll>;
using vvi = V<vi>;
using vpi = V<pi>;
using vb = V<bool>;
using vvb = V<vb>;
vi dx = {0,1,-1,0};
vi dy = {1,0,0,-1};

#define sz(x) x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define f first
#define s second

#define lb lower_bound
#define ub upper_bound

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)

using str = string;

const int MOD = 1e9 + 7;
const ll BIG = 1e18;
const int INF = 1e9 + 2;

temp(T) bool ckmin(T& a, const T& b) {
    return b < a ? a = b, 1 : 0; } // set a = min(a,b)
temp(T) bool ckmax(T& a, const T& b) {
    return a < b ? a = b, 1 : 0; } // set a = max(a,b)

void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s = "") {
    unsyncIO();
    if (sz(s)) setIn(s+".in"), setOut(s+".out"); // for USACO
}

#define read(x) int x; cin >> x

temp(T) void pr(T x) {
    cout << to_string(x);
}
temp(T) void prln(T x) {
    cout << to_string(x) << "\n";
}


vi readV(int n) {

    vi input(n);
    F0R(i,n)
        cin >> input[i];
    return input;
}

int rand(int a, int b) {
    return a + rand() % (b - a + 1);
}

#define gen(x,a,b) int x = rand(a,b)

temp(T) void moveAll(V<T> &a, V<T> &b) {
    each(x,b) {
        a.pb(x);
    }
    b.clear();
}

vi genArr(int n, int a, int b) {
    vi generated(n);
    F0R(i,n) {
        generated[i] = rand(a,b);
    }
    return generated;
}

vvi genTree(int n) {
    vvi g(n + 1);
    F0R(i,n) {
        gen(p,0,i);
        g[p].pb(i + 1);
        g[i + 1].pb(p);
    }

    return g;
}

vvb readGrid(int a, int b) {
    vvb grid(a + 2, vb(b + 2, true));

    F0R(i, a) {
        F0R(j, b) {
            bool k; cin >> k;
            grid[i + 1][j + 1] = k;
        }
    }
    return grid;
}

struct mint {
    ll v;

    mint(ll t) : v(t % MOD) {}
    mint() = default;
    mint operator*(mint other) {
        return {v * other.v};
    }
    void operator*=(mint other) {
        v *= other.v;
        v %= MOD;
    }
    mint operator+(mint other) {
        return {v + other.v};
    }
    void operator+=(mint other) {
        v += other.v;
        v %= MOD;
    }
};

V<V<mint>> prePrefix;
V<V<mint>> prePrefix2;

// TODO precompute
mint pow(mint a, int b) {
    if (b == 0) return 1;
    mint ans = pow(a, b / 2);
    ans = ans * ans;
    if (b % 2) ans *= a;
    return ans;
}

int v(char k) {
    if (k == 'O') return 0;
    if (k == 'I') return 1;
    if (k == 'J') return 2;
    exit(5);
}

struct Seg {
    Seg *left = nullptr, *right = nullptr;
    int l, r, mid;
    mint hash = 0;
    mint hash2 = 0;
    int lazy = -1;

    Seg(int l, int r, str &def) : l(l) ,r(r), mid((r + l) / 2) {
        if (r -l > 1) {
            left = new Seg(l,mid,def);
            right = new Seg(mid,r,def);
            hash = left->hash + right->hash * pow(mint(3), mid - l);
            hash2 = left->hash2 + right->hash2 * pow(mint(4), mid - l);
        } else {
            hash = v(def[l]);
            hash2 = v(def[l]);
        }
    }

    void push() {
        if (lazy != -1) {
            hash = prePrefix[lazy][r - l];
            hash2 = prePrefix2[lazy][r - l];
            if (r - l > 1) {
                left->lazy = lazy;
                right->lazy = lazy;
            }
            lazy = -1;
        }
    }

    void update(int a, int b, int k) {
        push();
        if (b <= l || a >= r) return;
        if (a <= l && b >= r) {
            lazy = k;
            push();
            return;
        }

        left->update(a,b,k);
        right->update(a,b,k);
        hash = left->hash + right->hash * pow(mint(3), mid - l);
        hash2 = left->hash2 + right->hash2 * pow(mint(4), mid - l);
    }
};

using has = pair<mint,mint>;

has findHash(str a) {
    mint h = 0;
    mint h2 = 0;
    mint power = 1;
    mint power2 = 1;
    F0R(i, sz(a)) {
        h += power * v(a[i]);
        h2 += power2 * v(a[i]);
        power *= 3;
        power2 *= 4;
    }
    return {h, h2};
}

str add(str a, str b) {
    str c = a;
    F0R(i, sz(a)) {
        if (a[i] == b[i]) continue;
        if ('O' != a[i] and 'O' != b[i]) c[i] = 'O';
        if ('J' != a[i] and 'J' != b[i]) c[i] = 'J';
        if ('I' != a[i] and 'I' != b[i]) c[i] = 'I';
    }
    return c;
}

V<has> hashes;
void genHashes(str a, str b, str c) {
    set<str> found;

    str acc = a;
    while (!found.count(acc)) {
        found.ins(acc);
        acc = add(acc,a);
        hashes.pb(findHash(acc));
        acc = add(acc,b);
        hashes.pb(findHash(acc));
        acc = add(acc,c);
        hashes.pb(findHash(acc));
    }
}

bool findMatch(has hash) {
    each(x, hashes) {
        if (x.f.v ==hash.f.v && x.s.v == hash.s.v) return true;
    }
    return false;
}

int main() {
    setIO();

    int n; cin >> n;
    V<str> starts(3);

    F0R(i, 3) {
        cin >> starts[i];
    }

    int q; cin >> q;
    string def; cin >> def;
    Seg seg(0, sz(def), def);

    vi perm = {0,1,2};
    do {
        genHashes(starts[perm[0]], starts[perm[1]], starts[perm[2]]);
    } while (next_permutation(all(perm)));

    prePrefix.rsz(3, V<mint>(n + 1));
    prePrefix2.rsz(3, V<mint>(n + 1));
    F0R(i, 3) {
        mint h = 0;
        mint h2 = 0;
        mint power = 1;
        mint power2 = 1;
        F0R(j, n + 1) {
            prePrefix[i][j] = h;
            prePrefix2[i][j] = h2;
            h += power * (i);
            h2 += power2 * (i);
            power *= 3;
            power2 *= 4;
        }
    }

    cout << ((findMatch({seg.hash,seg.hash2})) ? "Yes" : "No") << "\n";
    F0R(i, q) {
        int l, r; cin >> l >> r;
        l--;
        char c; cin >> c;

        seg.update(l, r, v(c));
        cout << ((findMatch({seg.hash,seg.hash2})) ? "Yes" : "No") << "\n";
    }

    return 0;
}

Compilation message

Main.cpp: In function 'has findHash(str)':
Main.cpp:39:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
      |                                        ^
Main.cpp:40:18: note: in expansion of macro 'FOR'
   40 | #define F0R(i,a) FOR(i,0,a)
      |                  ^~~
Main.cpp:222:5: note: in expansion of macro 'F0R'
  222 |     F0R(i, sz(a)) {
      |     ^~~
Main.cpp: In function 'str add(str, str)':
Main.cpp:39:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
      |                                        ^
Main.cpp:40:18: note: in expansion of macro 'FOR'
   40 | #define F0R(i,a) FOR(i,0,a)
      |                  ^~~
Main.cpp:233:5: note: in expansion of macro 'F0R'
  233 |     F0R(i, sz(a)) {
      |     ^~~
Main.cpp: In function 'void setIn(str)':
Main.cpp:59:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'void setOut(str)':
Main.cpp:60:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 | void setOut(str s) { freopen(s.c_str(),"w",stdout); }
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 74 ms 1008 KB Output is correct
2 Correct 91 ms 924 KB Output is correct
3 Correct 108 ms 852 KB Output is correct
4 Correct 70 ms 972 KB Output is correct
5 Correct 78 ms 792 KB Output is correct
6 Correct 73 ms 820 KB Output is correct
7 Correct 80 ms 988 KB Output is correct
8 Correct 72 ms 900 KB Output is correct
9 Correct 85 ms 1008 KB Output is correct
10 Correct 71 ms 1036 KB Output is correct
11 Correct 73 ms 2508 KB Output is correct
12 Correct 82 ms 2728 KB Output is correct
13 Correct 86 ms 2512 KB Output is correct
14 Correct 77 ms 2456 KB Output is correct
15 Correct 79 ms 2408 KB Output is correct
16 Correct 75 ms 2452 KB Output is correct
17 Correct 73 ms 2476 KB Output is correct
18 Correct 77 ms 2460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 1008 KB Output is correct
2 Correct 91 ms 924 KB Output is correct
3 Correct 108 ms 852 KB Output is correct
4 Correct 70 ms 972 KB Output is correct
5 Correct 78 ms 792 KB Output is correct
6 Correct 73 ms 820 KB Output is correct
7 Correct 80 ms 988 KB Output is correct
8 Correct 72 ms 900 KB Output is correct
9 Correct 85 ms 1008 KB Output is correct
10 Correct 71 ms 1036 KB Output is correct
11 Correct 73 ms 2508 KB Output is correct
12 Correct 82 ms 2728 KB Output is correct
13 Correct 86 ms 2512 KB Output is correct
14 Correct 77 ms 2456 KB Output is correct
15 Correct 79 ms 2408 KB Output is correct
16 Correct 75 ms 2452 KB Output is correct
17 Correct 73 ms 2476 KB Output is correct
18 Correct 77 ms 2460 KB Output is correct
19 Correct 623 ms 41508 KB Output is correct
20 Correct 670 ms 41048 KB Output is correct
21 Correct 485 ms 37844 KB Output is correct
22 Correct 396 ms 33804 KB Output is correct
23 Correct 223 ms 4424 KB Output is correct
24 Correct 245 ms 4392 KB Output is correct
25 Correct 458 ms 40084 KB Output is correct
26 Correct 473 ms 40072 KB Output is correct
27 Correct 486 ms 39968 KB Output is correct
28 Correct 487 ms 40056 KB Output is correct
29 Correct 542 ms 38964 KB Output is correct
30 Correct 246 ms 4404 KB Output is correct
31 Correct 481 ms 40100 KB Output is correct
32 Correct 456 ms 36740 KB Output is correct
33 Correct 238 ms 4252 KB Output is correct
34 Correct 476 ms 40072 KB Output is correct
35 Correct 391 ms 30356 KB Output is correct
36 Correct 241 ms 4432 KB Output is correct
37 Correct 276 ms 4432 KB Output is correct
38 Correct 554 ms 40068 KB Output is correct
39 Correct 411 ms 39972 KB Output is correct
40 Correct 332 ms 26960 KB Output is correct
41 Correct 677 ms 40076 KB Output is correct
42 Correct 76 ms 39856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 1008 KB Output is correct
2 Correct 91 ms 924 KB Output is correct
3 Correct 108 ms 852 KB Output is correct
4 Correct 70 ms 972 KB Output is correct
5 Correct 78 ms 792 KB Output is correct
6 Correct 73 ms 820 KB Output is correct
7 Correct 80 ms 988 KB Output is correct
8 Correct 72 ms 900 KB Output is correct
9 Correct 85 ms 1008 KB Output is correct
10 Correct 71 ms 1036 KB Output is correct
11 Correct 73 ms 2508 KB Output is correct
12 Correct 82 ms 2728 KB Output is correct
13 Correct 86 ms 2512 KB Output is correct
14 Correct 77 ms 2456 KB Output is correct
15 Correct 79 ms 2408 KB Output is correct
16 Correct 75 ms 2452 KB Output is correct
17 Correct 73 ms 2476 KB Output is correct
18 Correct 77 ms 2460 KB Output is correct
19 Correct 77 ms 2444 KB Output is correct
20 Correct 103 ms 2364 KB Output is correct
21 Correct 72 ms 2384 KB Output is correct
22 Correct 62 ms 2384 KB Output is correct
23 Correct 70 ms 2384 KB Output is correct
24 Correct 69 ms 2316 KB Output is correct
25 Correct 72 ms 2568 KB Output is correct
26 Correct 62 ms 2384 KB Output is correct
27 Correct 72 ms 2384 KB Output is correct
28 Correct 68 ms 2380 KB Output is correct
29 Correct 72 ms 2484 KB Output is correct
30 Correct 60 ms 2388 KB Output is correct
31 Correct 77 ms 2624 KB Output is correct
32 Correct 74 ms 2384 KB Output is correct
33 Correct 72 ms 2472 KB Output is correct
34 Correct 64 ms 2388 KB Output is correct
35 Correct 82 ms 2544 KB Output is correct
36 Correct 73 ms 2464 KB Output is correct
37 Correct 75 ms 2384 KB Output is correct
38 Correct 76 ms 2568 KB Output is correct
39 Correct 71 ms 2384 KB Output is correct
40 Correct 79 ms 2520 KB Output is correct
41 Correct 72 ms 2480 KB Output is correct
42 Correct 88 ms 2388 KB Output is correct
43 Correct 74 ms 2384 KB Output is correct
44 Correct 73 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 1008 KB Output is correct
2 Correct 91 ms 924 KB Output is correct
3 Correct 108 ms 852 KB Output is correct
4 Correct 70 ms 972 KB Output is correct
5 Correct 78 ms 792 KB Output is correct
6 Correct 73 ms 820 KB Output is correct
7 Correct 80 ms 988 KB Output is correct
8 Correct 72 ms 900 KB Output is correct
9 Correct 85 ms 1008 KB Output is correct
10 Correct 71 ms 1036 KB Output is correct
11 Correct 73 ms 2508 KB Output is correct
12 Correct 82 ms 2728 KB Output is correct
13 Correct 86 ms 2512 KB Output is correct
14 Correct 77 ms 2456 KB Output is correct
15 Correct 79 ms 2408 KB Output is correct
16 Correct 75 ms 2452 KB Output is correct
17 Correct 73 ms 2476 KB Output is correct
18 Correct 77 ms 2460 KB Output is correct
19 Correct 623 ms 41508 KB Output is correct
20 Correct 670 ms 41048 KB Output is correct
21 Correct 485 ms 37844 KB Output is correct
22 Correct 396 ms 33804 KB Output is correct
23 Correct 223 ms 4424 KB Output is correct
24 Correct 245 ms 4392 KB Output is correct
25 Correct 458 ms 40084 KB Output is correct
26 Correct 473 ms 40072 KB Output is correct
27 Correct 486 ms 39968 KB Output is correct
28 Correct 487 ms 40056 KB Output is correct
29 Correct 542 ms 38964 KB Output is correct
30 Correct 246 ms 4404 KB Output is correct
31 Correct 481 ms 40100 KB Output is correct
32 Correct 456 ms 36740 KB Output is correct
33 Correct 238 ms 4252 KB Output is correct
34 Correct 476 ms 40072 KB Output is correct
35 Correct 391 ms 30356 KB Output is correct
36 Correct 241 ms 4432 KB Output is correct
37 Correct 276 ms 4432 KB Output is correct
38 Correct 554 ms 40068 KB Output is correct
39 Correct 411 ms 39972 KB Output is correct
40 Correct 332 ms 26960 KB Output is correct
41 Correct 677 ms 40076 KB Output is correct
42 Correct 76 ms 39856 KB Output is correct
43 Correct 77 ms 2444 KB Output is correct
44 Correct 103 ms 2364 KB Output is correct
45 Correct 72 ms 2384 KB Output is correct
46 Correct 62 ms 2384 KB Output is correct
47 Correct 70 ms 2384 KB Output is correct
48 Correct 69 ms 2316 KB Output is correct
49 Correct 72 ms 2568 KB Output is correct
50 Correct 62 ms 2384 KB Output is correct
51 Correct 72 ms 2384 KB Output is correct
52 Correct 68 ms 2380 KB Output is correct
53 Correct 72 ms 2484 KB Output is correct
54 Correct 60 ms 2388 KB Output is correct
55 Correct 77 ms 2624 KB Output is correct
56 Correct 74 ms 2384 KB Output is correct
57 Correct 72 ms 2472 KB Output is correct
58 Correct 64 ms 2388 KB Output is correct
59 Correct 82 ms 2544 KB Output is correct
60 Correct 73 ms 2464 KB Output is correct
61 Correct 75 ms 2384 KB Output is correct
62 Correct 76 ms 2568 KB Output is correct
63 Correct 71 ms 2384 KB Output is correct
64 Correct 79 ms 2520 KB Output is correct
65 Correct 72 ms 2480 KB Output is correct
66 Correct 88 ms 2388 KB Output is correct
67 Correct 74 ms 2384 KB Output is correct
68 Correct 73 ms 2388 KB Output is correct
69 Correct 605 ms 33552 KB Output is correct
70 Correct 734 ms 39856 KB Output is correct
71 Correct 296 ms 4432 KB Output is correct
72 Correct 245 ms 4432 KB Output is correct
73 Correct 236 ms 4492 KB Output is correct
74 Correct 480 ms 34168 KB Output is correct
75 Correct 277 ms 4236 KB Output is correct
76 Correct 503 ms 39944 KB Output is correct
77 Correct 480 ms 33756 KB Output is correct
78 Correct 295 ms 4432 KB Output is correct
79 Correct 237 ms 4416 KB Output is correct
80 Correct 525 ms 28956 KB Output is correct
81 Correct 277 ms 4268 KB Output is correct
82 Correct 538 ms 40128 KB Output is correct
83 Correct 575 ms 37676 KB Output is correct
84 Correct 270 ms 4432 KB Output is correct
85 Correct 269 ms 4276 KB Output is correct
86 Correct 537 ms 30692 KB Output is correct
87 Correct 577 ms 39936 KB Output is correct
88 Correct 232 ms 4284 KB Output is correct
89 Correct 525 ms 35448 KB Output is correct
90 Correct 586 ms 39884 KB Output is correct
91 Correct 320 ms 4348 KB Output is correct
92 Correct 458 ms 30328 KB Output is correct
93 Correct 264 ms 4372 KB Output is correct
94 Correct 241 ms 4504 KB Output is correct
95 Correct 302 ms 4432 KB Output is correct
96 Correct 555 ms 39988 KB Output is correct
97 Correct 503 ms 39904 KB Output is correct
98 Correct 443 ms 26952 KB Output is correct
99 Correct 833 ms 40116 KB Output is correct
100 Correct 161 ms 39948 KB Output is correct