Advertisement
Guest User

Untitled

a guest
Aug 26th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define mp make_pair
  4. #define boost ios_base::sync_with_stdio(false)
  5. #define pb push_back
  6. #define e1 first
  7. #define e2 second
  8. #define tysiac 1010
  9. #define milion 1000100
  10. #define OUT(x) { cout << x; return 0; }
  11. typedef long long int ll;
  12. typedef unsigned int ui;
  13. typedef unsigned long long ull;
  14. typedef long double ld;
  15. typedef pair <int, int> PII;
  16. typedef pair <ll, ll> PLL;
  17. typedef pair <ld, ld> PDD;
  18. typedef pair <PII, PII> PP;
  19. typedef pair <ll, int> PLI;
  20. const ll P = 31;
  21. const int MOD = 1e9+7;
  22. const int inf = 1e9+9;
  23. const ll mod = 1e9+696969;
  24. #define maxn 100100
  25. ll t[maxn][2], dr[maxn][2];
  26. ll pot[maxn], INV[maxn];
  27. int zap, n;
  28. string s;
  29. ll potmod(ll a, ll b)
  30. {
  31.     if (!b) return 1;
  32.     if (b & 1) return (a%mod*potmod(a*a%mod, b>>1))%mod;
  33.     return potmod(a*a%mod, b>>1);
  34. }
  35. void add(int p, ll ile, int par)
  36. {
  37.     for (; p<maxn; p += p & (-p)) dr[p][par] += ile;
  38. }
  39. ll get(int p, int par)
  40. {
  41.     ll ret = 0;
  42.     for (; p > 0; p -= p & (-p)) ret += dr[p][par];
  43.     return ret;
  44. }
  45. ll getH(int a, int b, int par)
  46. {
  47.     ll H = get(b, par) - get(a-1, par) + mod*mod;
  48.     H %= mod;
  49.     H *= INV[a];
  50.     return (H % mod);
  51. }
  52. ll tmphash[2];
  53.  
  54. void update(int a, int ile, int par)
  55. {
  56.     add(a, - (t[a][par] * pot[a] % mod), par);
  57.     t[a][par] = ile;
  58.     add(a, t[a][par] * pot[a] % mod, par);
  59. }
  60.  
  61. bool thesame(int a, int b)
  62. {
  63.     ll H1 = getH(a, b, 0);
  64.     ll H2 = getH(n - b + 1, n - a + 1, 1);
  65.     return (H1 == H2);
  66. }
  67. int query(int a, int par)
  68. {
  69.     if (a == n && par == 3) return -1;
  70.     int lewy = a, prawy = ((par == 3)?a+1:a);
  71.     int x = 0, y = min(lewy - 1, n - prawy);
  72.     while (x < y)
  73.     {
  74.         int k = ((x + y) >> 1) + 1;
  75.         if (thesame(lewy - k, prawy + k)) x = k;
  76.         else y = --k;
  77.     }
  78.     int k = x;
  79.     if (par == 3)
  80.     {
  81.         if (k < 1 && t[lewy][0] != t[prawy][0]) return -1;
  82.         return 2*k+2;
  83.     }
  84.     return 2*k+1;
  85. }
  86. int main()
  87. {
  88.     boost;
  89.     cin >> s >> zap;
  90.     n = s.length();
  91.     pot[0] = 1;
  92.     for (int i=1; i<=(n+2); ++i) pot[i] = (pot[i-1] * P) % mod;
  93.     for (int i=0; i<=(n+2); ++i) INV[i] = potmod(pot[i], mod - 2);
  94.     for (int i=0; i<n; ++i)
  95.     {
  96.         int akt = i + 1;
  97.         t[akt][0] = s[i] - 'a' + 1;
  98.         t[n - akt + 1][1] = s[i] - 'a' + 1;
  99.     }
  100.  
  101.     for (int j=0; j<2; ++j)
  102.       for (int i=1; i<=n; ++i) add(i, (pot[i] * t[i][j])%mod, j);
  103.     for (int z=1; z<=zap; ++z)
  104.     {
  105.         int q, poz;
  106.         char znak;
  107.         cin >> q >> poz;
  108.         if (q == 1)
  109.         {
  110.             cin >> znak;
  111.             update(poz, znak - 'a' + 1, 0);
  112.             update(n - poz + 1, znak - 'a' + 1, 1);
  113.         }
  114.         else cout << query(poz, q) << '\n';
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement