题意:

维护一个长度为n的字符串,两种操作:

1.将 [l,r] 的字符变为 c

2.询问 d 是否为 $S(l,r)$ 的周期

解法:

首先分析如何令 [l,r] 的周期为d,利用循环串的性质得:

只需要保证 $S(l+d,r) = S(l,r-d)$ 即可。

注意$S(l,r)$周期为 d 的定义是对于$l<=i<=r-d$有$S(i+d) = S(i)$从而,只需要维护hash即可。

应用线段树可以$O(nlogn)$

注意hash一般都要用两个模数,不然极易被卡。

好久没写hash,WA了几下。

 #include <iostream>
#include <cstdio>
#include <cstring> #define l(x) ch[x][0]
#define r(x) ch[x][1]
#define N 100010
#define LL unsigned long long
#define P 1000000007ULL using namespace std; int totn,n,m,K;
int ch[N<<][];
char S[N];
int siz[N<<],setv[N<<];
LL power[N],power_mod[N];
LL Spower[N],Spower_mod[N];
LL hashv[N<<];
LL hash_mod[N<<]; void update(int x)
{
siz[x]=siz[l(x)]+siz[r(x)];
hashv[x] = hashv[l(x)]*power[siz[r(x)]] + hashv[r(x)];
hash_mod[x] = (hash_mod[l(x)]*power_mod[siz[r(x)]]%P + hash_mod[r(x)])%P;
} void push(int x)
{
if(setv[x]==-) return;
setv[l(x)]=setv[r(x)]=setv[x];
hashv[l(x)]=Spower[siz[l(x)]-] * (LL)setv[x];
hashv[r(x)]=Spower[siz[r(x)]-] * (LL)setv[x];
hash_mod[l(x)]=Spower_mod[siz[l(x)]-] * (LL)setv[x] % P;
hash_mod[r(x)]=Spower_mod[siz[r(x)]-] * (LL)setv[x] % P;
setv[x]=-;
} int build(int l,int r)
{
int x=++totn;
setv[x]=-;
if(l==r)
{
hash_mod[x]=hashv[x]=S[l-]-'';
siz[x]=;
return x;
}
int mid=(l+r)>>;
l(x)=build(l,mid);
r(x)=build(mid+,r);
update(x);
return x;
} LL ask_mod(int x,int l,int r,int ql,int qr,int &sizv)
{
if(ql<=l && r<=qr)
{
sizv=siz[x];
return hash_mod[x];
}
push(x);
int mid=(l+r)>>,tmp_sizv;
if(ql<=mid && mid<qr)
{
sizv=;
LL tmp1=ask_mod(l(x),l,mid,ql,qr,tmp_sizv);
sizv+=tmp_sizv;
LL tmp2=ask_mod(r(x),mid+,r,ql,qr,tmp_sizv);
sizv+=tmp_sizv;
update(x);
return (tmp1*power_mod[tmp_sizv]%P+tmp2)%P;
}
if(ql<=mid)
{
LL ans=ask_mod(l(x),l,mid,ql,qr,sizv);
update(x);
return ans;
}
else
{
LL ans=ask_mod(r(x),mid+,r,ql,qr,sizv);
update(x);
return ans;
}
} LL ask(int x,int l,int r,int ql,int qr,int &sizv)
{
if(ql<=l && r<=qr)
{
sizv=siz[x];
return hashv[x];
}
push(x);
int mid=(l+r)>>,tmp_sizv;
if(ql<=mid && mid<qr)
{
sizv=;
LL tmp1=ask(l(x),l,mid,ql,qr,tmp_sizv);
sizv+=tmp_sizv;
LL tmp2=ask(r(x),mid+,r,ql,qr,tmp_sizv);
sizv+=tmp_sizv;
update(x);
return tmp1*power[tmp_sizv]+tmp2;
}
if(ql<=mid)
{
LL ans=ask(l(x),l,mid,ql,qr,sizv);
update(x);
return ans;
}
else
{
LL ans=ask(r(x),mid+,r,ql,qr,sizv);
update(x);
return ans;
}
} void set(int x,int l,int r,int ql,int qr,int qv)
{
if(ql<=l && r<=qr)
{
setv[x] = qv;
hashv[x] = Spower[siz[x]-]*(LL)setv[x];
hash_mod[x] = (Spower_mod[siz[x]-] * (LL)setv[x]) % P;
return;
}
push(x);
int mid=(l+r)>>;
if(ql<=mid) set(l(x),l,mid,ql,qr,qv);
if(mid<qr) set(r(x),mid+,r,ql,qr,qv);
update(x);
} int main()
{
power[]=power_mod[]=;
for(int i=;i<N;i++)
{
power[i] = power[i-]*10ULL;
power_mod[i] = power_mod[i-]*10ULL%P;
}
Spower[]=Spower_mod[]=;
for(int i=;i<N;i++)
{
Spower[i] = Spower[i-] + power[i];
Spower_mod[i] = (Spower_mod[i-] + power_mod[i])%P;
}
while(~scanf("%d%d%d",&n,&m,&K))
{
scanf("%s",S);
totn=;
build(,n);
int cmd,l,r,x;
for(int i=;i<=m+K;i++)
{
scanf("%d%d%d%d",&cmd,&l,&r,&x);
if(cmd==) set(,,n,l,r,x);
else
{
int d=x;
if(r-l+==d)
{
puts("YES");
continue;
}
if(ask(,,n,l+d,r,x)==ask(,,n,l,r-d,x)
&& ask_mod(,,n,l+d,r,x)==ask_mod(,,n,l,r-d,x))
puts("YES");
else puts("NO");
}
}
}
return ;
}
/*
20 1 2
34075930750342906718
2 1 20 20
1 1 20 6
2 1 20 1
*/

