N - Subpalindromes

URAL - 1989

这个是一个哈希+线段树,这个题目也不算特别难,但是呢,还比较有意思。

这个题目给你两个操作,一个是回答l~r 区间是不是回文,一个是对一个点进行单点修改。

这个用哈希还是比较好用的,首先就是把所有字符映射成一个数字,然后就相当于给你一串数字进行以上操作。

这个最好就是从小到大进行一个递增的哈希,这个看代码吧,说不太清楚。

主要注意一点就是最后要保证这个哈希的位数是一样的,具体看代码。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + ;
const ull base = ;
ull h[maxn], p[maxn];
ull sum1[maxn * ], sum2[maxn * ];
int n, m;
char str1[maxn], str2[maxn]; void push_up(int id) {
sum1[id] = sum1[id << ] + sum1[id << | ];
sum2[id] = sum2[id << ] + sum2[id << | ];
//printf("sum[%d]=%llu nisum[%d]=%llu\n", id, sum[id], id, nisum[id]);
} void build(int id, int l, int r) {
if (l == r) {
sum1[id] = (str1[l] - 'a' + )*p[l];//哈希赋值,从小往大,递增赋值
sum2[id] = (str2[l] - 'a' + )*p[l];
return;
}
int mid = (l + r) >> ;
build(id << , l, mid);
build(id << | , mid + , r);
push_up(id);
} void update(int id, int l, int r, int pos, int val, int f) {
if (l == r) {
if (f == ) sum1[id] = val * p[l];
else sum2[id] = val * p[l];
return;
}
int mid = (l + r) >> ;
if (pos <= mid) update(id << , l, mid, pos, val, f);
else update(id << | , mid + , r, pos, val, f);
push_up(id);
} ull query(int id, int l, int r, int x, int y, int f) {
if (x <= l && y >= r) {
if (f == ) return sum1[id];
return sum2[id];
}
int mid = (l + r) >> ;
ull ans = ;
if (x <= mid) ans += query(id << , l, mid, x, y, f);
if (y > mid) ans += query(id << | , mid + , r, x, y, f);
return ans;
} char s[maxn], ch[]; int main() {
scanf("%s", str1 + );
n = strlen(str1 + );
p[] = ;
for (int i = ; i <= n; i++) p[i] = p[i - ] * base, str2[i] = str1[n + - i];
build(, , n);
scanf("%d", &m);
while (m--) {
int x, y;
scanf("%s", s);
if (s[] == 'p') {
scanf("%d%d", &x, &y);
ull rest1 = query(, , n, x, y, );//这样子查询还是有点难想的
ull rest2 = query(, , n, n + - y, n + - x, );
if (x < n + - y) rest1 *= p[n + - x - y];//这个if是保证这两个rest的数量级是一样的,这个可以画一个x轴就知道了
else rest2 *= p[x + y - n - ]; if (rest1 == rest2) printf("Yes\n");
else printf("No\n");
}
else {
scanf("%d%s", &x, ch);
update(, , n, x, ch[] - 'a' + , );
update(, , n, n - x + , ch[] - 'a' + , );
}
}
}

哈希

N - Subpalindromes URAL - 1989 哈希+线段树的更多相关文章

  1. 51Nod1553 周期串查询 字符串 哈希 线段树

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1553.html 题目传送门 - 51Nod1553 题意 有一个串只包含数字字符.串的长度为n,下标 ...

  2. URAL 1297 后缀数组+线段树

    思路: 论文题--*n 倒过来接上 分奇偶讨论 求LCP 搞棵线段树即可 //By SiriusRen #include <cstdio> #include <cstring> ...

  3. 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)

    原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解    By 岩之痕 目录: 一:综述 ...

  4. 【URAL 1989】 Subpalindromes(线段树维护哈希)

    Description You have a string and queries of two types: replace i'th character of the string by char ...

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

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

  6. 线段树+哈希【CF580E】Kefa and Watch

    线段树+哈希[CF580E]Kefa and Watch Description \(n\)个数的字符串,\(m + k\)个操作 1 l r k把\(l - r\)赋值为\(k\) 2 l r d询 ...

  7. [bzoj2124]等差子序列——线段树+字符串哈希

    题目大意 给一个1到N的排列\(A_i\),询问是否存在\(p_i\),\(i>=3\),使得\(A_{p_1}, A_{p_2}, ... ,A_{p_len}\)是一个等差序列. 题解 显然 ...

  8. CF213E Two Permutations 线段树维护哈希值

    当初竟然看成子串了$qwq$,不过老师的$ppt$也错了$qwq$ 由于子序列一定是的排列,所以考虑插入$1$到$m$到$n-m+1$到$n$; 如何判断呢?可以用哈希$qwq$: 我们用线段树维护哈 ...

  9. HDU3973 线段树 + 字符哈希

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3973 , 线段树 + 字符哈希,好题. 又学了一种新的哈希方法,hhhh~ 解法: 想法是用P进制的数 ...

随机推荐

  1. https的秘钥公钥以及之间的会话流程

      一 共享秘钥 1.1 概念 共享秘钥和我们生活中同一把锁的钥匙概念类似,对同一把锁来说,加锁时使用什么钥匙,解锁也必须使用同样的钥匙. 1.2 共享秘钥在HTTP传输中的缺点 以共享密钥方式加密时 ...

  2. Python财经数据接口包TuShare的使用

    安装TuShare 方式1:pip install tushare 方式2:访问https://pypi.python.org/pypi/tushare/下载安装 方式3:将源代码下载到本地pytho ...

  3. day7作业

    # day7作业 # 1. 使用while循环输出1 2 3 4 5 6 8 9 10 count = 1 while count < 11: if count == 7: count += 1 ...

  4. K - Two Contests

    题目连接:https://atcoder.jp/contests/agc040/tasks/agc040_b 大佬题解:https://blog.csdn.net/duanghaha/article/ ...

  5. 详解 File类

    在讲解File类之前,本人先要讲解下 路径,因为我们对于文件的操作是离不开路径的: 目录 路径: File类 文件名称过滤器: 路径: 请观看本人博文 -- <详解 绝对路径与 相对路径> ...

  6. 【题解】P2024 [NOI2001]食物链 - 数据结构 - 并查集

    P2024 [NOI2001]食物链 声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 题目描述 动物王国中有三类动物 \(A,B ...

  7. socket小计

    socket,是一个实现了双向通信的链接. 将http比喻为轿车,承载数据.传递数据,那么socket,就是轿车的发动机,它轿车动起来.

  8. php中session_id()函数详细介绍,会话id生成过程及session id长度

    php中session_id()函数原型及说明session_id()函数说明:stringsession_id([string$id])session_id() 可以用来获取/设置 当前会话 ID. ...

  9. REDHAT7进入单用户模式

    Redhat7采用的是grub2,和Redhat6.x进入单用户的方法不同. 一.init方法 1.centos7的grub2界面会有两个入口,正常系统入口和救援模式: 2.修改grub2引导 在正常 ...

  10. 防cc攻击利器之Httpgrard

    一.httpgrard介绍 HttpGuard是基于openresty,以lua脚本语言开发的防cc攻击软件.而openresty是集成了高性能web服务器Nginx,以及一系列的Nginx模块,这其 ...