Submission #1069670

# Submission time Handle Problem Language Result Execution time Memory
1069670 2024-08-22T07:57:08 Z danikoynov Crossing (JOI21_crossing) C++14
100 / 100
2189 ms 225204 KB
#include<bits/stdc++.h>
#define endl '\n'

using namespace std;

int n;
string c1, c2, c3;

unordered_set < string > st;

string action(string s1, string s2)
{
    string res;
    res.resize(n);
    for (int i = 0; i < n; i ++)
    {
        if (s1[i] == s2[i])
            res[i] = s1[i];
        else
        {
            char c = 'J';
            if (s1[i] == c || s2[i] == c)
                c = 'O';
            if (s1[i] == c || s2[i] == c)
                c = 'I';
            res[i] = c;
        }
    }
    return res;
}
void brute(string s)
{
    if (st.find(s) != st.end())
        return;

    st.insert(s);

    brute(action(s, c1));
    brute(action(s, c2));
    brute(action(s, c3));
}

/*
0 - J
1 - O
2 - I
*/

int id[256];

struct Node
{
    int cnt[3];
    int match[3];

    Node()
    {
        for (int i = 0; i < 3; i ++)
        {
            cnt[i] = match[i] = 0;
        }
    }
};

Node unite(Node lf, Node rf)
{
    Node mf;
    for (int i = 0; i < 3; i ++)
    {
        mf.cnt[i] = lf.cnt[i] + rf.cnt[i];
        mf.match[i] = lf.match[i] + rf.match[i];
    }

    return mf;
}

const int MAXN = 2e5 + 10;

struct SegmentTree
{
    Node tree[4 * MAXN];
    string pat;

    void build_tree(int root, int left, int right, string &val)
    {
        lazy[root] = 3;
        if (left == right)
        {
            tree[root] = Node();
            tree[root].cnt[id[pat[left]]] ++;
            if (val[left] == pat[left])
                tree[root].match[id[pat[left]]] ++;
            return;
        }

        int mid = (left + right) / 2;
        build_tree(root * 2, left, mid, val);
        build_tree(root * 2 + 1, mid + 1, right, val);
    
        tree[root] = unite(tree[root * 2], tree[root * 2 + 1]);

        //cout << "here " << root << " " << left << " " << right << " " << tree[root].match[0] + tree[root].match[1] + tree[root].match[2] << endl;
    }

    int lazy[4 * MAXN];

    void push_lazy(int root, int left, int right)
    {
        if (lazy[root] == 3)
            return;
        for (int i = 0; i < 3; i ++)
        {   
            tree[root].match[i] = 0;
        }

        tree[root].match[lazy[root]] = tree[root].cnt[lazy[root]];
        
        if (left != right)
        {
            lazy[root * 2] = lazy[root];
            lazy[root * 2 + 1] = lazy[root];
        }

        lazy[root] = 3;
    }
    void update_range(int root, int left, int right, int qleft, int qright, int val)
    {
        push_lazy(root, left, right);
        if (left > qright || right < qleft)
            return;

        if (left >= qleft && right <= qright)
        {
            lazy[root] = val;
            push_lazy(root, left, right);
            return;
        }

        int mid = (left + right) / 2;
        update_range(root * 2, left, mid, qleft, qright, val);
        update_range(root * 2 + 1, mid + 1, right, qleft, qright, val);

        tree[root] = unite(tree[root * 2], tree[root * 2 + 1]);
    }
};
int q;
string t;

string val[10];
SegmentTree seg[10];

int m;

bool check_answer()
{
    for (int i = 1; i <= m; i ++)
    {
        int match = 0;
        for (int j = 0; j < 3; j ++)
            match += seg[i].tree[1].match[j];
        if (match == n)
            return true;
    }
    return false;
}

