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/problem/E

Description

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 Input

3 1 2
112
2 2 3 1
1 1 3 8
2 1 2 1

Sample Output

NO
YES

HINT

?

題意

給你一個字符集只有10的串 ,然后又兩個操作

1.區間更新

2.查詢lr區間的字符的周期是否為d

題解:

直接暴力線段樹hash就好了

查詢操作只有判斷(l,l+len-d)和(l+len-d+1,r)是否一樣就好了

可以用類似錯位來解釋

代碼來自peterpan

代碼:

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
#define input freopen("/Users/peteryuanpan/data.txt","r",stdin)#define N 100010
int n, m, k, lens;
char s[N];#define lch id<<1
#define rch id<<1|1
class HashTree{
public:int mod, p;ll powp[N], sump[N];void getpowp(){powp[0] = 1;sump[0] = 1;for(int i = 1; i < N; i++){powp[i] = powp[i-1] * p;if(powp[i] >= mod) powp[i] %= mod;sump[i] = sump[i-1] + powp[i];if(sump[i] >= mod) sump[i] %= mod;}}ll v[N << 2];int len[N << 2];char lazy[N << 2];void plant(int id,int l,int r){lazy[id] = '\0';if(l == r){v[id] = s[l];len[id] = 1;return;}int mid = (l + r) >> 1;plant(lch, l, mid);plant(rch, mid + 1, r);len[id] = len[lch] + len[rch];v[id] = v[lch] * powp[len[rch]] + v[rch];if(v[id] >= mod) v[id] %= mod;}void pushdown(int id){if(lazy[id] != '\0'){lazy[lch] = lazy[rch] = lazy[id];v[lch] = lazy[id] * sump[len[lch] - 1];if(v[lch] >= mod) v[lch] %= mod;v[rch] = lazy[id] * sump[len[rch] - 1];if(v[rch] >= mod) v[rch] %= mod;lazy[id] = '\0';}}void update(int id,int ql,int qr,int l,int r,char c){if(ql == l && qr == r){lazy[id] = c;v[id] = c * sump[len[id] - 1];if(v[id] >= mod) v[id] %= mod;return;}pushdown(id);int mid = (l + r) >> 1;if(qr <= mid) update(lch, ql, qr, l, mid, c);else if(mid < ql) update(rch, ql, qr, mid + 1, r, c);else update(lch, ql, mid, l, mid, c), update(rch, mid + 1, qr, mid + 1, r, c);v[id] = v[lch] * powp[len[rch]] + v[rch];if(v[id] >= mod) v[id] %= mod;}ll query(int id,int ql,int qr,int l,int r){if(ql == l && qr == r){return v[id];}pushdown(id);int mid = (l + r) >> 1;if(qr <= mid) return query(lch, ql, qr, l, mid);else if(mid < ql) return query(rch, ql, qr, mid + 1, r);else{ll t1 = query(lch, ql, mid, l, mid);ll t2 = query(rch, mid + 1, qr, mid + 1, r);ll t = t1 * powp[qr - (mid + 1) + 1] + t2;if(t >= mod) t %= mod;return t;}}
}tree1, tree2;bool equal(int l1, int r1, int l2, int r2){if(tree1.query(1, l1, r1, 0, lens - 1) != tree1.query(1, l2, r2, 0, lens - 1)) return false;if(tree2.query(1, l1, r1, 0, lens - 1) != tree2.query(1, l2, r2, 0, lens - 1)) return false;return true;
}bool judge(int l, int r, int d){if(r - l + 1 == d) return true;int l2 = l + d, r2 = r;int len = r2 - l2 + 1;int l1 = l, r1 = l1 + len - 1;return equal(l1, r1, l2, r2);
}int main(){//input;tree1.mod = 1e9 + 7, tree1.p = 799817, tree1.getpowp();tree2.mod = 1e9 + 9, tree2.p = 451309, tree2.getpowp();scanf("%d%d%d",&n,&m,&k);scanf("%s",s);lens = (int)strlen(s);tree1.plant(1, 0, lens - 1), tree2.plant(1, 0, lens - 1);for(int i = 1; i <= m + k; i++){int ty, l, r;scanf("%d%d%d",&ty,&l,&r);l--, r--;if(ty == 1){char c[5];scanf("%s",c);tree1.update(1, l, r, 0, lens - 1, c[0]);tree2.update(1, l, r, 0, lens - 1, c[0]);}else{int d;scanf("%d",&d);printf("%s\n", judge(l, r, d) ? "YES" : "NO");}}return 0;
}

