Codeforces 580E Kefa and Watch 线段树+双值Hash

传送门:http://codeforces.com/contest/580/problem/E

E. Kefa and Watch
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One day Kefa the parrot was walking down the street as he was on the way home from the restaurant when he saw something glittering by the road. As he came nearer he understood that it was a watch. He decided to take it to the pawnbroker to earn some money.

The pawnbroker said that each watch contains a serial number represented by a string of digits from 0 to 9, and the more quality checks this number passes, the higher is the value of the watch. The check is defined by three positive integers l, r and d. The watches pass a check if a substring of the serial number from l to r has period d. Sometimes the pawnbroker gets distracted and Kefa changes in some substring of the serial number all digits to c in order to increase profit from the watch.

The seller has a lot of things to do to begin with and with Kefa messing about, he gave you a task: to write a program that determines the value of the watch.

Let us remind you that number x is called a period of string s (1 ≤ x ≤ |s|), if si  =  si + x for all i from 1 to |s|  -  x.

Input

The first line of the input contains three positive integers n, m and k (1 ≤ n ≤ 105, 1 ≤ m + k ≤ 105) — the length of the serial number, the number of change made by Kefa and the number of quality checks.

The second line contains a serial number consisting of n digits.

Then m + k lines follow, containing either checks or changes.

The changes are given as 1 l r с (1 ≤ l ≤ r ≤ n, 0 ≤ c ≤ 9). That means that Kefa changed all the digits from the l-th to the r-th to be c.

The checks are given as 2 l r d (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ r - l + 1).

Output

For each check on a single line print "YES" if the watch passed it, otherwise print "NO".

Sample test(s)
Input
3 1 2
112
2 2 3 1
1 1 3 8
2 1 2 1
Output
NO
YES
Input
6 2 3
334934
2 2 5 2
1 4 4 3
2 1 6 3
1 2 3 8
2 3 6 1
Output
NO
YES
NO
Note

In the first sample test two checks will be made. In the first one substring "12" is checked on whether or not it has period 1, so the answer is "NO". In the second one substring "88", is checked on whether or not it has period 1, and it has this period, so the answer is "YES".

In the second statement test three checks will be made. The first check processes substring "3493", which doesn't have period 2. Before the second check the string looks as "334334", so the answer to it is "YES". And finally, the third check processes substring "8334", which does not have period 1.

题意:给出一个长度小于10w的0~9的串,有m次区间覆盖修改和k次区间询问。对于询问,是询问区间l,r内,是否为循环节长度为d的串。

思路:用线段树维护这个序列的hash值(因为有修改操作).当询问l,r时,如果区间长度小于等于d,那么直接输出YES,如果区间长度小于等于2d.那么检查多余的部分是否和前面相等。对于更一般的情况,是长度>2d的,这时候,先特判多余的部分,剩下的区间的长度就是d的整数倍数(假设为k倍),如果前k-1部分与后k-1部分相同,那么这个就是循环节长度为d的串。(这里仔细想想就好啦,关键就是这个问题)。

