Subpalindromes 【URAL - 1989】【线段树+哈希hash】

题目链接

一道线段树单点更新加上哈希的简单题。。。

完整代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const ull Hash=1e9+7;
const int maxN=1e5+5;
int N;
char s[maxN], ques[100];
ull a[maxN], ba[maxN];
struct node
{
    ull left, right;
    node(ull a=0, ull b=0):left(a), right(b) {}
}tree[maxN<<2];
void pre_did()
{
    ba[0]=1;
    for(int i=1; i<maxN; i++) ba[i]=ba[i-1]*Hash;
}
void pushup(int rt, int l, int r)
{
    int mid=(l+r)>>1;
    tree[rt].left=tree[rt<<1].left*ba[r-mid]+tree[rt<<1|1].left;
    tree[rt].right=tree[rt<<1].right+tree[rt<<1|1].right*ba[mid-l+1];
}
void buildTree(int rt, int l, int r)
{
    if(l==r)
    {
        tree[rt].left=tree[rt].right=s[l];
        return;
    }
    int mid=(l+r)>>1;
    buildTree(rt<<1, l, mid);
    buildTree(rt<<1|1, mid+1, r);
    pushup(rt, l, r);
}
void update(int rt, int l, int r, int qx, char val)
{
    if(l==r)
    {
        tree[rt]=node(val, val);
        return;
    }
    int mid=(l+r)>>1;
    if(qx<=mid) update(rt<<1, l, mid, qx, val);
    else update(rt<<1|1, mid+1, r, qx, val);
    pushup(rt, l, r);
}
void query(int rt, int l, int r, int ql, int qr, ull &hash1, ull &hash2)
{
    if(ql<=l && qr>=r)
    {
        hash1 = ( hash1 + tree[rt].left*ba[qr-r] );
        hash2 = ( hash2 + tree[rt].right*ba[l-ql] );
        return;
    }
    int mid=(l+r)>>1;
    if(ql<=mid) query(rt<<1, l, mid, ql, qr, hash1, hash2);
    if(qr>mid) query(rt<<1|1, mid+1, r, ql, qr, hash1, hash2);
}
int main()
{
    pre_did();
    while(scanf("%s", s+1)!=EOF)
    {
        int len=(int)strlen(s+1);
        buildTree(1, 1, len);
        scanf("%d", &N);
        while(N--)
        {
            getchar();
            scanf("%s", ques);
            if(ques[0]=='p')
            {
                int l ,r;
                scanf("%d %d", &l, &r);
                ull tmp1=0, tmp2=0;
                query(1, 1, len, l, r, tmp1, tmp2);
                if(tmp1 == tmp2) printf("Yes\n");
                else printf("No\n");
            }
            else
            {
                int x;  char change;
                scanf("%d %c", &x, &change);
                update(1, 1, len, x, change);
            }
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值