Codeforces Round #321 (Div. 2)E 线段树+字符串hash

本文介绍了一种使用双关键字哈希与线段树解决字符串周期性检查问题的方法,通过构建线段树来高效地处理字符串修改及周期性验证查询。

E. Kefa and Watch
time limit per test1.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 c (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”.

Examples
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.

这个题有几个坑点
第一个就是需要双关键字hash
第二个是在线段树更新的过程中需要去掉不需要的长度
第三个是lazy在用到的时候必须进行更新
其实也不算坑点
还是自己菜吧…

#include<iostream>
#include<cstring>
#include<string>
#include<map>
using namespace std;
int mo1 = 1000000009, mo2 = 1000000007;
unsigned long long p1[100011], s1[110001], p2[110001], s2[101001];
int bas = 17;
struct qq
{
    long long z, y, chang, z1, z2, lazy;
};
qq shu[600001];
string q;
void jian(int gen, int zuo, int you)
{
    shu[gen].z = zuo;
    shu[gen].y = you;
    shu[gen].lazy = -1;
    if (zuo == you)
    {
        shu[gen].z1 = (q[zuo-1] - '0') % mo1;
        shu[gen].z2 = (q[zuo-1] - '0') % mo2;
        return;
    }
    int mid = (zuo + you) / 2;
    jian(2 * gen, zuo, mid);
    jian(2 * gen + 1, mid + 1, you);
    shu[gen].z1 = (p1[shu[2 * gen + 1].y - shu[2 * gen + 1].z +1] * shu[2 * gen].z1 + shu[2 * gen + 1].z1) % mo1;
    shu[gen].z2 = (p2[shu[2 * gen + 1].y - shu[2 * gen + 1].z +1] * shu[2 * gen].z2 + shu[2 * gen + 1].z2) % mo2;
}
void gengxin(int gen, int zuo, int you, int wz, int wy, int bian)
{
    if (zuo >= wz&&you <= wy)
    {
        shu[gen].lazy = bian;
        shu[gen].z1 = bian*s1[you - zuo] % mo1;
        shu[gen].z2 = bian*s2[you - zuo] % mo2;
        return;
    }
    else if (zuo > wy || you < wz)return;
    else
    {
        int mid = (zuo + you) / 2;
        if (shu[gen].lazy != -1)
        {
            shu[2 * gen].lazy = shu[2 * gen + 1].lazy = shu[gen].lazy;
            shu[gen].lazy = -1;
            shu[2 * gen].z1 = (shu[2 * gen].lazy*s1[shu[2 * gen].y - shu[2 * gen].z]) % mo1;
            shu[2 * gen].z2 = (shu[2 * gen].lazy*s2[shu[2 * gen].y - shu[2 * gen].z]) % mo2;
            shu[2 * gen + 1].z1 = (shu[2 * gen].lazy*s1[shu[2 * gen + 1].y - shu[2 * gen + 1].z]) % mo1;
            shu[2 * gen + 1].z2 = (shu[2 * gen].lazy*s2[shu[2 * gen + 1].y - shu[2 * gen + 1].z]) % mo2;
        }
        gengxin(2 * gen, zuo, mid, wz, wy, bian);
        gengxin(2 * gen + 1, mid + 1, you, wz, wy, bian);
        shu[gen].z1 = (p1[shu[2 * gen + 1].y - shu[2 * gen + 1].z + 1] * shu[2 * gen].z1 + shu[2 * gen + 1].z1) % mo1;
        shu[gen].z2 = (p2[shu[2 * gen + 1].y - shu[2 * gen + 1].z + 1] * shu[2 * gen].z2 + shu[2 * gen + 1].z2) % mo2;
    }
}
int flag = 0;
pair<pair<long long,long long>, long long> xunw(int gen, int zuo, int you, int wz, int wy)
{
    if (zuo > wy || you < wz)return make_pair(make_pair(-1, -1),0);
    if (zuo >= wz&&you <= wy)return make_pair(make_pair(shu[gen].z1, shu[gen].z2),you-zuo+1);
    int mid = (zuo + you) / 2;
    if (shu[gen].lazy != -1)
    {
        shu[2 * gen].lazy = shu[2 * gen + 1].lazy = shu[gen].lazy;
        shu[gen].lazy = -1;
        shu[2 * gen].z1 = (shu[2 * gen].lazy*s1[shu[2 * gen].y - shu[2 * gen].z]) % mo1;
        shu[2 * gen].z2 = (shu[2 * gen].lazy*s2[shu[2 * gen].y - shu[2 * gen].z]) % mo2;
        shu[2 * gen + 1].z1 = (shu[2 * gen].lazy*s1[shu[2 * gen + 1].y - shu[2 * gen + 1].z]) % mo1;
        shu[2 * gen + 1].z2 = (shu[2 * gen].lazy*s2[shu[2 * gen + 1].y - shu[2 * gen + 1].z]) % mo2;
    }
    pair<pair<long long, long long>, long long>zz = xunw(2 * gen, zuo, mid, wz, wy);
    pair<pair<long long, long long>, long long>yy = xunw(2 * gen + 1, mid + 1, you, wz, wy);
    if (zz.first.first == -1)return yy;
    if (yy.first.first == -1)return zz;
    int youc = yy.second;
    return make_pair(make_pair((zz.first.first*p1[youc] + yy.first.first) % mo1, (zz.first.second*p2[youc] + yy.first.second) % mo2),zz.second+yy.second);
}
int main()
{
    int n, m, k;
    cin >> n >> m >> k;
    cin >> q;
    p1[0] = p2[0] = s1[0] = s2[0] = 1;
    for (int a = 1;a <= q.size();a++)
    {
        p1[a] = p1[a - 1] * bas%mo1;
        s1[a] = (s1[a - 1] + p1[a]) % mo1;
        p2[a] = p2[a - 1] * bas%mo2;
        s2[a] = (s2[a - 1] + p2[a]) % mo2;
    }
    int r, t, y, u;
    jian(1, 1, q.size());
    for (int a = 1;a <= m + k;a++)
    {
        scanf("%d%d%d%d", &r, &t, &y, &u);
        if (r == 1)gengxin(1, 1, q.size(), t, y, u);
        else
        {
            if (y - t + 1 == u)
            {
                cout << "YES" << endl;
                continue;
            }
            pair<pair<long long, long long>, long long> qwe = xunw(1, 1, q.size(), t, y - u);
            pair<pair<long long, long long>, long long> wer = xunw(1, 1, q.size(), t+u, y);
            if (qwe.first.first == wer.first.first&&qwe.first.second == wer.first.second)cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
    return 0;
}
基于部落竞争与成员合作算法(CTCM)融合动态窗口法DWA的无人机三维动态避障方法研究,MATLAB代码 动态避障路径规划:基于部落竞争与成员合作算法(CTCM)融合动态窗口法DWA的无人机三维动态避障方法研究,MATLAB 融合DWA的青蒿素优化算法(AOA)求解无人机三维动态避障路径规划,MATLAB代码 基于动态环境下多智能体自主避障路径优化的DWA算法研究,MATLAB代码 融合DWA的青蒿素优化算法AOA求解无人机三维动态避障路径规划,MATLAB代码 基于DWA的多智能体动态避障路径规划算法研究,MATLAB代码 融合动态窗口法DWA的粒子群算法PSO求解无人机三维动态避障路径规划研究,MATLAB代码 基于粒子群算法PSO融合动态窗口法DWA的无人机三维动态避障路径规划研究,MATLAB代码 基于ACOSRAR-DWA无人机三维动态避障路径规划,MATLAB代码 基于ACOSRAR-DWA无人机三维动态避障路径规划,MATLAB代码 基于DWA的动态环境下无人机自主避障路径优化,MATLAB代码 基于DWA的动态环境下机器人自主避障路径规划,MATLAB代码 基于城市场景下RRT、ACO、A*算法的无人机三维路径规划方法研究,MATLAB代码 基于城市场景下无人机三维路径规划的导航变量的多目标粒子群优化算法(NMOPSO),MATLAB代码 导航变量的多目标粒子群优化算法(NMOPSO)求解复杂城市场景下无人机三维路径规划,MATLAB代码 原创:5种最新多目标优化算法求解多无人机协同路径规划(多起点多终点,起始点、无人机数、障碍物可自定义),MATLAB代码 原创:4种最新多目标优化算法求解多无人机协同路径规划(多起点多终点,起始点、无人机数、障碍物可自定义),MATLAB代码 高维超多目标优化:基于导航变量的多目标粒子群优化算法(NMOPSO)的无人机三维
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值