87102024-01-25 21:45:29szilTúlcsorduláscpp17Elfogadva 100/100435ms30492 KiB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int MAXN = 200'010;
const int HASH_CNT = 2;
const ll MOD = 1e9+7;

ll mult(ll a, ll b){return (a*b)%MOD;}
ll poww(ll a, ll b){
    if(b==0)return 1LL;
    if(b&1)return mult(poww(a, b-1), a);
    ll c = poww(a, b/2);
    return mult(c, c);
}

ll eredeti_primes[1000] = {3, 5, 7, 11};
ll primes[MAXN][HASH_CNT], inv_primes[MAXN][HASH_CNT];


struct Hash {
    ll val[HASH_CNT];

    bool operator==(const Hash &x) const {
        for (int i = 0; i < HASH_CNT; i++) if (val[i] != x.val[i]) return false;
        return true;
    }
};

bool a[MAXN], b[MAXN];
Hash ha[MAXN], hb[MAXN];

Hash H(Hash *arr, int l, int r) {
    Hash res;
    for (int i = 0; i < HASH_CNT; i++) {
        res.val[i] = (arr[r].val[i] - arr[l-1].val[i] + MOD) % MOD;
        res.val[i] = mult(res.val[i], inv_primes[l-1][i]); // TODO check
    }
    return res;
}

int main(){
    cin.tie(0); cin.sync_with_stdio(0);
    for(int i = 0; i < HASH_CNT; i++){
        primes[0][i] = 1;
        inv_primes[0][i] = 1;
        for(int j = 1; j < MAXN; j++){
            primes[j][i] = (primes[j-1][i] * eredeti_primes[i]) % MOD;
            inv_primes[j][i] = poww(primes[j][i], MOD-2);
        }
    }
    int n; cin >> n;
    for (int i = 1; i <= n; i++) {
        char c; cin >> c;
        a[i] = 1-(c-'0');
    }

    for (int i = 1; i <= n; i++) {
        char c; cin >> c;
        b[i] = c-'0';
    }
    
    for (int i = 0; i < HASH_CNT; i++) {
        for (int j = 1; j <= n; j++) {
            ha[j].val[i] = (ha[j-1].val[i] + a[j] * primes[j][i]) % MOD;
            hb[j].val[i] = (hb[j-1].val[i] + b[j] * primes[j][i]) % MOD;
        }
    }

    int q; cin >> q;
    while (q--) {
        int x, y, l; cin >> x >> y >> l;
        x++; y++;
        int lo = 0, hi = l; // TODO check
        while (lo < hi) {
            int mid = (lo + hi) / 2;
            if (H(ha, x, x+mid) == H(hb, y, y+mid)) {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }
        if (lo == l) cout << "1 ";
        else cout << 1-b[y+lo] << " ";
    }
    cout << "\n";
    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva181ms14448 KiB
2Elfogadva180ms14772 KiB
subtask240/40
3Elfogadva181ms15096 KiB
4Elfogadva180ms15196 KiB
5Elfogadva181ms15412 KiB
6Elfogadva180ms15884 KiB
7Elfogadva180ms15900 KiB
8Elfogadva182ms16204 KiB
9Elfogadva182ms16164 KiB
subtask330/30
10Elfogadva180ms15776 KiB
11Elfogadva200ms16576 KiB
12Elfogadva266ms21136 KiB
13Elfogadva266ms23824 KiB
14Elfogadva335ms29288 KiB
15Elfogadva402ms28536 KiB
16Elfogadva400ms28448 KiB
17Elfogadva395ms29328 KiB
subtask430/30
18Elfogadva188ms16888 KiB
19Elfogadva225ms29556 KiB
20Elfogadva261ms19908 KiB
21Elfogadva307ms26592 KiB
22Elfogadva407ms30032 KiB
23Elfogadva409ms30172 KiB
24Elfogadva400ms29688 KiB
25Elfogadva391ms29232 KiB
26Elfogadva405ms30316 KiB
27Elfogadva435ms30308 KiB
28Elfogadva412ms30492 KiB