Submission #1015530

# Submission time Handle Problem Language Result Execution time Memory
1015530 2024-07-06T13:22:27 Z daffuwu Crossing (JOI21_crossing) C++14
100 / 100
501 ms 40692 KB
#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

long long n, sz, q, l, r, pw3[200069][2], ps[200069][2], sm[2];
const long long dv[] = {805306457, 1610612741};
pair<long long, long long> ans;
bool bad;
char ch;
string s[3], cs, t0;
vector<string> ls;
map<char, long long> conv;
map<string, bool> ex;
map<pair<long long, long long>, bool> hsh;

char crot(char a, char b)
{
    if (a>b) swap(a, b);
    if (a == b) return a;
    if (a == 'I' && b == 'J') return 'O';
    if (a == 'I' && b == 'O') return 'J';
    return 'I';
}

string cross(string a, string b)
{
    int i;
    string c = "";
    for (i=0; i<n; i++)
    {
        c += crot(a[i], b[i]);
    }
    return c;
}

long long calc(long long l, long long r, long long id)
{
    //3^l+3^(l+1)+...+3^r
    if (l == 0) return ps[r][id];
    return (ps[r][id]-ps[l-1][id]+dv[id])%dv[id];
}

struct segtree
{
    long long l, r, val[2], lz = -1;
    segtree *le, *ri;

    void bd(long long cl, long long cr)
    {
        l = cl;
        r = cr;
        if (cl == cr)
        {
            val[0] = conv[t0[cl-1]]*pw3[cl-1][0]%dv[0];
            val[1] = conv[t0[cl-1]]*pw3[cl-1][1]%dv[1];
            return;
        }
        long long md = (cl+cr)/2;
        le = new segtree();
        ri = new segtree();
        le->bd(cl, md);
        ri->bd(md+1, cr);
        val[0] = (le->val[0]+ri->val[0])%dv[0];
        val[1] = (le->val[1]+ri->val[1])%dv[1];
    }

    void prop()
    {
        if (lz != -1)
        {
            val[0] = calc(l-1, r-1, 0)*lz%dv[0];
            val[1] = calc(l-1, r-1, 1)*lz%dv[1];
            if (l != r)
            {
                le->lz = lz;
                ri->lz = lz;
            }
            lz = -1;
        }
    }

    void upd(long long ql, long long qr, long long x)
    {
        prop();
        if (l>qr || r<ql) return;
        if (l>=ql && r<=qr)
        {
            lz = x;
            prop();
            return;
        }
        le->upd(ql, qr, x);
        ri->upd(ql, qr, x);
        val[0] = (le->val[0]+ri->val[0])%dv[0];
        val[1] = (le->val[1]+ri->val[1])%dv[1];
    }

    pair<long long, long long> qry(long long ql, long long qr)
    {
        prop();
        if (l>qr || r<ql) return {0, 0};
        if (l>=ql && r<=qr) return {val[0], val[1]};
        pair<long long, long long> pl = le->qry(ql, qr), pr = ri->qry(ql, qr);
        return {(pl.fr+pr.fr)%dv[0], (pl.sc+pr.sc)%dv[1]};
    }
} *rt;