坑:第75组数组卡无符号64位自动溢出的hash,所以我们需要换一种hash的方法,分别mod 1e9+7和1e9+9即可。如果两个hash值分别对应相等,那么就认为串相等。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef unsigned __int64 uLL;
const int maxn = 100005;
const int MOD1 = 1000000007;
const int MOD2 = 1000000009;
uLL sum1[maxn << 2];
uLL power1[maxn];
uLL powersum1[maxn];
uLL sum2[maxn << 2];
uLL power2[maxn];
uLL powersum2[maxn];
uLL c[maxn << 2];
void pushdown(int id, int L, int R)
{
	if (c[id] != -1)
	{
		int m = R - L + 1;
		c[id << 1] = c[id << 1 | 1] = c[id];
		sum1[id << 1] = powersum1[m - (m >> 1)] * c[id] % MOD1;
		sum1[id << 1 | 1] = powersum1[m >> 1] * c[id] % MOD1;

		sum2[id << 1] = powersum2[m - (m >> 1)] * c[id] % MOD2;
		sum2[id << 1 | 1] = powersum2[m >> 1] * c[id] % MOD2;
		c[id] = -1;
	}
}
void pushup(int id, int L, int R)
{
	int m = R - L + 1;
	sum1[id] = (sum1[id << 1] + sum1[id << 1 | 1] * power1[m - (m >> 1) + 1]) % MOD1;
	sum2[id] = (sum2[id << 1] + sum2[id << 1 | 1] * power2[m - (m >> 1) + 1]) % MOD2;
}
char v[maxn];
void build(int id, int L, int R)
{
	c[id] = -1;
	if (L == R)
	{
		sum1[id] = sum2[id] = v[L];
	}
	else
	{
		int mid = L + R >> 1;
		build(id << 1, L, mid);
		build(id << 1 | 1, mid + 1, R);
		pushup(id, L, R);
	}
}
void que(int id, int L, int R, int l, int r, uLL &hash1, uLL &hash2)
{
	if (l <= L&&R <= r)
	{
		hash1 = (hash1 + power1[L - l + 1] * sum1[id]) % MOD1;
		hash2 = (hash2 + power2[L - l + 1] * sum2[id]) % MOD2;
	}
	else
	{
		pushdown(id, L, R);
		int mid = L + R >> 1;
		if (l <= mid)  que(id << 1, L, mid, l, r, hash1, hash2);
		if (mid < r) que(id << 1 | 1, mid + 1, R, l, r, hash1, hash2);
	}
}
void op(int id, int L, int R, int l, int r, char cc)
{
	if (l <= L&&R <= r)
	{
		c[id] = cc;
		sum1[id] = (powersum1[R - L + 1] * cc) % MOD1;
		sum2[id] = (powersum2[R - L + 1] * cc) % MOD2;
	}
	else
	{
		pushdown(id, L, R);
		int mid = L + R >> 1;
		if (l <= mid) op(id << 1, L, mid, l, r, cc);
		if (mid < r) op(id << 1 | 1, mid + 1, R, l, r, cc);
		pushup(id, L, R);
	}
}
int main()
{
	power1[1] = powersum1[1] = power2[1] = powersum2[1] = 1;
	for (int i = 2; i < maxn; i++)
	{
		power1[i] = (power1[i - 1] * 131) % MOD1;
		power2[i] = (power2[i - 1] * 131) % MOD2;
		powersum1[i] = (powersum1[i - 1] + power1[i]) % MOD1;
		powersum2[i] = (powersum2[i - 1] + power2[i]) % MOD2;
	}
	int n, m, k;
	scanf("%d %d %d", &n, &m, &k);
	scanf("%s", v + 1);
	build(1, 1, n);
	m += k;
	while (m--)
	{
		int opt;
		scanf("%d", &opt);
		if (opt == 1)
		{
			int l, r, cc;
			scanf("%d %d %d", &l, &r, &cc);
			cc += 48;
			op(1, 1, n, l, r, (char)cc);
		}
		else
		{
			int l, r, d;
			scanf("%d %d %d", &l, &r, &d);
			if (r - l + 1 <= d)
			{
				puts("YES");
			}
			else if (r - l + 1 <= 2 * d)
			{
				int len = r - l + 1 - d;
				uLL tmp11 = 0, tmp22 = 0, tmp12 = 0, tmp21 = 0;
				que(1, 1, n, l, l + len - 1, tmp11, tmp12);
				que(1, 1, n, r - len + 1, r, tmp21, tmp22);
				if (tmp11 == tmp21&&tmp12 == tmp22)
				{
					puts("YES");
				}
				else puts("NO");
			}
			else
			{
				if ((r - l + 1) % d == 0)
				{
					int blknum = (r - l + 1) / d;
					int len = (blknum - 1)*d;
					uLL tmp11 = 0, tmp22 = 0, tmp12 = 0, tmp21 = 0;
					que(1, 1, n, l, l + len - 1, tmp11, tmp12);
					que(1, 1, n, r - len + 1, r, tmp21, tmp22);
					if (tmp11 == tmp21&&tmp12 == tmp22)
					{
						puts("YES");
					}
					else puts("NO");
				}
				else
				{
					int blknum = (r - l + 1) / d;
					int len = (blknum - 1)*d;
					int tail = r - l + 1 - blknum*d;
					uLL tmp11 = 0, tmp22 = 0, tmp12 = 0, tmp21 = 0;
					que(1, 1, n, l, l + len - 1, tmp11, tmp12);
					que(1, 1, n, l + d, l + d + len - 1, tmp21, tmp22);
					if (tmp11 == tmp21&&tmp12 == tmp22)
					{
						uLL tmp31 = 0, tmp32 = 0, tmp41 = 0, tmp42 = 0;
						que(1, 1, n, l, l + tail - 1, tmp31, tmp32);
						que(1, 1, n, r - tail + 1, r, tmp41, tmp42);
						if (tmp31 == tmp41&&tmp32 == tmp42) puts("YES");
						else puts("NO");
					}
					else
					{
						puts("NO");
					}
				}
			}
		}
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值