Submission #771176

# Submission time Handle Problem Language Result Execution time Memory
771176 2023-07-02T14:21:45 Z Sam_a17 Crossing (JOI21_crossing) C++17
100 / 100
641 ms 36764 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt __builtin_popcount
 
#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long

void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
 
// Indexed Set  
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
// template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(Tree <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
 

// for random generations
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 myrand(131);
 
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
 
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();
 
  if(str == "input") {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  } else if(str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  }
}
const long long mod = 1e9 + 7;


long long add(long long a, long long b) {
  return (a + b) % mod;
}

long long sub(long long a, long long b) {
  return (a - b) % mod;
}

long long mult(long long a, long long b) {
  return (a * b) % mod;
}


const int N = 2e5 + 10;
int n;
string s[3];

long long hi1[4][N];
long long hi2[4][N];

long long h1[N], h2[N];
long long pw1[N], pw2[N];
 

struct segTree {
  struct node {
    pair<long long, long long> value;
    int sz;
    long long  lazy;
  };
  vector<node> tree;
  int size = 1;
 
  void propagate(int x, int lx, int rx) {
    if(rx - lx == 1 || tree[x].lazy == -1) {
      return;
    }
 
    int mid = (lx + rx) / 2;
    tree[2 * x + 1].lazy = tree[x].lazy;
    int sz = tree[2 * x + 1].sz;
    tree[2 * x + 1].value = {hi1[tree[x].lazy][sz], hi2[tree[x].lazy][sz]};
 
    tree[2 * x + 2].lazy = tree[x].lazy;
    sz = tree[2 * x + 2].sz;
    tree[2 * x + 2].value = {hi1[tree[x].lazy][sz], hi2[tree[x].lazy][sz]};
 
    tree[x].lazy = -1;
  }

  node merge(node a, node b) {
    auto first = a;
    auto second = b;
    int sz2 = a.sz;

    second.value.first = mult(second.value.first, pw1[sz2]);
    second.value.second = mult(second.value.second, pw2[sz2]);
    
    first.value.first = add(first.value.first, second.value.first);
    first.value.second = add(first.value.second, second.value.second);

    first.sz += second.sz;

    first.lazy = -1;
    return first;
  }
  
  void build(int x, int lx, int rx, string& str) {
    tree[x].sz = rx - lx;

    if(rx - lx == 1) {
      if(lx < n) {
        tree[x].value = {str[lx] - 'a' + 1, str[lx] - 'a' + 1};
        tree[x].lazy = -1;
      }
    } else {
      int mid = (lx + rx) / 2;
      build(2 * x + 1, lx, mid, str);
      build(2 * x + 2, mid, rx, str);

      tree[x] = merge(tree[2 * x + 1], tree[2 * x + 2]);
    }
  }

  void init(long long s, string& str) {
    while(size < s) {
      size *= 2;
    }
    tree.assign(2 * size - 1, {{0, 0}, 0, -1});
    build(0, 0, size, str);
  }
 
  void upd(int l, int r, long long value, int x, int lx, int rx) {
    propagate(x, lx, rx);
    if(lx >= r || rx <= l) {
      return;
    }
 
    if(lx >= l && rx <= r) {
      int sz = tree[x].sz;
      tree[x].value = {hi1[value][sz], hi2[value][sz]};
      tree[x].lazy = value;
      propagate(x, lx, rx);
      return;
    }
 
    int mid = (lx + rx) / 2;
    upd(l, r, value, 2 * x + 1, lx, mid);
    upd(l, r, value, 2 * x + 2, mid, rx);
    
    // dbg(tree[2 * x + 1].value)
    // dbg(tree[2 * x + 2].value)
    tree[x] = merge(tree[2 * x + 1], tree[2 * x + 2]);
  }
 
  void upd(int l, int r, long long value) {
    upd(l, r, value, 0, 0, size);
  }
 
  node qry(int l, int r, int x, int lx, int rx) {
    propagate(x, lx, rx);
    if(lx >= r || rx <= l) {
      return {{0, 0}, 0, -1};
    }
 
    if(lx >= l && rx <= r) {
      return tree[x];
    }
 
    int mid = (lx + rx) / 2;
    node s1 = qry(l, r, 2 * x + 1, lx, mid);
    node s2 = qry(l, r, 2 * x + 2, mid, rx);
 
    return merge(s1, s2);
  }
 
  node qry(int l, int r) {
    return qry(l, r, 0, 0, size);
  }
};
segTree seg;

set<string> all;

long long compute1(int l, int r) {
  return (h1[l] - (pw1[r - l + 1] * h1[r + 1]) % mod + mod) % mod;
}
 
long long compute2(int l, int r) {
  return (h2[l] - (pw2[r - l + 1] * h2[r + 1]) % mod + mod) % mod;
}

string cross(string &a, string &b) {
  string res = "";
  for(int i = 0; i < n; i++) {
    if(a[i] == b[i]) {
      res += a[i];
    } else {
      set<char> st;
      
      st.insert('a');
      st.insert('b');
      st.insert('c');

      st.erase(a[i]);
      st.erase(b[i]);
      res += *st.begin();
    }
  }

  return res;
}


pair<long long, long long> compute_hsh(string cur) {
  vector<long long> hu1(n + 2, 0);
  vector<long long> hu2(n + 2, 0);
  for(int i = n - 1; i >= 0; i--) {
    hu1[i] = ((hu1[i + 1] * 53) % mod + cur[i] - 'a' + 1) % mod; 
    hu2[i] = ((hu2[i + 1] * 31) % mod + cur[i] - 'a' + 1) % mod; 
  }
  return {hu1[0], hu2[0]};
}


void solve_() {
  cin >> n;

  for(int i = 0; i < 3; i++) {
    cin >> s[i];
    for(int j = 0; j < n; j++) {
      if(s[i][j] == 'J') {
        s[i][j] = 'a';
      } else if(s[i][j] == 'O') {
        s[i][j] = 'b';
      } else {
        s[i][j] = 'c';
      }
    }
  }

  pw1[0] = pw2[0] = 1;
  for(int i = 1; i <= n; i++) {
    pw1[i] = (pw1[i - 1] * 53) % mod;
    pw2[i] = (pw2[i - 1] * 31) % mod; 
  }

  for(int i = 1; i <= 3; i++) {
    hi1[i][1] = i;
    hi2[i][1] = i;
    for(int j = 2; j <= n; j++) {
      hi1[i][j] = add(hi1[i][j - 1], mult(i, pw1[j - 1]));
      hi2[i][j] = add(hi2[i][j - 1], mult(i, pw2[j - 1]));
    }
  }


  set<string> st_all;
  for(int mask = 1; mask < (1 << 3); mask++) {
    vector<int> ind;
    for(int i = 0; i < 3; i++) {
      if(mask & (1 << i)) {
        ind.push_back(i);
      }
    }

    do {
      string si = s[ind[0]];
      for(int i = 1; i < sz(ind); i++) {
        si = cross(si, s[ind[i]]);
      }
      st_all.insert(si);
    } while(next_permutation(all(ind)));
  }

  set<pair<long long, long long>> all_hashis;
  for(auto i: st_all) {
    all_hashis.insert(compute_hsh(i));
  }

  int q; cin >> q;

  string k; cin >> k;

  for(int i = 0; i < n; i++) {
    if(k[i] == 'J') k[i] = 'a';
    if(k[i] == 'O') k[i] = 'b';
    if(k[i] == 'I') k[i] = 'c';
  }
  seg.init(n + 2, k);
  
  auto currr = seg.tree[0].value;
  if(all_hashis.find(currr) != all_hashis.end()) {
    cout << "Yes" << '\n';
  } else {
    cout << "No" << '\n';
  }

  while(q--) {
    int l, r;
    char ch;
    cin >> l >> r >> ch;
    l--, r--;

    if(ch == 'J') {
      seg.upd(l, r + 1, 1);
    } else if(ch == 'O') {
      seg.upd(l, r + 1, 2);
    } else {
      seg.upd(l, r + 1, 3);
    }

    auto currr = seg.tree[0].value;
    if(all_hashis.find(currr) != all_hashis.end()) {
      cout << "Yes" << '\n';
    } else {
      cout << "No" << '\n';
    }
  }
}

