Prefix sums(minimums, ...)


Plus
Pin


Problem description Progress
ID 30659. The amount on the line
Темы: Prefix sums(minimums, ...)   

Given an immutable array of length n and q queries like “calculate the sum of an array subsegment from l to r ”. Print the answer for each request.

Input
The first line contains a number n – array size (\(1 <= n <= 10^5\)). The second line contains n &ndash numbers ; array elements. Modulo numbers do not exceed \(10^9\). The third line contains the number q – number of requests (\(1 <= q <= 10^5\)). Followed by q lines, each of which contains 2 numbers: l and r (\(1 <= l <= r <= n\)).

Output
Print the answers to all queries, each on a separate line.
 

 

Examples
# Input Output
1
5
1 2 3 4 5
3
1 2
3 3
2 5
3
3
14

ID 29638. Quiet Don №3
Темы: Prefix sums(minimums, ...)   

Natalya Korshunova really misses Grigory Melekhov and wants to return to him. But, unfortunately, Grigory loves Aksinya, so Natalya decided to prove to her beloved that she is better than her.
To do this, Natalia went to Grigory and declared that she could solve any problem, whatever he suggested. Melekhov accepted the challenge.
 
Grigory gives Natalia an A array consisting of n non-negative integers. Then he asks her to perform q operations of the same type, consisting of the following: "Given the numbers l, r and k . Further, for each index i from l to r, the number k is substituted instead of the number A i and is considered a bitwise exclusive “or” all numbers in the segment \([l;r]\), after which the number Aith place again >i".
Thus, there are \(r – l + 1\) independent substitutions that do not change the array, and accordingly \(r – l + 1\) results in a bitwise exclusive “or”. Natalia needs to tell Grigory a bitwise exclusive “or” all substitution results (for a better understanding, check out the examples).
 
Help Natalia Korshunova cope with this task! Then Gregory will definitely return to her!
 
Input
The first line is an integer n (\(1 <= n <= 10^5\)) – number of array elements.
The second line contains n non-negative integers not exceeding \(10^8\).
The third line is an integer q (\(1 <= q <= 10^5\)) – number of requests.
The following contains q lines, each containing 3 integers: l, r, k (\(1 <= l <= r <= n\), \(0 <= k <= 10^8\)).
 
Output
You need to output q responses for each query on one line separated by a space.
 

 

Examples
# Input Output
1
5
1 2 3 4 5
2
1 3 7
4 5 10
7 1


Explanation
First request:
1) 7 ⊕ 2⊕ 3 = 6
2) 1 ⊕ 7⊕ 3 = 5
3) 1 ⊕ 2⊕ 7 = 4
6 ⊕ 5 ⊕ 4 = 7
Answer: 7.
 
Second request:
1) 10 ⊕ 5 = 15
2) 4 ⊕ 10 = 14
15 ⊕ 14 = 1
Answer: 1.
 

ID 29640. Quiet don №2
Темы: "Two Pointers"    Prefix sums(minimums, ...)   

Aksinya loves Gregory, but she is married to Stepan. She is unhappy with her husband, so the time she spends with him can be characterized by a negative indicator of Aksinya's happiness (\(a_i < 0\)), and the time that she spends with Gregory, a positive indicator of happiness (\(a_i > 0\)). It is known that Aksinya spends one day either with her husband or with her lover. 

Find the maximum total happiness for L days in which Aksinya will spend no more than C days with her husband.
 
Input
The first line contains 3 numbers: N – number of days, L and C (\(1 <= L, C <= N <= 1 000 000\)).
The second line contains N numbers a_i (\(1 <= |a_i| <= 1,000,000 000\)).

Input
You want to display the answer to the problem.
 

 

Examples
# Input Output
1 5 3 3
1 -1 2 -2 3
3

ID 29655. tree felling
Темы: Prefix sums(minimums, ...)   

Chubaty teaches Grigory Melekhov how to perform a Baklan strike with a saber. As targets, they use n trees in a row, numbered from 1 to n. Chubaty, estimated the strength of all trees by natural numbers, and wrote them down. For each tree that Melekhov was able to cut, he receives a number of points equal to the number written on the tree, and if he could not, he loses the same amount.

Chubaty asks Grigory to hit trees from l to r, in ascending order of their numbers. Melekhov recently hurt his shoulder, so he can successfully cut down a tree every other time, i.e. if he cut down a tree with number i, then he will not be able to cut down a tree with number i + 1, but will be able to cut down the tree with number i + 2 etc.

Chubat m once asked Grigory to perform blows, but he forgot what trees Melekhov could cut down. Help him determine how many points Gregory scored for each attempt.
 
Input
The first line contains 2 numbers n and m (\(1 <= n, m <= 100000 \))
The second line contains n numbers - the strength of all trees, where the strength of the tree i is written at position i.
The following m lines contain pairs of numbers l and r (\(1 < = l <= r <= n\)), meaning which piece of trees Chubaty asked to cut down.
 
Output
For each query print how many points Grigory earned in this attempt.
 

 

Examples
# Input Output
1
6 6
1 2 3 4 5 6
16
1 5
2 6
2 5
2 4
2 2
-3
3
4
-2
3
2

ID 30699. Gangs of Fomin No. 2
Темы: Prefix sums(minimums, ...)    Fermat's Little Theorem    Remains    Fast exponentiation   

Fomin's gang consists of n groups, each of which has ai people. q raids are planned. The i-th raid will include exactly one robber from each group whose number lies in the segment \([l_i, r_i]\).

Melekhov is sad, so for each raid he decided to calculate the number of possible units modulo \(10^9 + 7\). However, Gregory is constantly thinking about the meaning of life and searching for the truth, so he cannot concentrate on calculations and asks you for help.

Input
The first line contains the number n (\(1 <= n <= 10^5\)) – the number of groups in Fomin's gang.
The second line contains n natural numbers ai (\(1 <= a_i < = 10^6\)) – the number of people in the i-th group.
The third line contains the number q – number of raids.
The following are q lines, each containing two numbers – li and ri (\(1 <= l_i <= r_i <= n\)) – numbers of groups participating in i-th raid.

Imprint
Print q numbers, each on a separate line – response to the task.

 

Examples
# Input Output
1 6
1 3 7 1 4 100
3
1 3
34
26
21
7
8400

ID 30691. Gangs of Fomin
Темы: Prefix sums(minimums, ...)   

Fomin's gang consists of n groups, each of which has ai people. q raids are planned. The ith raid will have exactly one rogue from each group whose number lies in the segment \([l_i, r_i]\).  

Melekhov is sad, so for each raid he decided to calculate the number of possible units modulo \(10^9 + 7\). However, Gregory is constantly thinking about the meaning of life and searching for the truth, so he cannot concentrate on calculations and asks you for help.

Input
The first line is a number n (\(1 <= n <= 10^5\)) – the number of groups in Fomin's gang.
The second line contains n natural numbers ai (\(1 <= a_i <= 2\) ) – number of people in i-th group.
The third line contains the number q – number of raids.
The following are q lines, each containing two numbers – li and ri (\(1 <= l_i <= r_i <= n\)) – numbers of groups participating in the i-th raid.

Output
Output q numbers, each on a separate line – response to the task.

 

Examples
# Input Output
1
6
1 2 1 1 2 2
3
1 3
3 4
2 6
2
1
8

ID 33256. Cat Goose and random matrix
Темы: heap    Prefix sums(minimums, ...)   

Cat Goose prepared for Nick Fury a rectangular table a of size \(n \cdot m\) containing numbers from 0 code> to p−1. Nick Fury immediately realized that each number in this table was chosen randomly with equal probability from 0 to p−1 , regardless of the others.

Your task — find a rectangular submatrix of this table, in which the sum is divisible by p. Among all such submatrices, you need to find the one in which the sum of the elements is maximum.

Formally, you need to find \(1 <= i_1 <= i_2 <= n\), \( 1 <= j_1 <= j_2 <= m\) that the sum of ax,y over all \(i_1 <= x <= i_2\), \(j_1 <= y <= j_2\) split on p, and among these has the maximum amount.

Input
The first line contains three integers n, m, p (\(1 <= n m, p <= 1,000,000\)) — dimensions of the matrix and the number by which the sum of the submatrix should be divided.
The following n lines contain m integers, the j-th number in the i-th line equals ai,j (\(0 <= a_{i,j} <= p ? 1\) ).
It is guaranteed that each number in a is chosen independently at random, equiprobably from 0 to p−1.