Kefa and Watch的更多相关文章

  1. codeforces 580D:Kefa and Dishes

    Description When Kefa came to the restaurant and sat at a table, the waiter immediately brought him ...

  2. CF 321B Kefa and Company(贪心)

    题目链接: 传送门 Kefa and Company time limit per test:2 second     memory limit per test:256 megabytes Desc ...

  3. Kefa and Park

    #include<bits/stdc++.h> #define max 100005 using namespace std; int cats[max]; vector<int&g ...

  4. B - Kefa and Company

    B - Kefa and Company Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  5. Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp

    题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...

  6. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  7. Codeforces Round #321 (Div. 2) C. Kefa and Park dfs

    C. Kefa and Park Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/probl ...

  8. Codeforces Round #321 (Div. 2) B. Kefa and Company 二分

    B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...

  9. Codeforces Round #321 (Div. 2) A. Kefa and First Steps 水题

    A. Kefa and First Steps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/58 ...

  10. Codeforces 841A - Generous Kefa

    题目链接:http://codeforces.com/problemset/problem/841/A One day Kefa found n baloons. For convenience, w ...

随机推荐

  1. adb命令具体解释(二)——手机缺失sqlite3时操作数据库的多种解决方式

    在android应用开发无处不在SQLite数据库的身影.那么在开发中怎么使用adb命令操作数据库的功能呢? 以下我们将完整的介绍与数据库操作相关的命令集及当手机缺少sqlite3的时候的多种解决方式 ...

  2. trigger_error

    PHP trigger_error 自定义错误级别抛出错误. //如果设置成notice或warning则程序继续向下执行,如果是致命级别的错误则终止执行 $num1 = 1; if (is_numb ...

  3. 在Linux中将php-fpm配置成服务的方法

    1.配置php-fpm.conf vi /usr/local/php/etc/php-fpm.conf php-fpm.pid 目录必须指向:/usr/local/php/var/run/php-fp ...

  4. 1.新手上路:Windows下,配置Qt环境

    个人体会: 我最初只是想看看C++除了"黑窗口"之外,怎么才能做一些"更好看的东西".之后在网上看到有人推荐Qt,就看了一下官网(https://www.qt. ...

  5. ubuntu 下的中文输入法的安装和配置- ibus

    ibus输入法 Chinese语言包安装 首先需要给Ubuntu16.04安装Chinese语言包支持.  如上图点击其中的Install/Remove Languages…,这个对话框是通过syst ...

  6. jquery+easyui主界面布局一例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="workbench.aspx ...

  7. windows下route命令详解(转载)

    1.具体功能        该命令用于在本地IP路由表中显示和修改条目.使用不带参数的ROUTE可以显示帮助.            2.语法详解        route [-f] [-p] [co ...

  8. 简述C++中的多态机制

    前言 封装性,继承性,多态性是面向对象语言的三大特性.其中封装,继承好理解,而多态的概念让许多初学者感到困惑.本文将讲述C++中多态的概念以及多态的实现机制. 什么是多态? 多态就是多种形态,就是许多 ...

  9. Hibernate中的Sesson操作

    一.Session概述 Session是应用程序与数据库之间的一个会话,是Hibernate运作的中心,持久层操作的基础,相当于JDBC中的Connection.Session对象是通过Session ...

  10. ChannelHandler揭秘(Netty源码死磕5)

    精进篇:netty源码死磕5  揭开 ChannelHandler 的神秘面纱 目录 1. 前言 2. Handler在经典Reactor中的角色 3. Handler在Netty中的坐标位置 4. ...