Chef & Chutneys

3.9

16 votes
Advanced Data Structures, Segment Trees, Gaussian Elimination, Data Structures, Linear Algebra, Math
Problem

A chef has recently opened a new restaurant to serve his special dishes. He has a list of ingredients from which he makes them. Each ingredient is denoted by a unique lowercase letter. Thus, a dish is represented as a string of ingredients possibly with repititions. There are N people in his restaurant currently each of whom have ordered some dished.

You are given a collection of dishes with duplicates possibly. The chef can serve one of his favorite dishes from them if he is able to reorder all their ingredients (i.e. a permutation of the concatenation of all the strings) such that they form a palindrome. You have to facilitate the day-to-day working of the restaurant. There are two types of events happening in the restaurant:

  1. A person updates his order to get a new kind of dish.
  2. People in the range [L,R] wants to know the number of non-empty subsets of A[LR] from which the Chef can make one of his favorite dish.

You are given the initial orders of the people. Therefore, you have to process Q events of the mentioned types. 

Input format

  • First Line: Two integers N,Q denoting the number of people currently in the restaurant and the numbers events happening over time

  • Second Line: N space seperated strings consisting only of lowercase letters, A1,A2,A3,,AN where Ai denotes the chutney initially ordered by the ith person
  • Next Q Lines: Event of any of the above two type in the following format:
    1. 1 i X: The ith person has updated his order to a new chutney X: A[i]=X
    2. 2 L R: Count the number of subsets satisfying the constraint described above.

Output format

Print the required answer for each event of the 2nd type.

Constraints

1N2105

1iN,1LRN

1Q5104

1|Ai|,|X|106

There is at least one event of 2nd type.

 

Time Limit: 4
Memory Limit: 256
Source Limit:
Explanation

For the first 3 events, answers are as follows:

  1. We have only 1 subset from the first 4 chutneys from which Chef can prepare one of his favorite chutney: {tamarind,red,mint}tamrindednirmat.
    Note: There might by many possible favorite chutneys Chef can prepare from a specific collection of chutneys but people are only interested to know if he can prepare one of them.
  2. There is no subset from A[310] from which Chef can prepare any of his favorite chutney.
  3. Indices from which favorite chutney can be prepared: {1,2,3},{1,3,4,5,6,7}.
Editor Image

?