87052024-01-25 20:32:01mraronTúlcsorduláscpp17Accepted 100/100219ms24256 KiB
// @check-accepted: examples quadratic X=Y no-limits
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#define int long long
using namespace std;
int N;
string A, B;
constexpr int mod = 1e9 + 7;
constexpr int base1 = 2;
constexpr int base2 = 7;
vector<int> p1;
vector<int> p2;
struct Hash {
    vector<int> h1;
    vector<int> h2;
    Hash(const string& s) {
        int n = s.size();
        h1.resize(n + 1);
        h2.resize(n + 1);
        for (int i = 0; i < n; ++i) {
            h1[i + 1] = (h1[i] * base1 + s[i] - '0') % mod;
            h2[i + 1] = (h2[i] * base2 + s[i] - '0') % mod;
        }
    }
    pair<int, int> get(int l, int r) {
        return {(h1[r + 1] - h1[l] * p1[r - l + 1] % mod + mod) % mod, (h2[r + 1] - h2[l] * p2[r - l + 1] % mod + mod) % mod};
    }
};
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> N;
    p1.resize(N + 1);
    p1[0] = 1;
    for (int i = 1; i <= N; ++i) {
        p1[i] = p1[i - 1] * base1 % mod;
    }
    p2.resize(N + 1);
    p2[0] = 1;
    for (int i = 1; i <= N; ++i) {
        p2[i] = p2[i - 1] * base2 % mod;
    }
    cin >> A;
    cin >> B;
    for (int i = 0; i < N; ++i) {
        B[i] = 1 - (B[i] - '0') + '0';
    }
    Hash ha(A), hb(B);

    int Q;
    cin >> Q;

    for (int i = 0; i < Q; ++i) {
        int X, Y, L;
        cin >> X >> Y >> L;

        int l = 0, r = L;
        while (l < r) {
            int m = (l + r) / 2;
            if (ha.get(X, X + m) == hb.get(Y, Y + m)) {
                l = m + 1;
            } else {
                r = m;
            }
        }
        if (l == L) {
            cout << 1 << " ";
        } else {
            cout << B[Y + l] << " ";
        }
    }
    cout << '\n';
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2020 KiB
subtask240/40
3Accepted3ms2104 KiB
4Accepted3ms2232 KiB
5Accepted4ms2472 KiB
6Accepted3ms3088 KiB
7Accepted3ms3048 KiB
8Accepted4ms3048 KiB
9Accepted4ms3296 KiB
subtask330/30
10Accepted3ms3080 KiB
11Accepted17ms4156 KiB
12Accepted79ms11464 KiB
13Accepted59ms15468 KiB
14Accepted149ms23284 KiB
15Accepted145ms22224 KiB
16Accepted143ms21480 KiB
17Accepted202ms23312 KiB
subtask430/30
18Accepted8ms4616 KiB
19Accepted37ms23500 KiB
20Accepted56ms8676 KiB
21Accepted86ms18412 KiB
22Accepted219ms23756 KiB
23Accepted216ms24040 KiB
24Accepted143ms22940 KiB
25Accepted211ms22112 KiB
26Accepted212ms24160 KiB
27Accepted145ms24244 KiB
28Accepted216ms24256 KiB