题目大意:给一行字符串,两种操作:change(pos,char),将pos处字符改为char;isPalindrome(i,j),询问[i,j]之间是否为回文字符串。

题目分析:做正反两次字符串哈希,如果哈希值一样则回文。用线段树维护哈希值,单点更新即可。

我的挫代码如下:

# include<cstdio>
# include<iostream>
# include<cstring>
# include<algorithm>
using namespace std;
# define mid (l+(r-l)/2) const int N=100000; int seed[2]={31,131};
unsigned int base[2][N+5]; char str[N+5];
char op[2];
unsigned int tr_left[2][N*4+5];
unsigned int tr_right[2][N*4+5]; struct Node{
unsigned int left[2];
unsigned int right[2];
}; inline void init()
{
for(int i=0;i<2;++i){
base[i][0]=1;
for(int j=1;j<=N;++j){
base[i][j]=base[i][j-1]*seed[i];
}
}
} inline void read(int &x)
{
x=0;
char c;
while((c=getchar())&&(c<'0'||c>'9'));
x=c-'0';
while(c=getchar()){
if(c<'0'||c>'9') break;
x=x*10+c-'0';
}
} inline bool ok(Node *a)
{
for(int i=0;i<2;++i)
if(a->left[i]!=a->right[i]) return false;
return true;
} inline void pushUp(int rt,int l,int r)
{
for(int i=0;i<2;++i){
tr_left[i][rt]=tr_left[i][rt<<1]*base[i][r-mid]+tr_left[i][rt<<1|1];
tr_right[i][rt]=tr_right[i][rt<<1|1]*base[i][mid-l+1]+tr_right[i][rt<<1];
}
} inline void build(int rt,int l,int r)
{
if(l==r){
for(int i=0;i<2;++i)
tr_left[i][rt]=tr_right[i][rt]=str[l]-'a'+1;
}else{
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
pushUp(rt,l,r);
}
} inline void update(int rt,int l,int r,int x,char c)
{
if(l==r){
for(int i=0;i<2;++i)
tr_left[i][rt]=tr_right[i][rt]=c-'a'+1;
}else{
if(x<=mid) update(rt<<1,l,mid,x,c);
else update(rt<<1|1,mid+1,r,x,c);
pushUp(rt,l,r);
}
} inline Node* query(int rt,int l,int r,int L,int R)
{
Node* nde=new Node;
if(L<=l&&r<=R){
for(int i=0;i<2;++i){
nde->left[i]=tr_left[i][rt];
nde->right[i]=tr_right[i][rt];
}
}else{
Node* nde1=NULL;
Node* nde2=NULL;
if(L<=mid) nde1=query(rt<<1,l,mid,L,min(R,mid));
if(R>mid) nde2=query(rt<<1|1,mid+1,r,max(mid+1,L),R);
if(nde1!=NULL&&nde2!=NULL){
for(int i=0;i<2;++i){
nde->left[i]=nde1->left[i]*base[i][R-mid]+nde2->left[i];
nde->right[i]=nde2->right[i]*base[i][mid-L+1]+nde1->right[i];
}
}else{
if(nde1!=NULL){
for(int i=0;i<2;++i){
nde->left[i]=nde1->left[i];
nde->right[i]=nde1->right[i];
}
}else if(nde2!=NULL){
for(int i=0;i<2;++i){
nde->left[i]=nde2->left[i];
nde->right[i]=nde2->right[i];
}
}
}
if(nde1!=NULL) delete nde1;
if(nde2!=NULL) delete nde2;
}
return nde;
} int main()
{
init();
int m;
while(~scanf("%s",str))
{
int n=strlen(str);
build(1,0,n-1);
scanf("%d",&m);
int a,b;
char ch[2];
while(m--)
{
scanf("%s",op);
if(op[0]=='p'){
read(a);
read(b);
Node *nde=query(1,0,n-1,a-1,b-1);
if(ok(nde)) printf("Yes\n");
else printf("No\n");
delete nde;
}else if(op[0]=='c'){
read(a);
scanf("%s",ch);
update(1,0,n-1,a-1,ch[0]);
}
}
}
return 0;
}

  

URAL-1989 Subpalindromes(单点更新+hash)的更多相关文章

  1. URAL 1989 Subpalindromes (多项式hash) +【线段树】

    <题目链接> <转载于 >>>  > 题目大意:给你一段字符串,进行两种操作:1.询问[l,r]这个区间中的字符串是否是回文串: 2.更改该字符串中对应下标的 ...

  2. N - Subpalindromes URAL - 1989 哈希+线段树

    N - Subpalindromes URAL - 1989 这个是一个哈希+线段树,这个题目也不算特别难,但是呢,还比较有意思. 这个题目给你两个操作,一个是回答l~r 区间是不是回文,一个是对一个 ...

  3. ural1989 单点更新+字符串hash

    正解是双哈希,不过一次哈希也能解决.. 然后某个数字就对应一个字符串,虽然有些不同串对应同一个数字,但是概率非常小,可以忽略不计.从左到右.从右到左进行两次hash,如果是回文串,那么对应的整数必定存 ...

  4. 单点更新线段树 RMQ

    D. Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. ural 1989 subplindromes

    https://vjudge.net/problem/URAL-1989 题意: 先给出一个字符串,对于这个字符串,有两种操作,一种是询问从下标x到y的串是不是回文串,另一种是将下标为pos的字符改为 ...

  6. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  7. HDU 1166 敌兵布阵 线段树单点更新求和

    题目链接 中文题,线段树入门题,单点更新求和,建一棵树就可以了. #include <iostream> #include <cstdio> #include <cmat ...

  8. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

  9. UVA 12299 RMQ with Shifts(线段树:单点更新)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

随机推荐

  1. MongoDB 查询 (转) 仅限于C++开发

    1.find MongoDB使用find来进行查询.查询就是返回一个集合中文档的子集,子集合的范围从0个文档到整个集合.find的第一个参数 决定了要返回哪些文档.其形式也是一个文档,说明要查询的细节 ...

  2. c++形参和实参同名时,如何单步执行观察形参的变化。

    c++形参和实参同名时,如何单步执行观察形参的变化? 方法:当程序运行到函数中时,添加变量观察即可.

  3. Oracle数据库DECODE函数的使用.

    decode函数是Oracle数据库独有的. 语法为: decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 例子:select decode(sign(变量1-变量2) ...

  4. php源码编译常见错误解决方案

    在CentOS编译PHP5的时候有时会遇到以下的一些错误信息,基本上都可以通过yum安装相应的库来解决.以下是具体的一些解决办法: checking for BZip2 support… yes ch ...

  5. 黑马程序员——【Java基础】——多线程

    ---------- android培训.java培训.期待与您交流! ---------- 一.概述 (一)进程 正在执行中的程序,每一个进程执行都有一个执行顺序.该顺序是一个执行路径,或者叫一个控 ...

  6. BZOJ 2296 随机种子

    RT. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  7. Tips about Object-oriented programming

    1, Return subinterface For example, we have a parent interface: public interface A<T extends A< ...

  8. HDU 1054

    http://acm.hdu.edu.cn/showproblem.php?pid=1054 二分图最少顶点覆盖,模板题,双向边最后结果/2 #include <iostream> #in ...

  9. MySQL 仅保留7天、一个月数据

    /************************************************************************** * MySQL 仅保留7天.一个月数据 * 说明 ...

  10. LeetCode Permutations (全排列)

    题意: 给出n个元素,请产生出所有的全排列. 思路: 注意到可能会有相同的排列出现,比如 {2,2}.还有可能是乱序列(大部分情况下都是无所谓的). 递归(1):产生的过多的多余vector. cla ...