?

轉載于:https://www.cnblogs.com/qscqesze/p/4831870.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/374447.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/374447.shtml
英文地址,請注明出處:http://en.pswp.cn/news/374447.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python文字游戲源代碼求年紀_Python實現猜年齡游戲代碼實例

1. 在猜年齡的基礎上編寫登錄、注冊方法&#xff0c;并且把猜年齡游戲分函數處理&#xff0c;如 2. 登錄函數 3. 注冊函數 4. 猜年齡函數 5. 選擇獎品函數 代碼如下 import json real_age 18 prize_list [好迪洗發水, 綠箭俠, 小豬佩奇, 布娃娃, 再來一次!] import random us…

KVC 與 KVO

一、Key-Value Coding (KVC)鍵值編碼 KVC&#xff0c;即是指 NSKeyValueCoding&#xff0c;一個非正式的 Protocol&#xff0c;提供一種機制來間接訪問對象的屬性。KVO 就是基于 KVC 實現的關鍵技術之一。 一個對象擁有某些屬性。比如說&#xff0c;一個 Person 對象有一個 nam…

使用模擬的單元測試–測試技術5

我的最后一個博客是有關測試代碼方法的一系列博客中的第四篇&#xff0c;演示了如何創建使用存根對象隔離測試對象的單元測試。 今天的博客探討了有時被視為對立的技術&#xff1a;使用模擬對象進行單元測試。 同樣&#xff0c;我使用了從數據庫檢索地址的簡單方案&#xff1a;…

多線程中的volatile和偽共享

偽共享 false sharing&#xff0c;顧名思義&#xff0c;“偽共享”就是“其實不是共享”。那什么是“共享”&#xff1f;多CPU同時訪問同一塊內存區域就是“共享”&#xff0c;就會產生沖突&#xff0c;需要控制協議來協調訪問。會引起“共享”的最小內存區域大小就是一個cache…

C語言代碼規范(一)縮進與換行

一、縮進的空格數為4個。最好配置代碼編輯器將TAB鍵設置為空格替換&#xff0c;避免出現另一個編輯器打開時格式變亂的情況。 例如Notepad設置 KEIL設置 二、“{” 和 “}”各自獨占一行。 不規范例子&#xff1a; for(i 0; i < student_num; i) { if((score[i] > 0…

armv7 cortex a系列編程手冊_AWTK能為現代GUI編程帶來何種改變?

AWTK是一個伸縮性極強的嵌入式圖形框架&#xff0c;它的誕生會給GUI編程研發工程師帶來哪些改變&#xff1f;AWTK是一個伸縮性極強的嵌入式圖形框架&#xff0c;可在Cortex-M3這樣低端的單片機上運行&#xff0c;也可以在Cortex-A7/A8/A9等處理器&#xff0c;甚至DSP以及X86處理…

【轉】各種概念POJO、JAVABEAN、DAO、DTO、PO、VO、BO、SSH、EJB

POJO&#xff08;pure old java object&#xff09; 是普通java類&#xff0c;有一些private的參數作為對象的屬性&#xff0c;然后針對每一個參數定義get和set方法訪問的接口。我看到這個定義&#xff0c;心里就有個疑問了&#xff0c;這個POJO跟JavaBean的定義怎么就這么像&a…

為什么要編寫單元測試–測試技巧8

我對最近在“您應該測試什么”上的博客有很多反應&#xff0c;有些人出于各種原因同意我的想法&#xff0c;另一些人則認為建議某些類可能不需要單元測試是非常危險的。 已經處理了什么測試&#xff0c;今天的博客涉及為什么要編寫單元測試&#xff0c;而今天的示例代碼是基于一…

Git遷移 從SVN到Git

Migrating from SVN to Git 首先我們需要在Stach或者GitHub上新建一個Repository, 拿到它的URL。 接下來參照如下步驟 : At first we should create a new git repository at Stash and get the repository URL, and then follow below steps: 1. 切換到本地git工作目錄 chang…

