Problem: Almost periodic string

Given the string S. Answer Q queries for this string S. In the i-th query, determine if S [l_i, \ r_i] can be a string of period t_i when you can change from S [l_i, \ r_i] to one character. S [l, \ r] represents the substring of the string S from the lth to the rth.

If the character string W is a character string with period t, then for i \ = \ 1, \ 2, \ ..., \ | W | − t, W_ {i} = W_ {i + t}. I will do it.

Constraints

* 1 ≤ | S | ≤ 10 ^ 5
* 1 ≤ Q ≤ 10 ^ 5
* 1 ≤ l_i ≤ r_i ≤ | S |
* 1 ≤ t_i ≤ r_i − l_i + 1
* S consists of only lowercase letters



Input Format

Input is given from standard input in the following format.


S
Q
l_1 r_1 t_1
...
l_Q r_Q t_Q


Output Format

Output over Q lines. On line i, print the answer to the i-th query with `Yes` or` No`.

Sample Input 1


abcabcaxcabc
Four
1 9 3
8 12 3
1 4 2
2 3 2


Sample Output 1


Yes
Yes
No
Yes


Sample Input 2


isuruu
Four
3 6 1
3 6 2
3 6 3
2 4 1


Sample Output 2


Yes
Yes
Yes
No






Example

Input

abcabcaxcabc
4
1 9 3
8 12 3
1 4 2
2 3 2


Output

Yes
Yes
No
Yes

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;
const int M = 1000000007;

class suffix_array {
    // compare (rank[i], rank[i + k]) and (rank[j], rank[j + k])
    static bool compare_sa(int n, const vector<int>& rank, int i, int j, int k) {
        if (rank[i] != rank[j]) return rank[i] < rank[j];
        int ri = i + k <= n ? rank[i + k] : -1;
        int rj = j + k <= n ? rank[j + k] : -1;
        return ri < rj;
    }
public:
    static vector<int> construct_sa(const string& s) {
        int n = s.length();
        vector<int> sa(n + 1), rank(n + 1);
        for (int i = 0; i <= n; ++i) {
            sa[i] = i;
            rank[i] = i < n ? s[i] : -1;
        }
        for (int k = 1; k <= n; k <<= 1) {
            sort(sa.begin(), sa.end(), [&n, &k, &rank](const int& a, const int& b){ return compare_sa(n, rank, a, b, k); });

            vector<int> tmp(n + 1);
            for (int i = 1; i <= n; ++i)
                tmp[sa[i]] = tmp[sa[i - 1]] + compare_sa(n, rank, sa[i - 1], sa[i], k);
            for (int i = 0; i <= n; ++i)
                rank[i] = tmp[i];
        }
        return sa;
    }
    static vector<int> construct_lcp(const string& s, const vector<int>& sa) {
        int n = s.length();
        vector<int> rank(n + 1), lcp(n + 1);
        for (int i = 0; i <= n; ++i) rank[sa[i]] = i;

        int h = 0;
        for (int i = 0; i < n; ++i) {
            if (h > 0) --h;
            for (int j = sa[rank[i] - 1]; j + h < n && i + h < n; ++h)
                if (s[j + h] != s[i + h]) break;

            lcp[rank[i] - 1] = h;
        }
        return lcp;
    }
};

class segtree {
private:
    int n, s, t;
    vector<int> tr;
    const int ex = M;

    int q(int k, int l, int r) {
        return r <= s || t <= l ? ex : s <= l && r <= t ? tr[k]
                : min(q(k << 1 | 1, l, (l + r) >> 1), q((k + 1) << 1, (l + r) >> 1, r));
    }

public:
    segtree(int m) {
        n = 1;
        while (n < m) n <<= 1;
        tr.clear();
        tr.resize((n << 1) - 1, ex);
    }
    void update(int j, const int& x) {
        int i = j + n - 1;
        tr[i] = x;
        while (i > 0) { i = (i - 1) >> 1; tr[i] = min(tr[i << 1 | 1], tr[(i + 1) << 1]); }
    }
    // [s, t)
    int run(int _s, int _t) { s = _s; t = _t; return q(0, 0, n); }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    string s;
    int q;
    cin >> s >> q;
    int n = s.length();
    vector<int> sa = suffix_array::construct_sa(s);
    vector<int> rank(n + 1);
    for (int i = 0; i <= n; ++i) rank[sa[i]] = i;
    vector<int> lcp = suffix_array::construct_lcp(s, sa);
    segtree sg(n + 1);
    for (int i = 0; i <= n; ++i) {
        sg.update(i, lcp[i]);
    }
    auto run = [&](const int& a, const int& b){ return sg.run(min(a, b), max(a, b)); };
    for (int _ = 0; _ < q; ++_) {
        int l, r, t;
        cin >> l >> r >> t;
        --l;
        int l1 = run(rank[l], rank[l + t]);
        if (l + t + l1 >= r) {
            cout << "Yes\n";
        }
        else {
            bool b1 = (l1 < t || s[l + l1 - t] == s[l + l1 + t]) && (l + l1 + t * 2 >= r || s[l + l1 + t] == s[l + l1 + t * 2]);
            bool b2 = l + l1 + t * 2 >= r || s[l + l1] == s[l + l1 + t * 2];
            int l2 = run(rank[l + l1 + 1], rank[l + l1 + 1 + t]);
            int l3 = l + l1 + 1 + t * 2 >= r ? 0 : run(rank[l + l1 + t + 1], rank[l + l1 + t * 2 + 1]);
            // cout << l2 << ' ' << l3 << '\n';
            if ((l2 == t - 1 || l + l1 + 1 + t + l2 >= r) && l + l1 + 1 + t * 2 + l3 >= r && (b1 || b2)) {
                cout << "Yes\n";
            }
            else
                cout << "No\n";
        }
    }


    return 0;
}