CF1098F.Ж-function

NOI/NOI+/CTSC

通过率:0%

时间限制:6.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The length of the longest common prefix of two strings s=s1s2…sns=s_1 s_2 \ldots s_n and t=t1t2…tmt = t_1 t_2 \ldots t_m is defined as the maximum k≤min⁡(n,m)k \le \min(n, m) such that s1s2…sks_1 s_2 \ldots s_k equals t1t2…tkt_1 t_2 \ldots t_k. Let's denote the longest common prefix of two strings ss and tt as lcp(s,t)lcp(s,t).

Z-function of a string s1s2…sns_1 s_2 \dots s_n is a sequence of integers z1,z2,…,znz_1, z_2, \ldots, z_n, where zi=lcp(s1s2…sn,  sisi+1…sn)z_i = lcp(s_1 s_2 \ldots s_n,\ \ s_i s_{i+1} \dots s_n). Ж-function of a string ss is defined as z1+z2+…+znz_1 + z_2 + \ldots + z_n.

You're given a string s=s1s2…sns=s_1 s_2 \ldots s_n and qq queries. Each query is described by two integers lil_i and rir_i, where 1≤li≤ri≤n1 \le l_i \le r_i \le n. The answer for the query is defined as Ж-function of the string slisli+1…sris_{l_i} s_{l_i +1} \ldots s_{r_i}.

两个字符串 s=s1s2…sns=s_1 s_2 \ldots s_n 与 t=t1t2…tmt = t_1 t_2 \ldots t_m 的最长公共前缀长度定义为最大的整数 k≤min⁡(n,m)k \le \min(n, m),使得 s1s2…sk=t1t2…tks_1 s_2 \ldots s_k = t_1 t_2 \ldots t_k。我们记字符串 ss 与 tt 的最长公共前缀为 lcp(s,t)lcp(s,t)。

字符串 s1s2…sns_1 s_2 \dots s_n 的 Z-函数 是一个整数序列 z1,z2,…,znz_1, z_2, \ldots, z_n,其中 zi=lcp(s1s2…sn,  sisi+1…sn)z_i = lcp(s_1 s_2 \ldots s_n,\ \ s_i s_{i+1} \dots s_n)。字符串 ss 的 Ж-函数 定义为 z1+z2+…+znz_1 + z_2 + \ldots + z_n。

给定一个字符串 s=s1s2…sns=s_1 s_2 \ldots s_n 和 qq 个查询。每个查询由两个整数 lil_i 和 rir_i 描述,满足 1≤li≤ri≤n1 \le l_i \le r_i \le n。该查询的答案定义为子串 slisli+1…sris_{l_i} s_{l_i +1} \ldots s_{r_i} 的 Ж-函数值。

输入格式

The first line contains the string ss, consisting of lowercase English letters (1≤∣s∣≤200 0001 \leq |s| \leq 200\,000). The second line contains one integer qq — the number of queries (1≤q≤200 0001 \leq q \leq 200\,000). Each of the following qq lines contains two integers lil_i and rir_i, describing the query (1≤li≤ri≤∣s∣1 \leq l_i \leq r_i \leq |s|).

第一行包含字符串 ss,由小写英文字母组成(1≤∣s∣≤200 0001 \leq |s| \leq 200\,000)。第二行包含一个整数 qq —— 查询次数(1≤q≤200 0001 \leq q \leq 200\,000)。接下来的 qq 行中,每行包含两个整数 lil_i 和 rir_i,描述一次查询(1≤li≤ri≤∣s∣1 \leq l_i \leq r_i \leq |s|)。

输出格式

For every query output one integer: the value of Ж-function of the corresponding substring.

对于每个查询,输出一个整数:对应子串的 Ж 函数值。

输入输出样例

  • 输入#1

    abbd
    4
    2 3
    1 3
    3 3
    1 4

    输出#1

    3
    3
    1
    4
  • 输入#2

    bbaaa
    5
    2 4
    1 5
    1 5
    3 3
    1 2

    输出#2

    3
    6
    6
    1
    3

说明/提示

In the first sample case there are four queries:

  • the first query corresponds to the substring bb, and its Ж-function equals 2+1=32 + 1 = 3;
  • the second query corresponds to the substring abb, and its Ж-function equals 3+0+0=33 + 0 + 0 = 3;
  • the third query corresponds to the substring b, and its Ж-function equals 11.
  • the fourth query corresponds to the substring abdd, and its Ж-function equals 4+0+0+0=44 + 0 + 0 + 0= 4.

在第一个样例中,共有四个查询:

  • 第一个查询对应子串 bb,其 Ж-函数值为 2+1=32 + 1 = 3;
  • 第二个查询对应子串 abb,其 Ж-函数值为 3+0+0=33 + 0 + 0 = 3;
  • 第三个查询对应子串 b,其 Ж-函数值为 11;
  • 第四个查询对应子串 abdd,其 Ж-函数值为 4+0+0+0=44 + 0 + 0 + 0 = 4。

输入解题思路,AI测评打分。不知道怎么写?

首页