Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define mp make_pair
- #define boost ios_base::sync_with_stdio(false)
- #define pb push_back
- #define e1 first
- #define e2 second
- #define tysiac 1010
- #define milion 1000100
- #define OUT(x) { cout << x; return 0; }
- typedef long long int ll;
- typedef unsigned int ui;
- typedef unsigned long long ull;
- typedef long double ld;
- typedef pair <int, int> PII;
- typedef pair <ll, ll> PLL;
- typedef pair <ld, ld> PDD;
- typedef pair <PII, PII> PP;
- typedef pair <ll, int> PLI;
- const ll P = 31;
- const int MOD = 1e9+7;
- const int inf = 1e9+9;
- const ll mod = 1e9+696969;
- #define maxn 100100
- ll t[maxn][2], dr[maxn][2];
- ll pot[maxn], INV[maxn];
- int zap, n;
- string s;
- ll potmod(ll a, ll b)
- {
- if (!b) return 1;
- if (b & 1) return (a%mod*potmod(a*a%mod, b>>1))%mod;
- return potmod(a*a%mod, b>>1);
- }
- void add(int p, ll ile, int par)
- {
- for (; p<maxn; p += p & (-p)) dr[p][par] += ile;
- }
- ll get(int p, int par)
- {
- ll ret = 0;
- for (; p > 0; p -= p & (-p)) ret += dr[p][par];
- return ret;
- }
- ll getH(int a, int b, int par)
- {
- ll H = get(b, par) - get(a-1, par) + mod*mod;
- H %= mod;
- H *= INV[a];
- return (H % mod);
- }
- ll tmphash[2];
- void update(int a, int ile, int par)
- {
- add(a, - (t[a][par] * pot[a] % mod), par);
- t[a][par] = ile;
- add(a, t[a][par] * pot[a] % mod, par);
- }
- bool thesame(int a, int b)
- {
- ll H1 = getH(a, b, 0);
- ll H2 = getH(n - b + 1, n - a + 1, 1);
- return (H1 == H2);
- }
- int query(int a, int par)
- {
- if (a == n && par == 3) return -1;
- int lewy = a, prawy = ((par == 3)?a+1:a);
- int x = 0, y = min(lewy - 1, n - prawy);
- while (x < y)
- {
- int k = ((x + y) >> 1) + 1;
- if (thesame(lewy - k, prawy + k)) x = k;
- else y = --k;
- }
- int k = x;
- if (par == 3)
- {
- if (k < 1 && t[lewy][0] != t[prawy][0]) return -1;
- return 2*k+2;
- }
- return 2*k+1;
- }
- int main()
- {
- boost;
- cin >> s >> zap;
- n = s.length();
- pot[0] = 1;
- for (int i=1; i<=(n+2); ++i) pot[i] = (pot[i-1] * P) % mod;
- for (int i=0; i<=(n+2); ++i) INV[i] = potmod(pot[i], mod - 2);
- for (int i=0; i<n; ++i)
- {
- int akt = i + 1;
- t[akt][0] = s[i] - 'a' + 1;
- t[n - akt + 1][1] = s[i] - 'a' + 1;
- }
- for (int j=0; j<2; ++j)
- for (int i=1; i<=n; ++i) add(i, (pot[i] * t[i][j])%mod, j);
- for (int z=1; z<=zap; ++z)
- {
- int q, poz;
- char znak;
- cin >> q >> poz;
- if (q == 1)
- {
- cin >> znak;
- update(poz, znak - 'a' + 1, 0);
- update(n - poz + 1, znak - 'a' + 1, 1);
- }
- else cout << query(poz, q) << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement