codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1555D.md (2109B)


      1 Source: https://codeforces.com/problemset/problem/1555/D
      2 
      3 D. Say No to Palindromes
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not.
     22 
     23 Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase).
     24 
     25 You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase).
     26 
     27 You have to answer $m$ queries — calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive.
     28 
     29 Input
     30 
     31 The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of the string $s$ and the number of queries.
     32 
     33 The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters.
     34 
     35 The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) — parameters of the $i$-th query.
     36 
     37 Output
     38 
     39 For each query, print a single integer — the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive.
     40 
     41 Example
     42 
     43 Input
     44 
     45 ```
     46 5 4
     47 baacb
     48 1 3
     49 1 5
     50 4 5
     51 2 3
     52 ```
     53 
     54 Output
     55 
     56 ```
     57 1
     58 2
     59 0
     60 1
     61 ```
     62 
     63 Note
     64 
     65 Consider the queries of the example test.
     66 
     67 * in the first query, the substring is baa, which can be changed to bac in one operation;
     68 * in the second query, the substring is baacb, which can be changed to cbacb in two operations;
     69 * in the third query, the substring is cb, which can be left unchanged;
     70 * in the fourth query, the substring is aa, which can be changed to ba in one operation.