C語言代碼規范(二)空格

一、逗號, 之后加空格 printf("error! score[%d] %d\n", i, score[i]); 二、分號; 之后加空格 for(i 0; i < student_num; i) 三、關系運算符<、<、>、>、、! 前后加空格 if( (score[i] > 0) && (score[i] < 100) ) 四、賦值運算符…

c++ 多重背包狀態轉移方程_動態規劃入門——詳解經典問題零一背包

本文始發于個人公眾號&#xff1a;TechFlow&#xff0c;原創不易&#xff0c;求個關注今天是周三算法與數據結構專題的第12篇文章&#xff0c;動態規劃之零一背包問題。在之前的文章當中&#xff0c;我們一起探討了二分、貪心、排序和搜索算法&#xff0c;今天我們來看另一個非…

Discuz! 的編碼規范

前言 本規范由編程原則組成&#xff0c;融合并提煉了開發人員長時間積累下來的成熟經驗&#xff0c;意在幫助形成良好一致的編程風格。適用范圍 如無特殊說明&#xff0c;以下規則要求完全適用于Discuz!項目&#xff0c;同時也可大部分適用于COMSENZ旗下其他PHP項目。標準化的重…

C語言代碼規范(三)if語句

一、整型變量與0比較 許多人為了一時之便&#xff0c;模仿布爾變量風格寫為如下代碼 if(value) {... }if(!value) {... } 應當用 或 ! 來與0比較 if(0 value) {... }if(0 ! value) {... } 二、當if內的語句是與常量進行比較時&#xff0c;常量為左值&#xff0c;變量為右…

6月24 面向對象的設計原則-----工廠模式和單列模式

工廠模式&#xff1a; 工廠模式就是專門負責將大量有共同接口的類實例化&#xff0c;而且不必事先知道每次是要實例化哪一個類的模式。它定義一個用于創建對象的接口&#xff0c;由子類決定實例化哪一個類。 工廠模式相當于創建實例對象的new&#xff0c;經常要根據類Class生成…

LeetCode Subsets

原題鏈接在這里&#xff1a;https://leetcode.com/problems/subsets/ 題目&#xff1a; Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order.The solution set must not contain duplicate su…

使用ThreadPoolExecutor并行化獨立的單線程任務

Java SE 5.0中引入的任務執行框架是簡化多線程應用程序的設計和開發的巨大飛躍。 該框架提供了用于管理任務概念&#xff0c;管理線程生命周期及其執行策略的工具。 在此博客文章中&#xff0c;我們將描述該框架的功能&#xff0c;靈活性和簡單性&#xff0c;以展示一個簡單的用…

python定義一個圓_Python-矩形和圓形

原博文 2019-11-11 12:34 ? Exercise 15.1. 定義一個叫做Circle 類&#xff0c;類的屬性是圓心 (center) 和半徑 (radius) , 其中&#xff0c;圓心 (center) 是一個 Point 類&#xff0c;而半徑 (radius) 是一個數字。 實例化一個圓心 (center) 為 (150, 100) &#xff0c;半…

C語言代碼規范(四)命名規則

一、宏定義全部字母大寫&#xff0c;單詞間下劃線間隔 #define FLASH_PAGE_SIZE 256 #define FLASH_SECTOR_SIZE (4 * 1024) #define FLASH_BLOCK_SIZE (64 * 1024) #define FLASH_SIZE (16 * 1024 * 1024) 二、const修飾的常量全部字母大寫&#xff0c;單詞間…

Forbidden You don't have permission to access / on this server PHP

Forbidden You dont have permission to access / on this server PHP 在新安裝的谷歌游覽器里&#xff0c;打不了PHP網站了&#xff0c;錯誤顯示&#xff1a; Forbidden You dont have permission to access / on this server. 原因還是配置權限問題 解決辦法&#xff1a; wa…

Spring 3.1和JPA的持久層

1.概述 本教程顯示了如何使用Hibernate作為持久性提供程序使用JPA設置Spring 。 有關使用基于Java的配置和項目的基本Maven pom設置Spring上下文的分步介紹&#xff0c;請參閱本文 。 2. Java的JPA Spring配置 要在Spring項目中使用JPA&#xff0c; 需要設置EntityManager 。…