int main() 
{
    long long i, j, rr;
    scanf("%lld", &n);
    pw3[0][0] = pw3[0][1] = ps[0][0] = ps[0][1] = 1;
    conv['I'] = 0;
    conv['J'] = 1;
    conv['O'] = 2;
    for (i=1; i<n; i++)
    {
        for (j=0; j<=1; j++)
        {
            pw3[i][j] = pw3[i-1][j]*3%dv[j];
            ps[i][j] = (ps[i-1][j]+pw3[i][j])%dv[j];
        }
    } 
    cin >> s[0] >> s[1] >> s[2]; 
    ex[s[0]] = ex[s[1]] = ex[s[2]] = 1;
    for (auto [ky, _]:ex) ls.push_back(ky);
    for (; 1;)
    {
        sz = ls.size();
        bad = 1;
        for (i=0; i<sz; i++)
        {
            for (j=i+1; j<sz; j++)
            {
                cs = cross(ls[i], ls[j]);
                if (!ex.count(cs))
                {
                    ex[cs] = 1;
                    bad = 0;
                    i = sz+1;
                    break;
                }
            }
        }
        if (bad) break;
        ls.push_back(cs);
    }
    for (auto el:ls)
    {
        for (j=0; j<=1; j++)
        {
            sm[j] = 0;
            for (i=0; i<n; i++)
            {
                sm[j] += pw3[i][j]*conv[el[i]];
                sm[j] %= dv[j];
            }
        }
        hsh[{sm[0], sm[1]}] = 1;
    }
    rt = new segtree();
    scanf("%lld", &q);
    for (rr=0; rr<=q; rr++)
    {
        if (rr == 0)
        {
            cin >> t0;
            rt->bd(1, n);
        }
        else 
        {
            scanf("%lld%lld %c", &l, &r, &ch);
            rt->upd(l, r, conv[ch]);
        }
        ans = rt->qry(1, n);
        if (hsh.count(ans)) printf("Yes\n");
        else printf("No\n");
    }
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:128:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  128 |     for (auto [ky, _]:ex) ls.push_back(ky);
      |               ^
Main.cpp:113:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |     scanf("%lld", &n);
      |     ~~~~~^~~~~~~~~~~~
Main.cpp:164:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  164 |     scanf("%lld", &q);
      |     ~~~~~^~~~~~~~~~~~
Main.cpp:174:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  174 |             scanf("%lld%lld %c", &l, &r, &ch);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 73 ms 848 KB Output is correct
2 Correct 62 ms 904 KB Output is correct
3 Correct 62 ms 796 KB Output is correct
4 Correct 50 ms 1104 KB Output is correct
5 Correct 64 ms 848 KB Output is correct
6 Correct 50 ms 928 KB Output is correct
7 Correct 55 ms 900 KB Output is correct
8 Correct 52 ms 832 KB Output is correct
9 Correct 67 ms 848 KB Output is correct
10 Correct 52 ms 852 KB Output is correct
11 Correct 53 ms 2564 KB Output is correct
12 Correct 54 ms 2392 KB Output is correct
13 Correct 56 ms 2368 KB Output is correct
14 Correct 59 ms 2556 KB Output is correct
15 Correct 55 ms 2496 KB Output is correct
16 Correct 53 ms 2384 KB Output is correct
17 Correct 54 ms 2376 KB Output is correct
18 Correct 52 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 848 KB Output is correct
2 Correct 62 ms 904 KB Output is correct
3 Correct 62 ms 796 KB Output is correct
4 Correct 50 ms 1104 KB Output is correct
5 Correct 64 ms 848 KB Output is correct
6 Correct 50 ms 928 KB Output is correct
7 Correct 55 ms 900 KB Output is correct
8 Correct 52 ms 832 KB Output is correct
9 Correct 67 ms 848 KB Output is correct
10 Correct 52 ms 852 KB Output is correct
11 Correct 53 ms 2564 KB Output is correct
12 Correct 54 ms 2392 KB Output is correct
13 Correct 56 ms 2368 KB Output is correct
14 Correct 59 ms 2556 KB Output is correct
15 Correct 55 ms 2496 KB Output is correct
16 Correct 53 ms 2384 KB Output is correct
17 Correct 54 ms 2376 KB Output is correct
18 Correct 52 ms 2384 KB Output is correct
19 Correct 332 ms 37040 KB Output is correct
20 Correct 248 ms 37016 KB Output is correct
21 Correct 174 ms 35680 KB Output is correct
22 Correct 164 ms 32100 KB Output is correct
23 Correct 90 ms 6736 KB Output is correct
24 Correct 89 ms 4948 KB Output is correct
25 Correct 177 ms 37200 KB Output is correct
26 Correct 241 ms 37180 KB Output is correct
27 Correct 221 ms 36988 KB Output is correct
28 Correct 210 ms 37192 KB Output is correct
29 Correct 196 ms 36168 KB Output is correct
30 Correct 118 ms 6736 KB Output is correct
31 Correct 227 ms 37036 KB Output is correct
32 Correct 184 ms 34196 KB Output is correct
33 Correct 100 ms 7024 KB Output is correct
34 Correct 219 ms 37004 KB Output is correct
35 Correct 152 ms 28280 KB Output is correct
36 Correct 120 ms 4996 KB Output is correct
37 Correct 95 ms 4856 KB Output is correct
38 Correct 209 ms 36940 KB Output is correct
39 Correct 117 ms 37172 KB Output is correct
40 Correct 164 ms 26412 KB Output is correct
41 Correct 316 ms 37192 KB Output is correct
42 Correct 62 ms 36428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 848 KB Output is correct
2 Correct 62 ms 904 KB Output is correct
3 Correct 62 ms 796 KB Output is correct
4 Correct 50 ms 1104 KB Output is correct
5 Correct 64 ms 848 KB Output is correct
6 Correct 50 ms 928 KB Output is correct
7 Correct 55 ms 900 KB Output is correct
8 Correct 52 ms 832 KB Output is correct
9 Correct 67 ms 848 KB Output is correct
10 Correct 52 ms 852 KB Output is correct
11 Correct 53 ms 2564 KB Output is correct
12 Correct 54 ms 2392 KB Output is correct
13 Correct 56 ms 2368 KB Output is correct
14 Correct 59 ms 2556 KB Output is correct
15 Correct 55 ms 2496 KB Output is correct
16 Correct 53 ms 2384 KB Output is correct
17 Correct 54 ms 2376 KB Output is correct
18 Correct 52 ms 2384 KB Output is correct
19 Correct 69 ms 4436 KB Output is correct
20 Correct 66 ms 2384 KB Output is correct
21 Correct 54 ms 2536 KB Output is correct
22 Correct 67 ms 2132 KB Output is correct
23 Correct 63 ms 2380 KB Output is correct
24 Correct 55 ms 2384 KB Output is correct
25 Correct 55 ms 2388 KB Output is correct
26 Correct 66 ms 2284 KB Output is correct
27 Correct 54 ms 2384 KB Output is correct
28 Correct 48 ms 2296 KB Output is correct
29 Correct 55 ms 2384 KB Output is correct
30 Correct 42 ms 2388 KB Output is correct
31 Correct 57 ms 2388 KB Output is correct
32 Correct 52 ms 2388 KB Output is correct
33 Correct 57 ms 2396 KB Output is correct
34 Correct 53 ms 2388 KB Output is correct
35 Correct 54 ms 2496 KB Output is correct
36 Correct 53 ms 2388 KB Output is correct
37 Correct 56 ms 2568 KB Output is correct
38 Correct 59 ms 2404 KB Output is correct
39 Correct 58 ms 2388 KB Output is correct
40 Correct 52 ms 2384 KB Output is correct
41 Correct 60 ms 2384 KB Output is correct
42 Correct 57 ms 2452 KB Output is correct
43 Correct 55 ms 2384 KB Output is correct
44 Correct 73 ms 2420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 848 KB Output is correct
2 Correct 62 ms 904 KB Output is correct
3 Correct 62 ms 796 KB Output is correct
4 Correct 50 ms 1104 KB Output is correct
5 Correct 64 ms 848 KB Output is correct
6 Correct 50 ms 928 KB Output is correct
7 Correct 55 ms 900 KB Output is correct
8 Correct 52 ms 832 KB Output is correct
9 Correct 67 ms 848 KB Output is correct
10 Correct 52 ms 852 KB Output is correct
11 Correct 53 ms 2564 KB Output is correct
12 Correct 54 ms 2392 KB Output is correct
13 Correct 56 ms 2368 KB Output is correct
14 Correct 59 ms 2556 KB Output is correct
15 Correct 55 ms 2496 KB Output is correct
16 Correct 53 ms 2384 KB Output is correct
17 Correct 54 ms 2376 KB Output is correct
18 Correct 52 ms 2384 KB Output is correct
19 Correct 332 ms 37040 KB Output is correct
20 Correct 248 ms 37016 KB Output is correct
21 Correct 174 ms 35680 KB Output is correct
22 Correct 164 ms 32100 KB Output is correct
23 Correct 90 ms 6736 KB Output is correct
24 Correct 89 ms 4948 KB Output is correct
25 Correct 177 ms 37200 KB Output is correct
26 Correct 241 ms 37180 KB Output is correct
27 Correct 221 ms 36988 KB Output is correct
28 Correct 210 ms 37192 KB Output is correct
29 Correct 196 ms 36168 KB Output is correct
30 Correct 118 ms 6736 KB Output is correct
31 Correct 227 ms 37036 KB Output is correct
32 Correct 184 ms 34196 KB Output is correct
33 Correct 100 ms 7024 KB Output is correct
34 Correct 219 ms 37004 KB Output is correct
35 Correct 152 ms 28280 KB Output is correct
36 Correct 120 ms 4996 KB Output is correct
37 Correct 95 ms 4856 KB Output is correct
38 Correct 209 ms 36940 KB Output is correct
39 Correct 117 ms 37172 KB Output is correct
40 Correct 164 ms 26412 KB Output is correct
41 Correct 316 ms 37192 KB Output is correct
42 Correct 62 ms 36428 KB Output is correct
43 Correct 69 ms 4436 KB Output is correct
44 Correct 66 ms 2384 KB Output is correct
45 Correct 54 ms 2536 KB Output is correct
46 Correct 67 ms 2132 KB Output is correct
47 Correct 63 ms 2380 KB Output is correct
48 Correct 55 ms 2384 KB Output is correct
49 Correct 55 ms 2388 KB Output is correct
50 Correct 66 ms 2284 KB Output is correct
51 Correct 54 ms 2384 KB Output is correct
52 Correct 48 ms 2296 KB Output is correct
53 Correct 55 ms 2384 KB Output is correct
54 Correct 42 ms 2388 KB Output is correct
55 Correct 57 ms 2388 KB Output is correct
56 Correct 52 ms 2388 KB Output is correct
57 Correct 57 ms 2396 KB Output is correct
58 Correct 53 ms 2388 KB Output is correct
59 Correct 54 ms 2496 KB Output is correct
60 Correct 53 ms 2388 KB Output is correct
61 Correct 56 ms 2568 KB Output is correct
62 Correct 59 ms 2404 KB Output is correct
63 Correct 58 ms 2388 KB Output is correct
64 Correct 52 ms 2384 KB Output is correct
65 Correct 60 ms 2384 KB Output is correct
66 Correct 57 ms 2452 KB Output is correct
67 Correct 55 ms 2384 KB Output is correct
68 Correct 73 ms 2420 KB Output is correct
69 Correct 408 ms 34260 KB Output is correct
70 Correct 428 ms 40448 KB Output is correct
71 Correct 105 ms 6964 KB Output is correct
72 Correct 95 ms 4948 KB Output is correct
73 Correct 94 ms 4984 KB Output is correct
74 Correct 181 ms 32044 KB Output is correct
75 Correct 95 ms 6736 KB Output is correct
76 Correct 207 ms 38084 KB Output is correct
77 Correct 190 ms 32248 KB Output is correct
78 Correct 98 ms 4940 KB Output is correct
79 Correct 98 ms 5048 KB Output is correct
80 Correct 310 ms 29832 KB Output is correct
81 Correct 111 ms 5204 KB Output is correct
82 Correct 375 ms 40484 KB Output is correct
83 Correct 375 ms 38148 KB Output is correct
84 Correct 108 ms 5208 KB Output is correct
85 Correct 109 ms 5240 KB Output is correct
86 Correct 259 ms 31736 KB Output is correct
87 Correct 293 ms 40440 KB Output is correct
88 Correct 114 ms 6992 KB Output is correct
89 Correct 260 ms 35872 KB Output is correct
90 Correct 288 ms 40592 KB Output is correct
91 Correct 108 ms 5204 KB Output is correct
92 Correct 296 ms 31160 KB Output is correct
93 Correct 108 ms 5156 KB Output is correct
94 Correct 117 ms 4944 KB Output is correct
95 Correct 114 ms 5120 KB Output is correct
96 Correct 222 ms 36928 KB Output is correct
97 Correct 179 ms 40692 KB Output is correct
98 Correct 291 ms 29096 KB Output is correct
99 Correct 501 ms 40692 KB Output is correct
100 Correct 218 ms 39876 KB Output is correct