Output
Print a single integer — the maximum sum of a rectangular submatrix where the sum is divisible by p. If there are none, print 0.

 

Examples
# Input Output
1
6 7 5
0 0 3 0 1 0 4
0 2 3 0 2 2 1
2 4 1 4 4 0 3
1 1 0 2 0 3 2
3 0 3 1 0 1 2
1 2 0 0 3 3 1
65

ID 38139. Enough green
Темы: Prefix sums(minimums, ...)   

Farmer John's pasture can be thought of as a  NxN grid (\(1<=N<=500\)) of square cells with grass (like a large Chess board). Due to the variability of the soil, the grass in some cells is greener than in others. Each cell (i,j) is described by an integer - the greenness level G(i,j), in the interval \ (1…200\).

Farmer John wants to take a photo of a rectangular sub-grid of his pasture. He wants the minimum of G in his photo to be sharp 100. Help him count how many different pictures he can take. The subgrid can range in size from the entire pasture to one cell. There are \(N^2(N+1)^2/4\) different sublattices, use a 64-bit integer (like < code>long long in C++).



Input
The first line contains N. Each of the following N lines contains N integers and together they describe the magnitudes G(i,j)  ;for pasture NхN .

Imprint
Output the number of different photographs that Farmer John can take, i.e. the number of rectangular sublattices in which the minimum level of "greenness" exactly 100.

Note that the answer requires a 64-bit integer variable of type long long in C++.

 
 
Examples
# Input Output
1 3
57 120 87
200 100 150
2 141 135
8

ID 38384. The choice of flowers for the bouquet
Темы: Binary search in an array    Prefix sums(minimums, ...)   

Volodya wants to beautifully congratulate his wife on their wedding anniversary and is going to give her a bouquet of n flowers.

Arriving at a flower shop, Volodya discovered that a bouquet can be made from flowers of m different types, and the number of flowers of each type is not limited. Volodya knows that the first flower of the i-th species in the bouquet makes his wife happier by ai, and each next flower  
this way she becomes happier on bi. That is, if in the bouquet xi > 0 flowers of type i, then flowers of this type make Volodya's wife happier by ai + (xi − 1) · bi.

Like any loving husband, Volodya wants to please his wife as much as possible. Therefore he wants to know by what maximum amount her happiness can increase from a bouquet, picked from the flowers available in the store.

Input data format
The first line contains two integers n and m (1 ≤ n ≤ 109 , 1 ≤ m ≤ 100 000) — required 
the number of flowers in the bouquet and the number of flowers available.
Each of the following m lines describes the i-th kind of flowers and contains two integers ai and bi (0 ≤ ai  , bi ≤ 109 ) — an increase in happiness from the first flower of the i-th species and an increase in happiness from each subsequent flower of this species.

Output data format
In a single line print a single number — the maximum increase in happiness that Volodya's wife can get from a bouquet of n flowers.
 

Examples
# Input Output
1 4 3
50
14
2 2
14
2 5 3
5 2
4 2
3 1
16

ID 38589. practice test
Темы: Arrays    Prefix sums(minimums, ...)    Dynamic Programming: One Parameter   

We have a grid with H rows and W columns. The square in the ith row and jth column will be called Square(i, j). Integers from 1 to H·W are written over the entire grid, and an integer written in Square(i, j) is equal to Ai,j.
You are a wizard and can teleport a shape placed on Square(i, j) to Square(x, y) by spending \(|x-i|+|y-j|\) magiks (magic coins).
Now you need to pass Q practice tests on your abilities as a wizard (sorceress). Ith test will be conducted as follows:
- initially the figure is located in the square, where the integer is written Li;
- Let x be the integer written in the square occupied by the shape. Repeatedly move the shape to the square where the integer x+D is written until x will not become equal to Ri. The test ends when x = Ri .
It is guaranteed that Ri- Li is divisible by D.
For each test, find the sum of magics consumed during that test.

Input
The first line contains three integers: H, W and D (\( 1\leq H,W \leq 300\), \(1 \leq D \leq H \cdot W\)).
The following H lines contain W numbers Ai,j (\(1 \leq A_{i,j} \leq H \cdot W\)\(A_{i,j} \ neq A_{x,y} ((i,j) \neq (x,y))\).
The next line contains an integer (\(1 \leq Q \leq 10^5\)).
The last Q lines contain 2 integers each: Li and Ri (\(1 \leq L_i \leq R_i \leq H \cdot W\)), \((R_i-L_i)\) multiple of D.

Imprint
For each test, print the sum of magics spent during this test. The output should be in the order of the tests.
 

 

Examples
# Input Output Explanations
1 3 3 2
1 4 3
2 5 7
8 9 6
1
48
5 - 4 is written as Square(1,2).
- 6 is in Square(3,3).
- 8 is written in Square (3,1).

Thus, the sum of magic points spent during one test is:
 \((|3-1|+|3-2|)+(|3-3|+|1-3|)=5\).
 
2 4 2 3
37
14
5 2
68
2
2 2
2 2
0
0
Note that there may be a test where the shape doesn't move at all, and there may be multiple identical tests.
3 5 5 4
13 25 7 15 17
16 22 20 2 9
14 11 12 1 19
10 6 23 8 18
3 21 5 24 4
3
13 13
2 10
13 13
0
5
0
 

 

ID 39110. Substring length
Темы: Dynamic Programming: One Parameter    Prefix sums(minimums, ...)   

You have the string s. You want to make a new line, writing each letter in it a number of times equal to the serial number of this letter in the alphabet. For example, s = "abcdc", newline s_new = "abbcccddddccc".  We were wondering what the length of the string will be if you write out all the characters of the original string, starting with the character l and ending with the character r. Total we k requests to you.


Input
In the first line, the program receives as input two numbers n and k (1<=n<=106, 1< ;=k<=106), where n is the string length, k is the number of requests. The second line contains the string s of length n. The next k lines contain the boundaries of the segments l and r (1 <= l <= r <= n). < code>l, r - serial numbers of characters in a string, starting from 1.

Imprint
For each query print the length of the string you got. One number per line. Total k lines.
 

Examples
# Input Output
1 5 3
abcd
15
23
3 5
13
5
10

ID 39356. Get OK
Темы: Strings    Prefix sums(minimums, ...)   

Given a string S of length N, consisting of the characters S, T, O and K. Answer requests like the following.
Query i(1 <= i <= Q): for given integers li and r(1 <= li< ri <= N),  the substring S is considered, starting at index li and ending at index ri < /sub> (both inclusive). You need to determine how many times OK  occurs as a substring in this string?
 

Explanation
A substring of a string is a string obtained by removing zero or more characters from the beginning and end of the string of the original string. For example, for the string KOTS, the substrings would be the strings KO, KOT , OT, OTS, (empty string) and others, but not OK.


Input
The first line contains two numbers N (2<=N<=105) and Q (1<=Q<=105). The second line contains a string S of length N. Each character in the string is S, T, O or K. Next come Q lines of 2 numbers each: li and r< /code>(1 <= li< ri <= N).

Imprint
Print Q lines. The Ith line should contain the response to the ith request.
 
Examples
# Input Output
1 8 3
OKOKTOKS
37
23
18
2
0
3

ID 39381. Harmonious sequence
Темы: Binary search    Prefix sums(minimums, ...)   

A series of lectures at the University of Flatland is devoted to the study of sequences.

The professor calls a sequence of integers \(a_1, a_2, ..., a_n\) harmonious if every number except \(a_1\) and \(a_n\), equals the sum of adjacent:  \(a_2 = a_1 + a_3, a_3=a_2+a_4, ..., a_{n-1}=a_{n-2}+a_n\). For example, the sequence [1,2,1,–1]  is harmonic because 2=1+1, and 1=2+(–1) .

Consider sequences of equal length: \(A=[a_1,a_2, ... a_n]\)   and \(B=[b_1,b_2, ... b_n]\). The distance between these sequences will be called the value \(d(A,B)= |a_1-b_1|+|a_2-b_2|+...+|a_n-b_n|\)  . For example, \(d([1,2,1,–1][1,2,0,0])=|1–1|+|2–2| ++|1–0|+|–1–0|=0+0+1+1=2 \)

At the end of the lecture, the professor wrote on the blackboard a sequence of n integers \(B=[b_1,b_2, ... b_n]\)and asked the students to find harmonious sequence \(A=[a_1,a_2, ... a_n]\) such that \(d( A, B)\) is minimal. To make it easier for himself to check, the professor asks you to write as an answer only the desired minimum distance \(d(A,B)\) .

It is required to write a program that, given a sequence B, determines at what minimum distance from sequence B there is a harmonic sequence A.

Input
The first line of the input file contains the integer n – the number of elements in the sequence ( \(3 \le n \le 300 000\)).

The second line contains n integers \(b_1, b_2, …, b_n (–10^9 \le b_i \le 10^9 )\) .< br />
Imprint
The output file must contain a single integer: the minimum possible distance from the sequence in the input file to a harmonic sequence.

Examples
# Input Output
1 4
1 2 0 0
2

ID 39482. Subsequence of length L
Темы: "Two Pointers"    Prefix sums(minimums, ...)   

Given a sequence of N numbers. All its continuous subsequences are considered, in which the number of negative numbers does not exceed C. Find among them a subsequence with the maximum sum, length L. In your answer, indicate the sum of the found subsequence.

Input
The first line contains 3 numbers: the number of numbers N (1 <= N <= 1 000 000),  L and C< /code> (\(1 <= L, C <= N <= 1,000,000\)). Each of the following N lines contain one number, modulo not exceeding 1000.

An example of the organization of source data in the input file (for L = 3 and C = 3):
5 3 3
1
-1
2
-2
3


Several sequences can be selected in this set, but with a maximum sum of 3 it will be: 2+(-2)+3. 
Answer(for С = 3 and L = 3): 3.  ;

 

ID 39574. no time to paint
Темы: Stack    Prefix sums(minimums, ...)   

Besi recently received a set of paints and she wants to paint a long hedge on one side of her pasture. The fence consists of N consecutive 1-meter segments (1≤N≤105). Besi has paints in 26 different colors, which she labeled with letters from 'A' to Z' in ascending order of darkness ('A' is the lightest color, 'Z' is the darkest). Therefore, it can describe the coloring of the hedge as a string of N characters (where each character is one of - from 'A' to Z').
Initially, all segments of the fence are not painted. Besi can paint any continuous range of segments with a single color with a single brush stroke, and she never paints a lighter over a darker one (she can paint a darker over a lighter one).

For example, she can color an initially unpainted segment of length 4 like this:

.... -> BBB. -> BBLL-> BQQL
Limited in time, Besi may leave some consecutive segments unpainted. She is now looking at Q line segment candidates (1≤Q≤105), each given by two integers (a,b) (1≤a≤b≤N) indicating the indexes of the segment endpoints that should stay unpainted.

For each candidate, specify the minimum number of touches required to color all segments outside that range with the desired color, leaving segments inside the range uncolored. Note that Besi doesn't actually color anything, so the answers for each candidate range are independent.

Input
The first line contains N and Q.
The next line contains N representing the desired color for each hedge segment.

Each of the next Q lines contains two space-separated integers a and b representing a range of segments that may not be colored.

Imprint
For each of the Q candidates print the answer on a new line.

Examples
# Input Output Explanation
1
8 2
ABBAABCB
3 6
14
4
3
In this example, the range exclusion matches the desired pattern. In this example, the BAAB range exclusion requires four touches to colorize, while the ABBA range exclusion requires only three.

.... -> AA.. -> ABBB-> ABCB

ID 39671. Substrings in a string
Темы: hash    Prefix sums(minimums, ...)   

Given a string S = s1s2...sn and a set of queries like (l1, r 1, l2, r2). For each query, it is required to answer whether the substrings sl1...sr1 and sl2...sr2 are equal .


Input:
The first line contains the string S (1 <= |S| <= 105) consisting of lowercase Latin letters. 
The second line contains a natural number q (1 <= q <= 105) - the number of requests.
The next q lines contain 4 natural numbers each - l1, r1, l2, r2 ( 1 <= l1 <= r1 <= |S|, 1 <=l2 <= r< sub>2 <= |S|).

Output:
For each query, print '+' if the substrings are equal, and '-' otherwise.

