题目链接
一道线段树单点更新加上哈希的简单题。。。
完整代码:
#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;
}