Manioc

subpalindrome

Jun 22nd, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MAX 100007
  3.  
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7.  
  8. ll h[2][4*MAX], power[MAX], inv[MAX];
  9. ll base[2] = {317, 307};
  10. ll mod[2] = {104000717, 104000711};
  11. int n;
  12. void init(){
  13.     power[0] = base[0];
  14.     inv[0] = 46258994;
  15.     //cout << inv[0] << endl;
  16.     for(int i = 1; i < MAX; i++){
  17.         power[i] = (power[i-1]*base[0])%mod[0];
  18.         inv[i] = (inv[i-1]*inv[0])%mod[0];
  19.     }
  20. }
  21.  
  22. void update(int tipo, int idx, char now, char old = '$'){
  23.     //cout << now << " " << old << endl;
  24.     int pos = idx;
  25.     if(old == '$') for(; idx <= n; idx += idx&(-idx)) h[tipo][idx] = (h[tipo][idx] + (now-'a'+1)*power[pos])%mod[0];
  26.     else{
  27.         for(; idx <= n; idx += idx&(-idx)) {
  28.             h[tipo][idx] = (h[tipo][idx] - (old-'a'+1)*power[pos])%mod[0];
  29.             while(h[tipo][idx] < 0) h[tipo][idx] += mod[0];
  30.             h[tipo][idx] = (h[tipo][idx] + (now-'a'+1)*power[pos])%mod[0];
  31.             while(h[tipo][idx] < 0) h[tipo][idx] += mod[0];
  32.         }
  33.     }
  34. }
  35.  
  36. ll query(int tipo, int idx){
  37.     ll hash = 0;
  38.     for(; idx > 0; idx -= idx&(-idx)) hash = (hash + h[tipo][idx])%mod[0];
  39.     return hash;
  40. }
  41.  
  42. ll query(int tipo, int l, int r){
  43.     return (((query(tipo, r)-query(tipo, l-1)+ mod[0])%mod[0])*inv[l])%mod[0];
  44. }
  45. int main(){
  46.     //ios_base::sync_with_stdio(false);
  47.     //cin.tie(NULL);
  48.  
  49.     char txt[MAX]; scanf("%s", txt);
  50.     n = strlen(txt);
  51.     init();
  52.     //cout << "ola\n";
  53.     for(int i = 1; i <= n; i++) {
  54.         update(0, i, txt[i-1]);
  55.         update(1, i, txt[n-i]);
  56.     }
  57.     //cout << "oi\n";
  58.     int k; scanf("%d", &k);
  59.     while(k--){
  60.         char tipo[15]; scanf("%s", tipo);
  61.  
  62.         if(tipo[0] == 'p'){
  63.             int l, r; cin >> l >> r;
  64.             ll x = query(0, l, r), y = query(1, n-r+1, n-l+1);
  65.             printf("%s\n", x == y? "Yes": "No");
  66.         }else{
  67.             int idx;
  68.             char letra; scanf("%d %c", &idx, &letra);
  69.             update(0, idx, letra, txt[idx-1]);
  70.             update(1, n-idx+1, letra, txt[idx-1]);
  71.             txt[idx-1] = letra;
  72.         }
  73.     }
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment