Submission #771157

# Submission time Handle Problem Language Result Execution time Memory
771157 2023-07-02T13:53:40 Z Sam_a17 Crossing (JOI21_crossing) C++17
0 / 100
77 ms 1100 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;
    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);
    
    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;
}



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';
      }
    }
  }

  if(s[0] == s[1] && s[1] == s[2]) {
    for(int i = n - 1; i >= 0; i--) {
      h1[i] = ((h1[i + 1] * 53) % mod + s[0][i] - 'a' + 1) % mod; 
      h2[i] = ((h2[i + 1] * 31) % mod + s[0][i] - 'a' + 1) % mod; 
    }

    pair<long long, long long> need = {h1[0], h2[0]};
    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]));
      }
    }

    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);

    if(seg.tree[0].value == need) {
      cout << "Yes" << '\n';
    } else {
      cout << "No" << '\n';
    }

    // dbg(need)
    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);
      }

      // dbg(seg.tree[0].value)
      if(seg.tree[0].value == need) {
        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:125:9: warning: unused variable 'mid' [-Wunused-variable]
  125 |     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 68 ms 964 KB Output is correct
2 Correct 76 ms 920 KB Output is correct
3 Correct 77 ms 912 KB Output is correct
4 Incorrect 72 ms 1100 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 68 ms 964 KB Output is correct
2 Correct 76 ms 920 KB Output is correct
3 Correct 77 ms 912 KB Output is correct
4 Incorrect 72 ms 1100 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 68 ms 964 KB Output is correct
2 Correct 76 ms 920 KB Output is correct
3 Correct 77 ms 912 KB Output is correct
4 Incorrect 72 ms 1100 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 68 ms 964 KB Output is correct
2 Correct 76 ms 920 KB Output is correct
3 Correct 77 ms 912 KB Output is correct
4 Incorrect 72 ms 1100 KB Output isn't correct
5 Halted 0 ms 0 KB -