void solve()
{
    id['J'] = 0;
    id['O'] = 1;
    id['I'] = 2;
    cin >> n;
    cin >> c1 >> c2 >> c3;
    brute(c1);
    brute(c2);
    brute(c3);
    assert(st.size() < 10);

    cin >> q >> t;
    t = "#" + t;
    for (string cur : st)
    {
        m ++;
        seg[m].pat = "#" + cur;
        seg[m].build_tree(1, 1, n, t);
    }
      //  cout << cur << endl;

    //cout << "find " << t << endl;
    if (check_answer())
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    for (int i = 1; i <= q; i ++)
    {
        int l, r;
        char c;
        cin >> l >> r >> c;
        for (int j = 1; j <= m; j ++)
        {
            seg[j].update_range(1, 1, n, l, r, id[c]);
        }

        if (check_answer())
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }

}

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}
int main()
{
    speed();
    solve();
    return 0;
}

Compilation message

Main.cpp: In member function 'void SegmentTree::build_tree(int, int, int, std::string&)':
Main.cpp:90:40: warning: array subscript has type 'char' [-Wchar-subscripts]
   90 |             tree[root].cnt[id[pat[left]]] ++;
      |                                        ^
Main.cpp:92:46: warning: array subscript has type 'char' [-Wchar-subscripts]
   92 |                 tree[root].match[id[pat[left]]] ++;
      |                                              ^
Main.cpp: In function 'void solve()':
Main.cpp:201:51: warning: array subscript has type 'char' [-Wchar-subscripts]
  201 |             seg[j].update_range(1, 1, n, l, r, id[c]);
      |                                                   ^