Examples:
 

Input Output
abacaba
4
1 1 7 7
1 3 5 7
3 4 4 5
1 7 1 7
++-+

ID 39672. Tom Sawyer and the word on the fence
Темы: hash    Greedy Algorithm    Prefix sums(minimums, ...)   

While painting the fence, Tom Sawyer wrote the word s on it. However, he then decided that palindrome words looked prettier.
Now he wants to add another word g to the given word s on the right so that the resulting word sg is a palindrome. However, in order to save paint, the length g should be as short as possible.
Help Tom Sawyer identify the word g.

Input:
The first line contains the word s (1 <= |s| <= 200000) consisting of lowercase Latin letters.

Output:
Print the minimum possible length of the word g that needs to be completed so that the word sg on the fence becomes a palindrome. If you don't need to add anything, then print '-'.

Examples:
 

Input Output
abc ba
a -

ID 39673. Reading aloud
Темы: hash    Prefix sums(minimums, ...)    Binary search    Binary search by answer   

Tom Sawyer and Huckleberry Finn read a newspaper clipping aloud together. But it so happened that Tom Sawyer began to read from the i-th character, and Huckleberry Finn from the j-th. 
How many letters can they read before they find they started from different places, or until they both read to the end?

Input:
The first line contains the string S (1 <= |S| <= 105), consisting of lowercase Latin letters - an inscription from a newspaper clipping.
The next line contains a natural number q - the number of requests.
The next q lines contain two natural numbers i and j each - the positions from which Tom Sawyer and Huckleberry Finn start reading, respectively.

Output:
Print q lines, each of which should contain one integer - the number of characters that match when reading substrings starting with the i-th and j-th characters.

Examples:
 

Input Output
abacaba
4
15
3 5
4 2
26
3
1
0
2

ID 39674. Huckleberry Finn and two strings
Темы: hash    Prefix sums(minimums, ...)    Bust   

Huckleberry Finn has two strings s and t of the same length n.
Huckleberry Finn likes strings to have the same prefixes (beginnings), so he can swap two characters in string s to make the common prefix of strings s and t larger.
However, this trick is rather tedious, so Huckleberry Finn will either not do it at all, or will do it exactly once.

Help Huckleberry Finn determine the longest common prefix length of strings s and t that he can get.


Input:
The first line contains a natural number n (1 <= n <= 200000) - the length of strings s and t
The second line contains a string s, consisting of lowercase Latin letters.
The third line contains a string t consisting of lowercase Latin letters.

Output:
Print one natural number - the maximum length of the common prefix s and t, which can be obtained by exchanging two characters in the string s at most once.

Examples:
 

Input Output
3
wai
add
1
5
qdyid
xreac
0

ID 39675. cultural contact
Темы: hash    Prefix sums(minimums, ...)    Bust   

At the beginning of the 18th century, a group of European explorers arrived on an island inhabited by a group of tribes that had never come into contact with representatives of European civilization.

To successfully establish contacts with the natives, the leader of the group plans to give a gift to the leader of each tribe he meets. To this end, he brought a long chain of glass, similar to precious stones. 
Let's represent the string as a string s, consisting of small letters of the English alphabet, where each letter means the type of piece of glass at the corresponding position. 
The researchers are going to cut the chain into some fragments, and then hand over exactly one fragment to the leader of each tribe encountered by the group. The research leader decided to split the chain into fragments according to the following rules:

  • In order not to spend a lot of time on cutting, each fragment should be a group of adjacent pieces of glass in the chain, that is, a substring of the string s.
  • All pieces of glass must be used, that is, each piece of glass must be included in exactly one fragment.
  • Because the researchers don't know how the aborigines will value certain types of glass, they want each chief to get the same set of glass without regard to order. In other words, for any type of glass, the number of glass of this type must be the same in each of the fragments.
  • Researchers don't know how many tribes live on the island, so the number of prepared fragments should be as large as possible.

Help the manager determine the maximum number of fragments that can be obtained.

Input:
The first line contains the string s (1 <= |s| <= 5000000).

Output:
Print a single number - the maximum possible number of fragments into which the researchers can cut the chain they have without violating any of the conditions formulated by the group leader.

