87042024-01-25 20:31:52mraronTúlcsorduláscpp17Accepted 100/100338ms77192 KiB
// @check-accepted: examples quadratic X=Y no-limits
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<random>
#include<chrono>
#include<bitset>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair
#define ins insert

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif

using ll = long long;
using ull = unsigned long long ;
using ld = long double ;
using str = string;
//using ordered_set=tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update>;

const double PI=acos(-1);
const ll INF = 1LL<<62;
const ll MINF = -(1LL<<62);

template<typename T> T getint() {
    T val=0;
    char c;
    
    bool neg=false;
    while((c=gc()) && !(c>='0' && c<='9')) {
        neg|=c=='-';
    }

    do {
        val=(val*10)+c-'0';
    } while((c=gc()) && (c>='0' && c<='9'));

    return val*(neg?-1:1);
}

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int>(0, n-1)(rng)


struct suffix_array {
	vector<int> sarr, inv, lcp;
	string t;
	suffix_array(string t) : t(t) {
		sarr.assign(sz(t)+1, 0);
		inv.assign(sz(t)+1, 0);

		sarr[0]=sz(t);
		iota(sarr.begin()+1, sarr.end(), 0);

		inv[sz(t)]=0;

		sort(sarr.begin()+1, sarr.end(), [&](int i, int j) -> bool {
			return t[i]<t[j];
		});

		inv[sarr[1]]=1;
		for(int i=2;i<=sz(t);++i) {
			int a=sarr[i-1], b=sarr[i];
			if(t[a]==t[b]) {
				inv[b]=inv[a];
			}else {
				inv[b]=i;
			}
		}

		for(int len=1;len<=sz(t);len*=2) {
			vector<int> nxt(sz(t)+1);
			iota(nxt.begin(), nxt.end(), 0);

			vector<int> sarr_orig(sarr);
			vector<int> inv_orig(inv);
			
			for(int i:sarr_orig) {
				if(i-len>=0) {
					sarr[nxt[inv[i-len]]++]=i-len;
				}
			}
			
			inv[sarr[1]]=1;
			for(int i=2;i<=sz(t);++i) {
				int a=sarr[i-1], b=sarr[i];
				if(inv_orig[a]==inv_orig[b] && (a+len>sz(t) || b+len>sz(t) || inv_orig[a+len]==inv_orig[b+len])) {
					inv[b]=inv[a];
				}else {
					inv[b]=i;
				}
			}
		}
	}

	void calc_lcp() {
		lcp.assign(sz(t)+1, 0);
		for(int i=0;i<sz(t);++i) {
			int pos=inv[i];
			if(pos==sz(t) || pos==0) {
				lcp[pos]=0;
			}else {
				int nxt=sarr[pos+1];
				while(max(nxt, i)+lcp[pos]<sz(t) && t[i+lcp[pos]]==t[nxt+lcp[pos]]) lcp[pos]++;
				lcp[inv[i+1]]=max(0, lcp[pos]-1);
			}
		}
	}

	void print_dbg() {
		for(int i=1;i<=sz(t);++i) {
			cerr<<t.substr(sarr[i])<<" "<<lcp[i]<<"\n";
		}
	}
};


template<int POW=19>
struct sparse {
	array<vector<int>, POW> dp;
	sparse(int N, int* t) {
		for(int i=0;i<POW;++i) {
			dp[i].resize(N);
		}

		for(int i=0;i<N;++i) {
			dp[0][i]=t[i];
		}

		for(int j=1;j<POW;++j) {
			for(int i=0;i<N;++i) {
				dp[j][i]=min(dp[j-1][i], dp[j-1][min(N-1, i+(1<<(j-1)))]);
			}
		}
	}

	int query(int x, int y) {
		int b=31-__builtin_clz(y-x+1);
		return min(dp[b][x], dp[b][y-(1<<b)+1]);
	}
};

int main() {
    IO;
    int n;
    cin>>n;
    string a,b;
    cin>>a>>b;
    for(int i=0;i<n;++i) {
        a[i]=a[i]=='0'?'1':'0';

    }
    
    string c="#"+a+"$"+b+"|";
    suffix_array sa(c);
    sa.calc_lcp();
    
    sparse sp(sz(sa.lcp), sa.lcp.data());
    
    int q;
    cin>>q;
    
    for(int i=0;i<q;++i) {
        int x,y,l;
        cin>>x>>y>>l;
        x++;
        y+=sz(a)+1+1;
        
        int L=sa.inv[x], R=sa.inv[y];
        if(L>R) swap(L,R);
        int ans=1e9;
        
        //~ for(int j=L;j<R;++j) {
            //~ ans=min(ans, sa.lcp[j]);
        //~ }
        ans = sp.query(L, R-1);
        
        cout<<(c[x+min(ans, l-1)]!='0' || c[y+min(ans, l-1)]=='0')<<" ";
    }
    cout<<"\n";
    
    
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2048 KiB
subtask240/40
3Accepted3ms2284 KiB
4Accepted3ms2488 KiB
5Accepted4ms2668 KiB
6Accepted4ms4340 KiB
7Accepted4ms3864 KiB
8Accepted6ms4564 KiB
9Accepted6ms4568 KiB
subtask330/30
10Accepted3ms3308 KiB
11Accepted20ms7024 KiB
12Accepted96ms32340 KiB
13Accepted180ms47040 KiB
14Accepted246ms75532 KiB
15Accepted323ms72096 KiB
16Accepted338ms69568 KiB
17Accepted272ms75528 KiB
subtask430/30
18Accepted13ms7444 KiB
19Accepted250ms75544 KiB
20Accepted83ms22088 KiB
21Accepted250ms57876 KiB
22Accepted282ms76176 KiB
23Accepted263ms76288 KiB
24Accepted321ms73140 KiB
25Accepted236ms70816 KiB
26Accepted259ms76988 KiB
27Accepted337ms77192 KiB
28Accepted263ms77076 KiB