Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define MAX 100007
- using namespace std;
- typedef long long ll;
- ll h[2][4*MAX], power[MAX], inv[MAX];
- ll base[2] = {317, 307};
- ll mod[2] = {104000717, 104000711};
- int n;
- void init(){
- power[0] = base[0];
- inv[0] = 46258994;
- //cout << inv[0] << endl;
- for(int i = 1; i < MAX; i++){
- power[i] = (power[i-1]*base[0])%mod[0];
- inv[i] = (inv[i-1]*inv[0])%mod[0];
- }
- }
- void update(int tipo, int idx, char now, char old = '$'){
- //cout << now << " " << old << endl;
- int pos = idx;
- if(old == '$') for(; idx <= n; idx += idx&(-idx)) h[tipo][idx] = (h[tipo][idx] + (now-'a'+1)*power[pos])%mod[0];
- else{
- for(; idx <= n; idx += idx&(-idx)) {
- h[tipo][idx] = (h[tipo][idx] - (old-'a'+1)*power[pos])%mod[0];
- while(h[tipo][idx] < 0) h[tipo][idx] += mod[0];
- h[tipo][idx] = (h[tipo][idx] + (now-'a'+1)*power[pos])%mod[0];
- while(h[tipo][idx] < 0) h[tipo][idx] += mod[0];
- }
- }
- }
- ll query(int tipo, int idx){
- ll hash = 0;
- for(; idx > 0; idx -= idx&(-idx)) hash = (hash + h[tipo][idx])%mod[0];
- return hash;
- }
- ll query(int tipo, int l, int r){
- return (((query(tipo, r)-query(tipo, l-1)+ mod[0])%mod[0])*inv[l])%mod[0];
- }
- int main(){
- //ios_base::sync_with_stdio(false);
- //cin.tie(NULL);
- char txt[MAX]; scanf("%s", txt);
- n = strlen(txt);
- init();
- //cout << "ola\n";
- for(int i = 1; i <= n; i++) {
- update(0, i, txt[i-1]);
- update(1, i, txt[n-i]);
- }
- //cout << "oi\n";
- int k; scanf("%d", &k);
- while(k--){
- char tipo[15]; scanf("%s", tipo);
- if(tipo[0] == 'p'){
- int l, r; cin >> l >> r;
- ll x = query(0, l, r), y = query(1, n-r+1, n-l+1);
- printf("%s\n", x == y? "Yes": "No");
- }else{
- int idx;
- char letra; scanf("%d %c", &idx, &letra);
- update(0, idx, letra, txt[idx-1]);
- update(1, n-idx+1, letra, txt[idx-1]);
- txt[idx-1] = letra;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment