Submission #715786

# Submission time Handle Problem Language Result Execution time Memory
715786 2023-03-28T00:58:23 Z cig32 Crossing (JOI21_crossing) C++17
100 / 100
1228 ms 24016 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 2e5 + 10;
int MOD;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
  if(p==0) return 1 % MOD;
  int r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
int inv(int b) { 
  return bm(b, MOD-2);
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
string gene(string a, string b) {
  string c;
  for(int i=0; i<a.size(); i++) {
    if(a[i] == b[i]) c += a[i];
    else {
      string t = "JOI";
      for(int j=0; j<3; j++) {
        if(a[i] != t[j] && b[i] != t[j]) c += t[j];
      }
    }
  }
  return c;
}
int random_prime(int l, int r) {
  while(1) {
    int x = rnd(l, r);
    int c = 0;
    for(int i=1; i*i<=x; i++) {
      if(x%i == 0) {
        c+=2;
        if(i*i == x) c--;
      }
    }
    if(c==2) return x;
  }
  return 1226;
}
int geometric_sequence_sum(int a, int x, int n) {
  // S = ax^0+ax^1+...+ax^(n-1)
  // xS = ax^1+ax^2+...+ax^n
  // (x-1)S = ax^n-a
  // S = a(x^n-1)/(x-1)
  
  int s = a;
  s *= bm(x, n) - 1;
  s %= MOD;
  s *= inv(x - 1);
  s %= MOD;
  return s;
}
int ans, n;
void add_segment(int l, int r, int v) {
  ans += geometric_sequence_sum(bm(3, n-r-1), 3, r-l+1) * v;
  ans %= MOD;
}
void sub_segment(int l, int r, int v) {
  ans -= geometric_sequence_sum(bm(3, n-r-1), 3, r-l+1) * v;
  ans += (MOD << 1);
  ans %= MOD;
}
void solve(int tc) {
  MOD = random_prime(5e8, 1.25 * 1e9);
  cin >> n;
  string s[9];
  cin >> s[0] >> s[1] >> s[2];
  s[3] = gene(s[0], s[1]);
  s[4] = gene(s[0], s[2]);
  s[5] = gene(s[1], s[2]);
  s[6] = gene(s[2], s[3]);
  s[7] = gene(s[1], s[4]);
  s[8] = gene(s[0], s[5]);
  int h[9];
  for(int i=0; i<9; i++) {
    h[i] = 0;
    for(int j=0; j<n; j++) {
      h[i] = (h[i] * 3 + (s[i][j] == 'J' ? 0 : (s[i][j] == 'O' ? 1 : 2))) % MOD;
    }
  }
  int q;
  cin >> q;
  string def;
  cin >> def;
  for(char c: def) ans = (ans * 3 + (c == 'J' ? 0 : (c == 'O' ? 1 : 2))) % MOD;
  bool yes = 0;
  for(int i=0; i<9; i++) yes |= (ans == h[i]);
  cout << (yes ? "Yes\n" : "No\n");
  set<pair<pair<int,int>,int> > ranges;
  for(int i=0; i<def.size(); i++) {
    int j = i;
    while(j+1 < def.size() && def[j+1] == def[i]) j++;
    ranges.insert({{i, j}, (def[i] == 'J' ? 0 : (def[i] == 'O' ? 1 : 2))});
    i = j;
  }
  for(int i=0; i<q; i++) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    char c;
    cin >> c;
    auto it = ranges.lower_bound({{l, 0}, 0});
    if(it != ranges.begin()) it--;
    vector<pair<pair<int,int>,int> > to_add, to_sub;
    to_add.push_back({{l, r}, (c == 'J' ? 0 : (c == 'O' ? 1 : 2))});
    while(it != ranges.end() && (*it).first.first <= r) {
      if((*it).first.second >= l) { // relevant
        to_sub.push_back(*it);
        if((*it).first.first < l) {
          to_add.push_back({{(*it).first.first, l-1}, (*it).second});
        }
        if((*it).first.second > r) {
          to_add.push_back({{r+1, (*it).first.second}, (*it).second});
        }
      }
      it++;
    }
    for(auto x: to_sub) {
      ranges.erase(x);
      sub_segment(x.first.first, x.first.second, x.second);
    }
    for(auto x: to_add) {
      ranges.insert(x);
      add_segment(x.first.first, x.first.second, x.second);
    }
    bool yes = 0;
    for(int i=0; i<9; i++) {
      if(h[i] == ans) yes = 1;
    }
    cout << (yes ? "Yes\n" : "No\n");
  }

}
int32_t main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}

