Submission #1311591

#TimeUsernameProblemLanguageResultExecution timeMemory
1311591Zbyszek99Crossing (JOI21_crossing)C++20
100 / 100
541 ms54816 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

ll p[2];
ll P[2][200001];
ll P_pref[2][200001];
map<char,int> mp = {{'J',0},{'O',1},{'I',2}};
vector<vi> possible;
unordered_set<ll> hashes;
int n;

const int tree_siz = 1024*1024-1;

struct segtree_str
{
    ll sum[tree_siz+1];
    ll oper[tree_siz+1];
    int d;
    segtree_str()
    {
        rep2(i,1,tree_siz) 
        {
            sum[i] = 0;
            oper[i] = -1;
        }
    }
    void set_seg2(int v, int l, int r, int x)
    {
        oper[v] = x;
        sum[v] = ((P_pref[d][r+1]-P_pref[d][l]+MOD)*x)%MOD;
    }
    void spych(int v, int l, int r)
    {
        if(oper[v] == -1) return;
        set_seg2(v*2,l,(l+r)/2,oper[v]);
        set_seg2(v*2+1,(l+r)/2+1,r,oper[v]);
        oper[v] = -1;
    }
    void set_seg(int akt, int p1, int p2, int s1, int s2, int x)
    {
        if(p2 < s1 || p1 > s2) return;
        if(p1 >= s1 && p2 <= s2)
        {
            set_seg2(akt,p1,p2,x);
            return;
        }
        spych(akt,p1,p2);
        set_seg(akt*2,p1,(p1+p2)/2,s1,s2,x);
        set_seg(akt*2+1,(p1+p2)/2+1,p2,s1,s2,x);
        sum[akt] = (sum[akt*2]+sum[akt*2+1])%MOD;
    }
    ll get_hash()
    {
        return sum[1];
    }
};

segtree_str segtree[2];

ll hash_vect(vi x)
{
    ll h = 0;
    ll h2 = 0;
    rep(i,n) h = (h+x[i]*P[0][i+1])%MOD;
    rep(i,n) h2 = (h2+x[i]*P[1][i+1])%MOD;
    return h+MOD*h2;
}

vi cross(vi& a, vi& b)
{
    vi c(n);
    rep(i,n) c[i] = (6-a[i]-b[i])%3;
    return c;
}

void add_vect(vi x)
{
    ll h = hash_vect(x);
    if(hashes.find(h) != hashes.end()) return;
    hashes.insert(h);
    possible.pb(x);
    rep(i,siz(possible)) add_vect(cross(x,possible[i]));
}

void check()
{
    ll h = segtree[0].get_hash()+segtree[1].get_hash()*MOD;
    if(hashes.find(h) == hashes.end()) cout << "No\n";
    else cout << "Yes\n";
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random_start();
    cin >> n;
    p[0] = 2137;
    p[1] = 694202137;
    rep(d,2)
    {
        segtree[d].d = d;
        P[d][0] = 1;
        P_pref[d][0] = 1;
        rep2(i,1,n)
        {
            P[d][i] = (P[d][i-1]*p[d])%MOD;
            P_pref[d][i] = (P_pref[d][i-1]+P[d][i])%MOD;
        }
    }
    rep(i,3)
    {
        string s;
        cin >> s;
        vi x(n);
        rep(j,n) x[j] = mp[s[j]];
        add_vect(x);
    }
    int q;
    cin >> q;
    string s;
    cin >> s;
    rep(i,n) rep(d,2) segtree[d].set_seg(1,0,tree_siz/2,i,i,mp[s[i]]);
    check();
    rep(i,q)
    {
        int l,r;
        char z;
        cin >> l >> r >> z;
        l--;
        r--;
        rep(d,2) segtree[d].set_seg(1,0,tree_siz/2,l,r,mp[z]);
        check();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...