int main() {
  setIO();

  auto solve = [&](int test_case)-> void {
    for(int i = 1; i <= test_case; i++) {
      solve_();
    }
  };
 
  int test_cases = 1;
  // cin >> test_cases;
  solve(test_cases);
 
  return 0;
} 

Compilation message

Main.cpp: In member function 'void segTree::propagate(int, int, int)':
Main.cpp:124:9: warning: unused variable 'mid' [-Wunused-variable]
  124 |     int mid = (lx + rx) / 2;
      |         ^~~
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:76:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:77:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:79:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:80:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 72 ms 908 KB Output is correct
2 Correct 83 ms 876 KB Output is correct
3 Correct 76 ms 972 KB Output is correct
4 Correct 68 ms 972 KB Output is correct
5 Correct 63 ms 908 KB Output is correct
6 Correct 61 ms 928 KB Output is correct
7 Correct 66 ms 972 KB Output is correct
8 Correct 66 ms 976 KB Output is correct
9 Correct 65 ms 960 KB Output is correct
10 Correct 68 ms 908 KB Output is correct
11 Correct 68 ms 992 KB Output is correct
12 Correct 81 ms 872 KB Output is correct
13 Correct 66 ms 972 KB Output is correct
14 Correct 68 ms 928 KB Output is correct
15 Correct 69 ms 876 KB Output is correct
16 Correct 67 ms 904 KB Output is correct
17 Correct 75 ms 1072 KB Output is correct
18 Correct 74 ms 1008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 908 KB Output is correct
2 Correct 83 ms 876 KB Output is correct
3 Correct 76 ms 972 KB Output is correct
4 Correct 68 ms 972 KB Output is correct
5 Correct 63 ms 908 KB Output is correct
6 Correct 61 ms 928 KB Output is correct
7 Correct 66 ms 972 KB Output is correct
8 Correct 66 ms 976 KB Output is correct
9 Correct 65 ms 960 KB Output is correct
10 Correct 68 ms 908 KB Output is correct
11 Correct 68 ms 992 KB Output is correct
12 Correct 81 ms 872 KB Output is correct
13 Correct 66 ms 972 KB Output is correct
14 Correct 68 ms 928 KB Output is correct
15 Correct 69 ms 876 KB Output is correct
16 Correct 67 ms 904 KB Output is correct
17 Correct 75 ms 1072 KB Output is correct
18 Correct 74 ms 1008 KB Output is correct
19 Correct 268 ms 31160 KB Output is correct
20 Correct 268 ms 31356 KB Output is correct
21 Correct 185 ms 30372 KB Output is correct
22 Correct 170 ms 29004 KB Output is correct
23 Correct 109 ms 2836 KB Output is correct
24 Correct 108 ms 2740 KB Output is correct
25 Correct 180 ms 31252 KB Output is correct
26 Correct 190 ms 31184 KB Output is correct
27 Correct 210 ms 31212 KB Output is correct
28 Correct 210 ms 31176 KB Output is correct
29 Correct 200 ms 30836 KB Output is correct
30 Correct 119 ms 2832 KB Output is correct
31 Correct 206 ms 31256 KB Output is correct
32 Correct 194 ms 29940 KB Output is correct
33 Correct 111 ms 2716 KB Output is correct
34 Correct 227 ms 31492 KB Output is correct
35 Correct 191 ms 27664 KB Output is correct
36 Correct 110 ms 2740 KB Output is correct
37 Correct 108 ms 2812 KB Output is correct
38 Correct 231 ms 31220 KB Output is correct
39 Correct 163 ms 31136 KB Output is correct
40 Correct 168 ms 26460 KB Output is correct
41 Correct 287 ms 31392 KB Output is correct
42 Correct 115 ms 31352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 908 KB Output is correct
2 Correct 83 ms 876 KB Output is correct
3 Correct 76 ms 972 KB Output is correct
4 Correct 68 ms 972 KB Output is correct
5 Correct 63 ms 908 KB Output is correct
6 Correct 61 ms 928 KB Output is correct
7 Correct 66 ms 972 KB Output is correct
8 Correct 66 ms 976 KB Output is correct
9 Correct 65 ms 960 KB Output is correct
10 Correct 68 ms 908 KB Output is correct
11 Correct 68 ms 992 KB Output is correct
12 Correct 81 ms 872 KB Output is correct
13 Correct 66 ms 972 KB Output is correct
14 Correct 68 ms 928 KB Output is correct
15 Correct 69 ms 876 KB Output is correct
16 Correct 67 ms 904 KB Output is correct
17 Correct 75 ms 1072 KB Output is correct
18 Correct 74 ms 1008 KB Output is correct
19 Correct 75 ms 856 KB Output is correct
20 Correct 84 ms 1004 KB Output is correct
21 Correct 71 ms 900 KB Output is correct
22 Correct 82 ms 904 KB Output is correct
23 Correct 70 ms 916 KB Output is correct
24 Correct 68 ms 908 KB Output is correct
25 Correct 70 ms 980 KB Output is correct
26 Correct 66 ms 936 KB Output is correct
27 Correct 70 ms 880 KB Output is correct
28 Correct 64 ms 848 KB Output is correct
29 Correct 70 ms 984 KB Output is correct
30 Correct 64 ms 912 KB Output is correct
31 Correct 77 ms 972 KB Output is correct
32 Correct 70 ms 908 KB Output is correct
33 Correct 73 ms 948 KB Output is correct
34 Correct 66 ms 840 KB Output is correct
35 Correct 71 ms 872 KB Output is correct
36 Correct 79 ms 984 KB Output is correct
37 Correct 71 ms 988 KB Output is correct
38 Correct 76 ms 904 KB Output is correct
39 Correct 80 ms 884 KB Output is correct
40 Correct 72 ms 972 KB Output is correct
41 Correct 71 ms 876 KB Output is correct
42 Correct 72 ms 884 KB Output is correct
43 Correct 71 ms 936 KB Output is correct
44 Correct 71 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 908 KB Output is correct
2 Correct 83 ms 876 KB Output is correct
3 Correct 76 ms 972 KB Output is correct
4 Correct 68 ms 972 KB Output is correct
5 Correct 63 ms 908 KB Output is correct
6 Correct 61 ms 928 KB Output is correct
7 Correct 66 ms 972 KB Output is correct
8 Correct 66 ms 976 KB Output is correct
9 Correct 65 ms 960 KB Output is correct
10 Correct 68 ms 908 KB Output is correct
11 Correct 68 ms 992 KB Output is correct
12 Correct 81 ms 872 KB Output is correct
13 Correct 66 ms 972 KB Output is correct
14 Correct 68 ms 928 KB Output is correct
15 Correct 69 ms 876 KB Output is correct
16 Correct 67 ms 904 KB Output is correct
17 Correct 75 ms 1072 KB Output is correct
18 Correct 74 ms 1008 KB Output is correct
19 Correct 268 ms 31160 KB Output is correct
20 Correct 268 ms 31356 KB Output is correct
21 Correct 185 ms 30372 KB Output is correct
22 Correct 170 ms 29004 KB Output is correct
23 Correct 109 ms 2836 KB Output is correct
24 Correct 108 ms 2740 KB Output is correct
25 Correct 180 ms 31252 KB Output is correct
26 Correct 190 ms 31184 KB Output is correct
27 Correct 210 ms 31212 KB Output is correct
28 Correct 210 ms 31176 KB Output is correct
29 Correct 200 ms 30836 KB Output is correct
30 Correct 119 ms 2832 KB Output is correct
31 Correct 206 ms 31256 KB Output is correct
32 Correct 194 ms 29940 KB Output is correct
33 Correct 111 ms 2716 KB Output is correct
34 Correct 227 ms 31492 KB Output is correct
35 Correct 191 ms 27664 KB Output is correct
36 Correct 110 ms 2740 KB Output is correct
37 Correct 108 ms 2812 KB Output is correct
38 Correct 231 ms 31220 KB Output is correct
39 Correct 163 ms 31136 KB Output is correct
40 Correct 168 ms 26460 KB Output is correct
41 Correct 287 ms 31392 KB Output is correct
42 Correct 115 ms 31352 KB Output is correct
43 Correct 75 ms 856 KB Output is correct
44 Correct 84 ms 1004 KB Output is correct
45 Correct 71 ms 900 KB Output is correct
46 Correct 82 ms 904 KB Output is correct
47 Correct 70 ms 916 KB Output is correct
48 Correct 68 ms 908 KB Output is correct
49 Correct 70 ms 980 KB Output is correct
50 Correct 66 ms 936 KB Output is correct
51 Correct 70 ms 880 KB Output is correct
52 Correct 64 ms 848 KB Output is correct
53 Correct 70 ms 984 KB Output is correct
54 Correct 64 ms 912 KB Output is correct
55 Correct 77 ms 972 KB Output is correct
56 Correct 70 ms 908 KB Output is correct
57 Correct 73 ms 948 KB Output is correct
58 Correct 66 ms 840 KB Output is correct
59 Correct 71 ms 872 KB Output is correct
60 Correct 79 ms 984 KB Output is correct
61 Correct 71 ms 988 KB Output is correct
62 Correct 76 ms 904 KB Output is correct
63 Correct 80 ms 884 KB Output is correct
64 Correct 72 ms 972 KB Output is correct
65 Correct 71 ms 876 KB Output is correct
66 Correct 72 ms 884 KB Output is correct
67 Correct 71 ms 936 KB Output is correct
68 Correct 71 ms 876 KB Output is correct
69 Correct 535 ms 30392 KB Output is correct
70 Correct 536 ms 34896 KB Output is correct
71 Correct 122 ms 5168 KB Output is correct
72 Correct 149 ms 5100 KB Output is correct
73 Correct 123 ms 5100 KB Output is correct
74 Correct 319 ms 32636 KB Output is correct
75 Correct 120 ms 5056 KB Output is correct
76 Correct 431 ms 35336 KB Output is correct
77 Correct 339 ms 32752 KB Output is correct
78 Correct 128 ms 5104 KB Output is correct
79 Correct 121 ms 5084 KB Output is correct
80 Correct 372 ms 31560 KB Output is correct
81 Correct 131 ms 5156 KB Output is correct
82 Correct 489 ms 36620 KB Output is correct
83 Correct 445 ms 35544 KB Output is correct
84 Correct 135 ms 5148 KB Output is correct
85 Correct 128 ms 5136 KB Output is correct
86 Correct 432 ms 32408 KB Output is correct
87 Correct 463 ms 36624 KB Output is correct
88 Correct 138 ms 5180 KB Output is correct
89 Correct 417 ms 34392 KB Output is correct
90 Correct 477 ms 36656 KB Output is correct
91 Correct 135 ms 5148 KB Output is correct
92 Correct 389 ms 32244 KB Output is correct
93 Correct 130 ms 5232 KB Output is correct
94 Correct 132 ms 5168 KB Output is correct
95 Correct 128 ms 5248 KB Output is correct
96 Correct 233 ms 34744 KB Output is correct
97 Correct 174 ms 36604 KB Output is correct
98 Correct 386 ms 31040 KB Output is correct
99 Correct 641 ms 36764 KB Output is correct
100 Correct 395 ms 35968 KB Output is correct