Compilation message

Main.cpp: In function 'std::string gene(std::string, std::string)':
Main.cpp:25:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |   for(int i=0; i<a.size(); i++) {
      |                ~^~~~~~~~~
Main.cpp: In function 'void solve(long long int)':
Main.cpp:100:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |   for(int i=0; i<def.size(); i++) {
      |                ~^~~~~~~~~~~
Main.cpp:102:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |     while(j+1 < def.size() && def[j+1] == def[i]) j++;
      |           ~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 539 ms 828 KB Output is correct
2 Correct 654 ms 1020 KB Output is correct
3 Correct 535 ms 1024 KB Output is correct
4 Correct 504 ms 988 KB Output is correct
5 Correct 495 ms 2380 KB Output is correct
6 Correct 498 ms 2256 KB Output is correct
7 Correct 525 ms 2384 KB Output is correct
8 Correct 538 ms 2392 KB Output is correct
9 Correct 523 ms 2472 KB Output is correct
10 Correct 523 ms 2432 KB Output is correct
11 Correct 515 ms 2520 KB Output is correct
12 Correct 512 ms 2380 KB Output is correct
13 Correct 502 ms 2432 KB Output is correct
14 Correct 518 ms 2452 KB Output is correct
15 Correct 527 ms 2360 KB Output is correct
16 Correct 520 ms 2384 KB Output is correct
17 Correct 517 ms 2620 KB Output is correct
18 Correct 476 ms 2372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 539 ms 828 KB Output is correct
2 Correct 654 ms 1020 KB Output is correct
3 Correct 535 ms 1024 KB Output is correct
4 Correct 504 ms 988 KB Output is correct
5 Correct 495 ms 2380 KB Output is correct
6 Correct 498 ms 2256 KB Output is correct
7 Correct 525 ms 2384 KB Output is correct
8 Correct 538 ms 2392 KB Output is correct
9 Correct 523 ms 2472 KB Output is correct
10 Correct 523 ms 2432 KB Output is correct
11 Correct 515 ms 2520 KB Output is correct
12 Correct 512 ms 2380 KB Output is correct
13 Correct 502 ms 2432 KB Output is correct
14 Correct 518 ms 2452 KB Output is correct
15 Correct 527 ms 2360 KB Output is correct
16 Correct 520 ms 2384 KB Output is correct
17 Correct 517 ms 2620 KB Output is correct
18 Correct 476 ms 2372 KB Output is correct
19 Correct 1122 ms 15260 KB Output is correct
20 Correct 1120 ms 15144 KB Output is correct
21 Correct 982 ms 18452 KB Output is correct
22 Correct 948 ms 17948 KB Output is correct
23 Correct 718 ms 4132 KB Output is correct
24 Correct 699 ms 4340 KB Output is correct
25 Correct 963 ms 24016 KB Output is correct
26 Correct 1085 ms 17860 KB Output is correct
27 Correct 850 ms 7364 KB Output is correct
28 Correct 847 ms 7480 KB Output is correct
29 Correct 804 ms 7104 KB Output is correct
30 Correct 711 ms 3700 KB Output is correct
31 Correct 818 ms 7384 KB Output is correct
32 Correct 839 ms 7548 KB Output is correct
33 Correct 715 ms 4120 KB Output is correct
34 Correct 974 ms 17880 KB Output is correct
35 Correct 889 ms 16100 KB Output is correct
36 Correct 733 ms 4212 KB Output is correct
37 Correct 765 ms 4116 KB Output is correct
38 Correct 979 ms 14864 KB Output is correct
39 Correct 712 ms 14860 KB Output is correct
40 Correct 1098 ms 16128 KB Output is correct
41 Correct 1228 ms 17888 KB Output is correct
42 Correct 460 ms 17900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 539 ms 828 KB Output is correct
2 Correct 654 ms 1020 KB Output is correct
3 Correct 535 ms 1024 KB Output is correct
4 Correct 504 ms 988 KB Output is correct
5 Correct 495 ms 2380 KB Output is correct
6 Correct 498 ms 2256 KB Output is correct
7 Correct 525 ms 2384 KB Output is correct
8 Correct 538 ms 2392 KB Output is correct
9 Correct 523 ms 2472 KB Output is correct
10 Correct 523 ms 2432 KB Output is correct
11 Correct 515 ms 2520 KB Output is correct
12 Correct 512 ms 2380 KB Output is correct
13 Correct 502 ms 2432 KB Output is correct
14 Correct 518 ms 2452 KB Output is correct
15 Correct 527 ms 2360 KB Output is correct
16 Correct 520 ms 2384 KB Output is correct
17 Correct 517 ms 2620 KB Output is correct
18 Correct 476 ms 2372 KB Output is correct
19 Correct 634 ms 2252 KB Output is correct
20 Correct 568 ms 2328 KB Output is correct
21 Correct 570 ms 2480 KB Output is correct
22 Correct 501 ms 2248 KB Output is correct
23 Correct 558 ms 2408 KB Output is correct
24 Correct 556 ms 2388 KB Output is correct
25 Correct 562 ms 2556 KB Output is correct
26 Correct 477 ms 2416 KB Output is correct
27 Correct 597 ms 2556 KB Output is correct
28 Correct 476 ms 2144 KB Output is correct
29 Correct 551 ms 2420 KB Output is correct
30 Correct 509 ms 2272 KB Output is correct
31 Correct 527 ms 2380 KB Output is correct
32 Correct 518 ms 2432 KB Output is correct
33 Correct 525 ms 2372 KB Output is correct
34 Correct 464 ms 2380 KB Output is correct
35 Correct 511 ms 2528 KB Output is correct
36 Correct 518 ms 2560 KB Output is correct
37 Correct 532 ms 2420 KB Output is correct
38 Correct 530 ms 2416 KB Output is correct
39 Correct 532 ms 2552 KB Output is correct
40 Correct 531 ms 2508 KB Output is correct
41 Correct 500 ms 2496 KB Output is correct
42 Correct 511 ms 2504 KB Output is correct
43 Correct 519 ms 2332 KB Output is correct
44 Correct 541 ms 2564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 539 ms 828 KB Output is correct
2 Correct 654 ms 1020 KB Output is correct
3 Correct 535 ms 1024 KB Output is correct
4 Correct 504 ms 988 KB Output is correct
5 Correct 495 ms 2380 KB Output is correct
6 Correct 498 ms 2256 KB Output is correct
7 Correct 525 ms 2384 KB Output is correct
8 Correct 538 ms 2392 KB Output is correct
9 Correct 523 ms 2472 KB Output is correct
10 Correct 523 ms 2432 KB Output is correct
11 Correct 515 ms 2520 KB Output is correct
12 Correct 512 ms 2380 KB Output is correct
13 Correct 502 ms 2432 KB Output is correct
14 Correct 518 ms 2452 KB Output is correct
15 Correct 527 ms 2360 KB Output is correct
16 Correct 520 ms 2384 KB Output is correct
17 Correct 517 ms 2620 KB Output is correct
18 Correct 476 ms 2372 KB Output is correct
19 Correct 1122 ms 15260 KB Output is correct
20 Correct 1120 ms 15144 KB Output is correct
21 Correct 982 ms 18452 KB Output is correct
22 Correct 948 ms 17948 KB Output is correct
23 Correct 718 ms 4132 KB Output is correct
24 Correct 699 ms 4340 KB Output is correct
25 Correct 963 ms 24016 KB Output is correct
26 Correct 1085 ms 17860 KB Output is correct
27 Correct 850 ms 7364 KB Output is correct
28 Correct 847 ms 7480 KB Output is correct
29 Correct 804 ms 7104 KB Output is correct
30 Correct 711 ms 3700 KB Output is correct
31 Correct 818 ms 7384 KB Output is correct
32 Correct 839 ms 7548 KB Output is correct
33 Correct 715 ms 4120 KB Output is correct
34 Correct 974 ms 17880 KB Output is correct
35 Correct 889 ms 16100 KB Output is correct
36 Correct 733 ms 4212 KB Output is correct
37 Correct 765 ms 4116 KB Output is correct
38 Correct 979 ms 14864 KB Output is correct
39 Correct 712 ms 14860 KB Output is correct
40 Correct 1098 ms 16128 KB Output is correct
41 Correct 1228 ms 17888 KB Output is correct
42 Correct 460 ms 17900 KB Output is correct
43 Correct 634 ms 2252 KB Output is correct
44 Correct 568 ms 2328 KB Output is correct
45 Correct 570 ms 2480 KB Output is correct
46 Correct 501 ms 2248 KB Output is correct
47 Correct 558 ms 2408 KB Output is correct
48 Correct 556 ms 2388 KB Output is correct
49 Correct 562 ms 2556 KB Output is correct
50 Correct 477 ms 2416 KB Output is correct
51 Correct 597 ms 2556 KB Output is correct
52 Correct 476 ms 2144 KB Output is correct
53 Correct 551 ms 2420 KB Output is correct
54 Correct 509 ms 2272 KB Output is correct
55 Correct 527 ms 2380 KB Output is correct
56 Correct 518 ms 2432 KB Output is correct
57 Correct 525 ms 2372 KB Output is correct
58 Correct 464 ms 2380 KB Output is correct
59 Correct 511 ms 2528 KB Output is correct
60 Correct 518 ms 2560 KB Output is correct
61 Correct 532 ms 2420 KB Output is correct
62 Correct 530 ms 2416 KB Output is correct
63 Correct 532 ms 2552 KB Output is correct
64 Correct 531 ms 2508 KB Output is correct
65 Correct 500 ms 2496 KB Output is correct
66 Correct 511 ms 2504 KB Output is correct
67 Correct 519 ms 2332 KB Output is correct
68 Correct 541 ms 2564 KB Output is correct
69 Correct 1050 ms 12896 KB Output is correct
70 Correct 1109 ms 15128 KB Output is correct
71 Correct 735 ms 4108 KB Output is correct
72 Correct 720 ms 4280 KB Output is correct
73 Correct 698 ms 4264 KB Output is correct
74 Correct 866 ms 16944 KB Output is correct
75 Correct 690 ms 4072 KB Output is correct
76 Correct 1045 ms 22580 KB Output is correct
77 Correct 1060 ms 15880 KB Output is correct
78 Correct 720 ms 4132 KB Output is correct
79 Correct 763 ms 4136 KB Output is correct
80 Correct 961 ms 14912 KB Output is correct
81 Correct 707 ms 4148 KB Output is correct
82 Correct 1012 ms 22512 KB Output is correct
83 Correct 1020 ms 18436 KB Output is correct
84 Correct 684 ms 4168 KB Output is correct
85 Correct 724 ms 4184 KB Output is correct
86 Correct 879 ms 12356 KB Output is correct
87 Correct 900 ms 7416 KB Output is correct
88 Correct 713 ms 3940 KB Output is correct
89 Correct 854 ms 7788 KB Output is correct
90 Correct 945 ms 8940 KB Output is correct
91 Correct 744 ms 3944 KB Output is correct
92 Correct 1004 ms 16184 KB Output is correct
93 Correct 746 ms 4208 KB Output is correct
94 Correct 741 ms 4192 KB Output is correct
95 Correct 721 ms 4440 KB Output is correct
96 Correct 884 ms 6808 KB Output is correct
97 Correct 747 ms 14976 KB Output is correct
98 Correct 1055 ms 15308 KB Output is correct
99 Correct 1228 ms 17932 KB Output is correct
100 Correct 428 ms 17876 KB Output is correct