# Verdict Execution time Memory Grader output
1 Correct 103 ms 200372 KB Output is correct
2 Correct 119 ms 200268 KB Output is correct
3 Correct 115 ms 200336 KB Output is correct
4 Correct 120 ms 200272 KB Output is correct
5 Correct 118 ms 200336 KB Output is correct
6 Correct 122 ms 200404 KB Output is correct
7 Correct 106 ms 200264 KB Output is correct
8 Correct 142 ms 200344 KB Output is correct
9 Correct 112 ms 200296 KB Output is correct
10 Correct 129 ms 200272 KB Output is correct
11 Correct 105 ms 200276 KB Output is correct
12 Correct 107 ms 200452 KB Output is correct
13 Correct 112 ms 200272 KB Output is correct
14 Correct 106 ms 200280 KB Output is correct
15 Correct 120 ms 200276 KB Output is correct
16 Correct 103 ms 200276 KB Output is correct
17 Correct 103 ms 200272 KB Output is correct
18 Correct 128 ms 200532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 200372 KB Output is correct
2 Correct 119 ms 200268 KB Output is correct
3 Correct 115 ms 200336 KB Output is correct
4 Correct 120 ms 200272 KB Output is correct
5 Correct 118 ms 200336 KB Output is correct
6 Correct 122 ms 200404 KB Output is correct
7 Correct 106 ms 200264 KB Output is correct
8 Correct 142 ms 200344 KB Output is correct
9 Correct 112 ms 200296 KB Output is correct
10 Correct 129 ms 200272 KB Output is correct
11 Correct 105 ms 200276 KB Output is correct
12 Correct 107 ms 200452 KB Output is correct
13 Correct 112 ms 200272 KB Output is correct
14 Correct 106 ms 200280 KB Output is correct
15 Correct 120 ms 200276 KB Output is correct
16 Correct 103 ms 200276 KB Output is correct
17 Correct 103 ms 200272 KB Output is correct
18 Correct 128 ms 200532 KB Output is correct
19 Correct 262 ms 204236 KB Output is correct
20 Correct 284 ms 206864 KB Output is correct
21 Correct 188 ms 206688 KB Output is correct
22 Correct 261 ms 206472 KB Output is correct
23 Correct 157 ms 202832 KB Output is correct
24 Correct 157 ms 202836 KB Output is correct
25 Correct 196 ms 207128 KB Output is correct
26 Correct 227 ms 206972 KB Output is correct
27 Correct 248 ms 207124 KB Output is correct
28 Correct 230 ms 207116 KB Output is correct
29 Correct 236 ms 206968 KB Output is correct
30 Correct 157 ms 202836 KB Output is correct
31 Correct 230 ms 207120 KB Output is correct
32 Correct 227 ms 206596 KB Output is correct
33 Correct 152 ms 202744 KB Output is correct
34 Correct 238 ms 207064 KB Output is correct
35 Correct 195 ms 206092 KB Output is correct
36 Correct 157 ms 202876 KB Output is correct
37 Correct 158 ms 202696 KB Output is correct
38 Correct 250 ms 206864 KB Output is correct
39 Correct 164 ms 207124 KB Output is correct
40 Correct 183 ms 205264 KB Output is correct
41 Correct 282 ms 207284 KB Output is correct
42 Correct 96 ms 206564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 200372 KB Output is correct
2 Correct 119 ms 200268 KB Output is correct
3 Correct 115 ms 200336 KB Output is correct
4 Correct 120 ms 200272 KB Output is correct
5 Correct 118 ms 200336 KB Output is correct
6 Correct 122 ms 200404 KB Output is correct
7 Correct 106 ms 200264 KB Output is correct
8 Correct 142 ms 200344 KB Output is correct
9 Correct 112 ms 200296 KB Output is correct
10 Correct 129 ms 200272 KB Output is correct
11 Correct 105 ms 200276 KB Output is correct
12 Correct 107 ms 200452 KB Output is correct
13 Correct 112 ms 200272 KB Output is correct
14 Correct 106 ms 200280 KB Output is correct
15 Correct 120 ms 200276 KB Output is correct
16 Correct 103 ms 200276 KB Output is correct
17 Correct 103 ms 200272 KB Output is correct
18 Correct 128 ms 200532 KB Output is correct
19 Correct 420 ms 200436 KB Output is correct
20 Correct 522 ms 200296 KB Output is correct
21 Correct 197 ms 200456 KB Output is correct
22 Correct 173 ms 200272 KB Output is correct
23 Correct 175 ms 200276 KB Output is correct
24 Correct 170 ms 200276 KB Output is correct
25 Correct 185 ms 200448 KB Output is correct
26 Correct 172 ms 200276 KB Output is correct
27 Correct 195 ms 200272 KB Output is correct
28 Correct 173 ms 200304 KB Output is correct
29 Correct 174 ms 200460 KB Output is correct
30 Correct 155 ms 200412 KB Output is correct
31 Correct 394 ms 200436 KB Output is correct
32 Correct 360 ms 200276 KB Output is correct
33 Correct 385 ms 200352 KB Output is correct
34 Correct 326 ms 200272 KB Output is correct
35 Correct 347 ms 200272 KB Output is correct
36 Correct 383 ms 200340 KB Output is correct
37 Correct 370 ms 200312 KB Output is correct
38 Correct 373 ms 200496 KB Output is correct
39 Correct 376 ms 200276 KB Output is correct
40 Correct 359 ms 200276 KB Output is correct
41 Correct 352 ms 200680 KB Output is correct
42 Correct 384 ms 200276 KB Output is correct
43 Correct 363 ms 200468 KB Output is correct
44 Correct 369 ms 200440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 200372 KB Output is correct
2 Correct 119 ms 200268 KB Output is correct
3 Correct 115 ms 200336 KB Output is correct
4 Correct 120 ms 200272 KB Output is correct
5 Correct 118 ms 200336 KB Output is correct
6 Correct 122 ms 200404 KB Output is correct
7 Correct 106 ms 200264 KB Output is correct
8 Correct 142 ms 200344 KB Output is correct
9 Correct 112 ms 200296 KB Output is correct
10 Correct 129 ms 200272 KB Output is correct
11 Correct 105 ms 200276 KB Output is correct
12 Correct 107 ms 200452 KB Output is correct
13 Correct 112 ms 200272 KB Output is correct
14 Correct 106 ms 200280 KB Output is correct
15 Correct 120 ms 200276 KB Output is correct
16 Correct 103 ms 200276 KB Output is correct
17 Correct 103 ms 200272 KB Output is correct
18 Correct 128 ms 200532 KB Output is correct
19 Correct 262 ms 204236 KB Output is correct
20 Correct 284 ms 206864 KB Output is correct
21 Correct 188 ms 206688 KB Output is correct
22 Correct 261 ms 206472 KB Output is correct
23 Correct 157 ms 202832 KB Output is correct
24 Correct 157 ms 202836 KB Output is correct
25 Correct 196 ms 207128 KB Output is correct
26 Correct 227 ms 206972 KB Output is correct
27 Correct 248 ms 207124 KB Output is correct
28 Correct 230 ms 207116 KB Output is correct
29 Correct 236 ms 206968 KB Output is correct
30 Correct 157 ms 202836 KB Output is correct
31 Correct 230 ms 207120 KB Output is correct
32 Correct 227 ms 206596 KB Output is correct
33 Correct 152 ms 202744 KB Output is correct
34 Correct 238 ms 207064 KB Output is correct
35 Correct 195 ms 206092 KB Output is correct
36 Correct 157 ms 202876 KB Output is correct
37 Correct 158 ms 202696 KB Output is correct
38 Correct 250 ms 206864 KB Output is correct
39 Correct 164 ms 207124 KB Output is correct
40 Correct 183 ms 205264 KB Output is correct
41 Correct 282 ms 207284 KB Output is correct
42 Correct 96 ms 206564 KB Output is correct
43 Correct 420 ms 200436 KB Output is correct
44 Correct 522 ms 200296 KB Output is correct
45 Correct 197 ms 200456 KB Output is correct
46 Correct 173 ms 200272 KB Output is correct
47 Correct 175 ms 200276 KB Output is correct
48 Correct 170 ms 200276 KB Output is correct
49 Correct 185 ms 200448 KB Output is correct
50 Correct 172 ms 200276 KB Output is correct
51 Correct 195 ms 200272 KB Output is correct
52 Correct 173 ms 200304 KB Output is correct
53 Correct 174 ms 200460 KB Output is correct
54 Correct 155 ms 200412 KB Output is correct
55 Correct 394 ms 200436 KB Output is correct
56 Correct 360 ms 200276 KB Output is correct
57 Correct 385 ms 200352 KB Output is correct
58 Correct 326 ms 200272 KB Output is correct
59 Correct 347 ms 200272 KB Output is correct
60 Correct 383 ms 200340 KB Output is correct
61 Correct 370 ms 200312 KB Output is correct
62 Correct 373 ms 200496 KB Output is correct
63 Correct 376 ms 200276 KB Output is correct
64 Correct 359 ms 200276 KB Output is correct
65 Correct 352 ms 200680 KB Output is correct
66 Correct 384 ms 200276 KB Output is correct
67 Correct 363 ms 200468 KB Output is correct
68 Correct 369 ms 200440 KB Output is correct
69 Correct 1860 ms 223240 KB Output is correct
70 Correct 1919 ms 225040 KB Output is correct
71 Correct 322 ms 202904 KB Output is correct
72 Correct 336 ms 202796 KB Output is correct
73 Correct 298 ms 202836 KB Output is correct
74 Correct 471 ms 207516 KB Output is correct
75 Correct 295 ms 202780 KB Output is correct
76 Correct 486 ms 209368 KB Output is correct
77 Correct 433 ms 207612 KB Output is correct
78 Correct 299 ms 202840 KB Output is correct
79 Correct 301 ms 202832 KB Output is correct
80 Correct 1272 ms 222220 KB Output is correct
81 Correct 692 ms 203856 KB Output is correct
82 Correct 1510 ms 224968 KB Output is correct
83 Correct 1466 ms 224496 KB Output is correct
84 Correct 713 ms 203860 KB Output is correct
85 Correct 710 ms 203680 KB Output is correct
86 Correct 1428 ms 222680 KB Output is correct
87 Correct 1626 ms 224904 KB Output is correct
88 Correct 754 ms 203860 KB Output is correct
89 Correct 1432 ms 223628 KB Output is correct
90 Correct 1562 ms 225204 KB Output is correct
91 Correct 787 ms 203660 KB Output is correct
92 Correct 1263 ms 222656 KB Output is correct
93 Correct 720 ms 203680 KB Output is correct
94 Correct 696 ms 203728 KB Output is correct
95 Correct 698 ms 203856 KB Output is correct
96 Correct 237 ms 207120 KB Output is correct
97 Correct 858 ms 224892 KB Output is correct
98 Correct 1313 ms 215276 KB Output is correct
99 Correct 2189 ms 225072 KB Output is correct
100 Correct 160 ms 224440 KB Output is correct