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. 几行代码实现cookie的盗取

    前言 上一篇文章中介绍了XSS(跨站脚本攻击)简单原理与几种类型.接下来通过实例用几行代码实现cookie的盗取. 正文 这里测试用的工具是DVWA(可以本地搭建,前面文章有介绍),和phpstudy ...

  2. AJ学IOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮

    AJ分享,必须精品 一:效果图 自定义tabBar实现最下面中间的添加按钮 二:思路 首先在自己的tabBarController中把系统的tabBar设置成自己的tabBar(NYTabBar),这 ...

  3. 【转】Centos7启动网卡(获取ip地址)

    这里之所以是查看下IP ,是我们后面要建一个Centos远程工具Xshell 连接Centos的时候,需要IP地址,所以我们这里先 学会查看虚拟机里的Centos7的IP地址 首先我们登录操作系统 用 ...

  4. Git应用详解第十讲:Git子库:submodule与subtree.md

    前言 前情提要:Git应用详解第九讲:Git cherry-pick与Git rebase 一个中大型项目往往会依赖几个模块,git提供了子库的概念.可以将这些子模块存放在不同的仓库中,通过submo ...

  5. H - Hamiltonian Hypercube Gym - 101170H

    规律题 首先我们要知道他的顺序是怎么来的,首先当n等于1时,是0,1 当n=2时,先按照与按顺序在他们前面分别加0,即00,01,在逆序加1,即11,10 构成的顺序为00,01,11,10:往后同理 ...

  6. calculator.py

    代码如下: #计算器类 class Count: def __init__(self, a, b): self.a = int(a) self.b = int(b) #计算器加法 def add(se ...

  7. Python 【面试强化宝典】

    四大数据类型的常用方法 列表常用方法 #1. append 用于在列表末尾追加新的对象 a = [1,2,3] a.append(4) #the result : [1, 2, 3, 4] #2. c ...

  8. Springboot:员工管理之登录、拦截器(十(4))

    1:构建登录javaBean com\springboot\vo\LoginUser.java package com.springboot.vo; import lombok.Data; @Data ...

  9. 深入理解kestrel的应用

    1 前言 之所以写本文章,是因为在我停止维护多年前写的NetworkSocket组件两年多来,还是有一些开发者在关注这个项目,我希望有类似需求的开发者明白为什么要停止更新,可以使用什么更好的方式来替换 ...

  10. Vue 3.0 Composition API - 中文翻译

    Composition API 发布转载请附原文链接 https://www.cnblogs.com/zgh-blog/articles/composition_api.html 这两天初步了解了下 ...