Examples:
 
Input Output
abbabbbab 3
aabb 1

Explanations:
In the first example, researchers can break the chain 'abbabbbab' fragments 'abb', 'abb', 'bab', then the leader of each tribe they meet will get one glass of type 'a' and two pieces of 'b'.

In the second example, the string cannot be divided into more than one fragment of the chain, observing all the conditions.

ID 39964. Maximum Questions
Темы: Dynamic programming    Prefix sums(minimums, ...)   

Max had two strings s of length n and t of length m written in her notebook, consisting of the letters "a"; and "b" Latin alphabet. Moreover, Max knows that the string t has the form «abab...», that is, the letter «a» is on the odd positions of the string, and the letter — "b".

Suddenly, in the morning, Max discovered that someone had messed up her string s. Some of the s's have been changed to "?".

Let's call the sequence of positions i, i + 1, ..., i + m - 1 an occurrence of string t in s, if 1 ≤ i ≤  n - m + 1 and t1 = si, t2&thinsp ;= si + 1, ..., tm = si +  m - 1.

The beauty of string s is measured as the maximum number of non-overlapping occurrences of string t in it. Max can replace some of the "?" on "a" or "b" (characters in different positions can be replaced by different letters). Max wants to make substitutions so that the beauty of the string s is as large as possible. Of all these options, she wants to replace as few "?" characters as possible. Find out how many substitutions she has to make.

Input:
The first line contains a single integer n (1 ≤ n ≤ 105) — string length s.
The second line of the input contains a string s of length n, consisting only of the letters "a"; and "b" the Latin alphabet, as well as the symbols «?».
The third line contains the integer m (1 ≤ m ≤ 105) — string length t. The string t itself contains "a"; in odd positions, and "b" on even numbers.

Output:
Print a single integer — the minimum number of substitutions that Vasya must make in order to make the beauty of the string s the maximum possible.

Examples:
 

Input Output
5
bb?a?
1
2
9
ab??ab???
3
2

Explanations:
In the first example, the string t is of the form "a". The only optimal option — replace all characters "?" to «a».
In the second example, using two replacements, you can get the string "aba?aba??". You cannot get more than two entries.

ID 41228. XOR and favorite number
Темы: Mo algorithm    Prefix sums(minimums, ...)   

Evan has a favorite number k and an array ai of length n. Now it asks you to answer m requests.

For each query given by a pair of numbers l and r, it is required to find the number of pairs of integers i and j such that l ≤ i ≤ j ≤ r and xor of numbers ai , ai + 1, ..., aj is k.

Input:
The first line contains integers n, m and k (1 ≤ n, m ≤ 105, 0 ≤ k ≤  106) — the length of the array, the number of requests, and Evan's favorite number, respectively.
The second line contains n integers ai (0 ≤ ai ≤ 106) — Evan's array.
Then there are m lines. The i-th line contains numbers li and ri (1 ≤ li ≤ r< sub>i ≤ n) defining the i-th query.

Output:
Print m lines, the answers to the questions in the order they appear in the input.

Examples:
 

Input Output
6 2 3
1 2 1 1 0 3
16
3 5
7
0
5 3 1
1 1 1 1 1
15
24
1 3
9
4
4

ID 43124. Binary sort
Темы: Prefix sums(minimums, ...)   

You have decided to visit your friend from Ozersk. However, at the entrance to the city, you were stopped and asked to solve a problem to make sure that you can actually drive into the territory of the closed city.
A binary string is a string consisting only of the characters 0 and 1. The police officer gave you the binary string s1s2 ... sn. You need to sort this string (that is, turn it into a string like  00 ... 0011 ... 11) in the least number of operations. In one operation, you can do the following:
x Pick an arbitrary index in row 1 <= i <= n;
x For all j >= i, change the value in the jth position to the opposite, that is, if sj = 1, then make sj = 0, and vice versa.

Input
Each test consists of several sets of input data. The first line contains an integer t (1 <= t <= 104) the number of test cases. The following is a description of the input datasets.
The first line of each test case contains a single integer n (1 <= n <= 105) the length of the string.
The second line of each test case contains a binary string s of length n.
It is guaranteed that the sum of n over all test cases does not exceed 2 ·105.

