답안 #1037111

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1037111 2024-07-28T06:17:27 Z juicy Crossing (JOI21_crossing) C++17
100 / 100
331 ms 31584 KB
#include <bits/stdc++.h>

using namespace std;

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

const int N = 2e5 + 5;

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

int rand(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

bool prime(int N) {
  for (int i = 2; i * i <= N; ++i) {
    if (N % i == 0) {
      return 0;
    }
  }
  return 1;
}

int rand_mod() {
  int x = rand(1e9, 2e9);
  while (!prime(x)) {
    --x;
  }
  return x;
}

int rand_base() {
  int x = rand(13, 999999);
  while (!prime(x)) {
    --x;
  }
  return x;
}

int n, q;
array<int, 3> MD{rand_mod(), rand_mod(), rand_mod()}, base{rand_base(), rand_base(), rand_base()};
array<int, 3> pw[N], pf[N], sum[4 * N], s[4 * N];
int lz[4 * N];
map<array<int, 3>, bool> mp;
string S;

int add(int x, int y, int c) {
  return ((long long) x + y) % MD[c];
}

int conv(char c) {
  return c == 'J' ? 1 : (c == 'O' ? 2 : 3);
}

void pull(int id, int l, int r) {
  int md = (l + r) / 2; 
  for (int i = 0; i < 3; ++i) {
    s[id][i] = add((long long) s[id * 2][i] * pw[r - md][i] % MD[i], s[id * 2 + 1][i], i);
  }
}

void bld(int id = 1, int l = 1, int r = n) {
  for (int i = 0; i < 3; ++i) {
    sum[id][i] = pf[r - l][i];
  }
  if (l == r) {
    int x = conv(S[l - 1]);
    s[id] = {x, x, x};
    return;
  }
  int md = (l + r) / 2;
  bld(id * 2, l, md);
  bld(id * 2 + 1, md + 1, r);
  pull(id, l, r);
}

void app(int id, int x) {
  for (int i = 0; i < 3; ++i) {
    s[id][i] = (long long) sum[id][i] * x % MD[i];
  }
  lz[id] = x;
}

void psh(int id) {
  if (lz[id]) {
    app(id * 2, lz[id]);
    app(id * 2 + 1, lz[id]);
    lz[id] = 0;
  }
}

void upd(int u, int v, int x, int id = 1, int l = 1, int r = n) {
  if (u <= l && r <= v) {
    app(id, x);
    return;
  }
  psh(id);
  int md = (l + r) / 2;
  if (u <= md) {
    upd(u, v, x, id * 2, l, md);
  } 
  if (md < v) {
    upd(u, v, x, id * 2 + 1, md + 1, r);
  }
  pull(id, l, r);
}

string cross(const string &a, const string &b) {
  string res;
  for (int i = 0; i < n; ++i) {
    if (a[i] == b[i]) {
      res += a[i];
    } else {
      for (auto c : {'J', 'O', 'I'}) {
        if (c != a[i] && c != b[i]) {
          res += c;
        }
      }
    }
  }
  return res;
}

array<int, 3> get(const string &s) {
  array<int, 3> h{}; 
  for (char c : s) {
    for (int i = 0; i < 3; ++i) {
      h[i] = add((long long) h[i] * base[i] % MD[i], conv(c), i); 
    }
  }
  return h;
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n;
  vector<string> st;
  for (int i = 0; i < 3; ++i) {
    string s; cin >> s;
    st.push_back(s);
    mp[get(s)] = 1;
  }  
  for (int i = 0; i < 3; ++i) {
    for (int j = i + 1; j < 3; ++j) {
      auto mix = cross(st[i], st[j]);
      auto h = get(mix);
      if (!mp.count(h)) {
        mp[h] = 1;
        st.push_back(mix);
      }
    }
  }
  bool flg = 1;
  while (flg) {
    flg = 0;
    for (int i = 0; i + 1 < st.size(); ++i) {
      auto mix = cross(st[i], st.back());
      auto h = get(mix);
      if (!mp.count(h)) {
        st.push_back(mix);
        mp[h] = 1;
        flg = 1;
        break;
      }
    }
  }
  for (auto it : {0, 1, 2}) {
    pw[0][it] = pf[0][it] = 1;
    for (int i = 1; i <= n; ++i) {
      pw[i][it] = (long long) pw[i - 1][it] * base[it] % MD[it];
      pf[i][it] = add(pf[i - 1][it], pw[i][it], it);
    }
  }
  cin >> q >> S;
  bld();
  auto qry = [&]() {
    cout << (mp.count(s[1]) ? "Yes" : "No") << "\n";
  };
  qry();
  while (q--) {
    int l, r; char c; cin >> l >> r >> c;
    upd(l, r, conv(c));
    qry();
  }
  return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:161:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  161 |     for (int i = 0; i + 1 < st.size(); ++i) {
      |                     ~~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 9040 KB Output is correct
2 Correct 70 ms 9036 KB Output is correct
3 Correct 78 ms 9124 KB Output is correct
4 Correct 51 ms 9044 KB Output is correct
5 Correct 50 ms 9172 KB Output is correct
6 Correct 51 ms 9200 KB Output is correct
7 Correct 50 ms 9052 KB Output is correct
8 Correct 69 ms 9040 KB Output is correct
9 Correct 55 ms 9268 KB Output is correct
10 Correct 63 ms 9124 KB Output is correct
11 Correct 55 ms 9048 KB Output is correct
12 Correct 54 ms 9044 KB Output is correct
13 Correct 55 ms 9040 KB Output is correct
14 Correct 56 ms 9040 KB Output is correct
15 Correct 59 ms 9044 KB Output is correct
16 Correct 56 ms 9044 KB Output is correct
17 Correct 62 ms 9040 KB Output is correct
18 Correct 79 ms 9088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 9040 KB Output is correct
2 Correct 70 ms 9036 KB Output is correct
3 Correct 78 ms 9124 KB Output is correct
4 Correct 51 ms 9044 KB Output is correct
5 Correct 50 ms 9172 KB Output is correct
6 Correct 51 ms 9200 KB Output is correct
7 Correct 50 ms 9052 KB Output is correct
8 Correct 69 ms 9040 KB Output is correct
9 Correct 55 ms 9268 KB Output is correct
10 Correct 63 ms 9124 KB Output is correct
11 Correct 55 ms 9048 KB Output is correct
12 Correct 54 ms 9044 KB Output is correct
13 Correct 55 ms 9040 KB Output is correct
14 Correct 56 ms 9040 KB Output is correct
15 Correct 59 ms 9044 KB Output is correct
16 Correct 56 ms 9044 KB Output is correct
17 Correct 62 ms 9040 KB Output is correct
18 Correct 79 ms 9088 KB Output is correct
19 Correct 234 ms 26416 KB Output is correct
20 Correct 254 ms 24876 KB Output is correct
21 Correct 159 ms 26348 KB Output is correct
22 Correct 146 ms 29376 KB Output is correct
23 Correct 108 ms 11604 KB Output is correct
24 Correct 100 ms 11604 KB Output is correct
25 Correct 171 ms 30284 KB Output is correct
26 Correct 162 ms 30288 KB Output is correct
27 Correct 195 ms 30292 KB Output is correct
28 Correct 189 ms 30292 KB Output is correct
29 Correct 186 ms 30304 KB Output is correct
30 Correct 115 ms 11604 KB Output is correct
31 Correct 192 ms 30240 KB Output is correct
32 Correct 182 ms 29816 KB Output is correct
33 Correct 104 ms 11604 KB Output is correct
34 Correct 189 ms 30296 KB Output is correct
35 Correct 143 ms 28820 KB Output is correct
36 Correct 103 ms 11604 KB Output is correct
37 Correct 106 ms 11600 KB Output is correct
38 Correct 229 ms 30040 KB Output is correct
39 Correct 134 ms 28024 KB Output is correct
40 Correct 146 ms 20552 KB Output is correct
41 Correct 249 ms 30548 KB Output is correct
42 Correct 65 ms 27480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 9040 KB Output is correct
2 Correct 70 ms 9036 KB Output is correct
3 Correct 78 ms 9124 KB Output is correct
4 Correct 51 ms 9044 KB Output is correct
5 Correct 50 ms 9172 KB Output is correct
6 Correct 51 ms 9200 KB Output is correct
7 Correct 50 ms 9052 KB Output is correct
8 Correct 69 ms 9040 KB Output is correct
9 Correct 55 ms 9268 KB Output is correct
10 Correct 63 ms 9124 KB Output is correct
11 Correct 55 ms 9048 KB Output is correct
12 Correct 54 ms 9044 KB Output is correct
13 Correct 55 ms 9040 KB Output is correct
14 Correct 56 ms 9040 KB Output is correct
15 Correct 59 ms 9044 KB Output is correct
16 Correct 56 ms 9044 KB Output is correct
17 Correct 62 ms 9040 KB Output is correct
18 Correct 79 ms 9088 KB Output is correct
19 Correct 69 ms 9216 KB Output is correct
20 Correct 75 ms 9072 KB Output is correct
21 Correct 53 ms 9044 KB Output is correct
22 Correct 55 ms 9140 KB Output is correct
23 Correct 59 ms 9040 KB Output is correct
24 Correct 60 ms 9040 KB Output is correct
25 Correct 62 ms 9044 KB Output is correct
26 Correct 58 ms 9240 KB Output is correct
27 Correct 64 ms 9044 KB Output is correct
28 Correct 55 ms 9072 KB Output is correct
29 Correct 59 ms 9052 KB Output is correct
30 Correct 54 ms 9044 KB Output is correct
31 Correct 63 ms 9140 KB Output is correct
32 Correct 60 ms 9044 KB Output is correct
33 Correct 63 ms 9092 KB Output is correct
34 Correct 57 ms 9044 KB Output is correct
35 Correct 59 ms 9040 KB Output is correct
36 Correct 68 ms 9044 KB Output is correct
37 Correct 63 ms 9044 KB Output is correct
38 Correct 69 ms 9268 KB Output is correct
39 Correct 67 ms 9264 KB Output is correct
40 Correct 64 ms 9040 KB Output is correct
41 Correct 63 ms 9256 KB Output is correct
42 Correct 65 ms 9040 KB Output is correct
43 Correct 61 ms 9056 KB Output is correct
44 Correct 65 ms 9044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 9040 KB Output is correct
2 Correct 70 ms 9036 KB Output is correct
3 Correct 78 ms 9124 KB Output is correct
4 Correct 51 ms 9044 KB Output is correct
5 Correct 50 ms 9172 KB Output is correct
6 Correct 51 ms 9200 KB Output is correct
7 Correct 50 ms 9052 KB Output is correct
8 Correct 69 ms 9040 KB Output is correct
9 Correct 55 ms 9268 KB Output is correct
10 Correct 63 ms 9124 KB Output is correct
11 Correct 55 ms 9048 KB Output is correct
12 Correct 54 ms 9044 KB Output is correct
13 Correct 55 ms 9040 KB Output is correct
14 Correct 56 ms 9040 KB Output is correct
15 Correct 59 ms 9044 KB Output is correct
16 Correct 56 ms 9044 KB Output is correct
17 Correct 62 ms 9040 KB Output is correct
18 Correct 79 ms 9088 KB Output is correct
19 Correct 234 ms 26416 KB Output is correct
20 Correct 254 ms 24876 KB Output is correct
21 Correct 159 ms 26348 KB Output is correct
22 Correct 146 ms 29376 KB Output is correct
23 Correct 108 ms 11604 KB Output is correct
24 Correct 100 ms 11604 KB Output is correct
25 Correct 171 ms 30284 KB Output is correct
26 Correct 162 ms 30288 KB Output is correct
27 Correct 195 ms 30292 KB Output is correct
28 Correct 189 ms 30292 KB Output is correct
29 Correct 186 ms 30304 KB Output is correct
30 Correct 115 ms 11604 KB Output is correct
31 Correct 192 ms 30240 KB Output is correct
32 Correct 182 ms 29816 KB Output is correct
33 Correct 104 ms 11604 KB Output is correct
34 Correct 189 ms 30296 KB Output is correct
35 Correct 143 ms 28820 KB Output is correct
36 Correct 103 ms 11604 KB Output is correct
37 Correct 106 ms 11600 KB Output is correct
38 Correct 229 ms 30040 KB Output is correct
39 Correct 134 ms 28024 KB Output is correct
40 Correct 146 ms 20552 KB Output is correct
41 Correct 249 ms 30548 KB Output is correct
42 Correct 65 ms 27480 KB Output is correct
43 Correct 69 ms 9216 KB Output is correct
44 Correct 75 ms 9072 KB Output is correct
45 Correct 53 ms 9044 KB Output is correct
46 Correct 55 ms 9140 KB Output is correct
47 Correct 59 ms 9040 KB Output is correct
48 Correct 60 ms 9040 KB Output is correct
49 Correct 62 ms 9044 KB Output is correct
50 Correct 58 ms 9240 KB Output is correct
51 Correct 64 ms 9044 KB Output is correct
52 Correct 55 ms 9072 KB Output is correct
53 Correct 59 ms 9052 KB Output is correct
54 Correct 54 ms 9044 KB Output is correct
55 Correct 63 ms 9140 KB Output is correct
56 Correct 60 ms 9044 KB Output is correct
57 Correct 63 ms 9092 KB Output is correct
58 Correct 57 ms 9044 KB Output is correct
59 Correct 59 ms 9040 KB Output is correct
60 Correct 68 ms 9044 KB Output is correct
61 Correct 63 ms 9044 KB Output is correct
62 Correct 69 ms 9268 KB Output is correct
63 Correct 67 ms 9264 KB Output is correct
64 Correct 64 ms 9040 KB Output is correct
65 Correct 63 ms 9256 KB Output is correct
66 Correct 65 ms 9040 KB Output is correct
67 Correct 61 ms 9056 KB Output is correct
68 Correct 65 ms 9044 KB Output is correct
69 Correct 235 ms 30180 KB Output is correct
70 Correct 290 ms 29624 KB Output is correct
71 Correct 103 ms 11604 KB Output is correct
72 Correct 107 ms 11600 KB Output is correct
73 Correct 102 ms 11820 KB Output is correct
74 Correct 158 ms 29336 KB Output is correct
75 Correct 103 ms 11556 KB Output is correct
76 Correct 171 ms 30292 KB Output is correct
77 Correct 163 ms 29336 KB Output is correct
78 Correct 105 ms 11588 KB Output is correct
79 Correct 102 ms 11600 KB Output is correct
80 Correct 186 ms 29560 KB Output is correct
81 Correct 110 ms 11720 KB Output is correct
82 Correct 218 ms 31332 KB Output is correct
83 Correct 206 ms 31016 KB Output is correct
84 Correct 109 ms 11764 KB Output is correct
85 Correct 111 ms 11668 KB Output is correct
86 Correct 195 ms 29696 KB Output is correct
87 Correct 234 ms 31332 KB Output is correct
88 Correct 118 ms 11796 KB Output is correct
89 Correct 228 ms 30480 KB Output is correct
90 Correct 224 ms 31360 KB Output is correct
91 Correct 117 ms 11604 KB Output is correct
92 Correct 193 ms 29656 KB Output is correct
93 Correct 110 ms 11864 KB Output is correct
94 Correct 103 ms 11600 KB Output is correct
95 Correct 113 ms 11804 KB Output is correct
96 Correct 229 ms 30044 KB Output is correct
97 Correct 182 ms 29312 KB Output is correct
98 Correct 192 ms 21216 KB Output is correct
99 Correct 331 ms 31584 KB Output is correct
100 Correct 129 ms 28548 KB Output is correct