Imprint
For each test case, print a single integer, the minimum number of operations that need to be done to sort the string.
 

Examples
# Input Output
1 6
1
1
2
10
3
101
4
1100
5
11001
6
100010
0
1
2
1
2
3


Remark
In the first test case, the string is already sorted.
In the second test case, you can choose i = 1 and then s = 01.
In the third test case, you can choose i = 1 and get s = 010, and then choose i = 2. As a result, we get s = 001, that is, a sorted string.
In the sixth test case, you can choose i = 5 at the first iteration and get  s = 100001. Then choose i = 2, then s = 111110. Then choose i = 1, getting the
tagged string s = 000001.

ID 43720. Darts
Темы: Prefix sums(minimums, ...)   

Petya and Vanya decided to come up with their own rules for playing Darts. They took a round playing field and divided it into sectors. Sectors are numbered with natural numbers. Each sector was assigned the number of points that can be obtained if you hit it with a dart. Before the start of the game, a zero sector is selected, which serves as a reference point: the number of points that a player scores when entering a sector is calculated as the number of points indicated in the sector multiplied by the distance (number of sectors) from the zero sector. When hitting the zero sector, the number of points is equal to zero.

Since it is not known in advance which sectors the players will fall into, the zero sector should be chosen so that the player scores the maximum number of points if he hits all sectors of the playing field once.

Determine which sector you want to choose zero.


Input
The first line of the input contains the number N — the number of sectors into which the playing field is divided.
The following N lines each contain a single number — the number of points that players gain by getting into the corresponding sector, starting from sector number 1.

Imprint
Print one number — sector number to be selected as zero.
 

Examples
# Input Output Note
1 6
8
20
5
13
7
19
3 For this example, — 3 (5 * 0 + 20 * 1 + 13 * 1 + 8 * 2 + 7 * 2 + 19 * 3 = 120)

ID 43721. Two garbage trucks
Темы: Prefix sums(minimums, ...)   

Garbage containers are installed at every kilometer of the two-way ring road. The length of the ring road is N kilometers. The zero kilometer and the Nth kilometer of the road are at the same point. The amount of garbage that accumulates daily in each of the containers is known.
There are two garbage trucks on the road. One goes clockwise, the other against. Both garbage trucks leave the recycling center at the same time towards each other and meet near one of the containers. Only one garbage truck can take out garbage from one container. The garbage delivery time is calculated as the product of the amount of garbage by the distance from the point to the recycling center. The waste recycling center was opened in one of the garbage collection points in such a way that the total time for garbage collection by two garbage trucks was minimal.
Determine which container number you need to place a recycling center in order to minimize the time for garbage trucks to collect garbage.

Input
The first line of the input contains number N (1 <= N <= 10,000,000) – the number of garbage collection points on the ring road. Each of the following N lines contains a number of – the amount of garbage in the container (all numbers are natural, the amount of garbage in each point does not exceed 1000). The numbers are given in order of containers on the motorway, starting from the first kilometer.

Imprint
Display one number on the screen - the answer to the problem.
 

Examples
# Input Output Explanation
1 7
8
20
5
13
7
19
21
7 With such initial data, it is necessary to open a recycling center near container number 7:
The first garbage truck collects garbage: 0 * 21 + 1 * 19 + 2 * 7 + 3 * 13 = 72
The second garbage truck will spend time: 0 * 21 + 8 * 1 + 20 * 2 + 3 * 5 = 63
Total time spent by two garbage trucks: 72

 
 

ID 43722. Continuous subsequence
Темы: Prefix sums(minimums, ...)   

Given a sequence of N natural numbers. It is known that the sum of all numbers in the sequence does not exceed 109. All its continuous subsequences are considered, in which the number of odd numbers is a multiple of K = 7. Find the largest sum of such a subsequence. 

Input
The first line of the input contains a single number N (1 <= N <= 1 000 000) -  number of numbers. Each of the following N lines contains one natural number not exceeding 1000.
 
Input
Print the answer to the problem

 
An example of the organization of source data in the input file (for К=4):
6
8
17
3
13
11
21


In this set, you can select the sequences 8+17+3+13+11 (sum 52) and 3+13+11+21 (sum 48). 
Answer(for K = 4): 52