QOJ.ac

QOJ

Time Limit: 1.5 s Memory Limit: 256 MB Total points: 100

# 11564. Simple Subsequence

Statistics

We call an array of integers $d_1, d_2, \ldots, d_m$ good if its length is equal to $0$, or for any $1\le i\le m$, both values $\sum\limits_{j=1}^{i} d_j$ and $\sum\limits_{j=i}^{m} d_j$ are non-negative. Here, $\sum\limits_{j=l}^{r} d_j$ denotes $d_l+d_{l+1}+\ldots+d_{r}$.

We define the beauty of the array as the length of its longest good subsequence$^1$.

You are given an array $a$ of length $n$, which consists of numbers $-1$ and $1$.

You need to perform $q$ queries. The queries are of two types:

  1. replace the element $a_p$ with $-a_p$, where $p$~--- the parameter of the query;
  2. find the beauty of the array consisting of elements $[a_{l},a_{l+1},\ldots,a_r]$, where $(l, r)$~--- the parameters of the query.

Input

In the first line, two integers $n$, $q$ are given $(1\le n, q\le 5 \cdot 10^5)$~--- the length of the array $a$ and the number of queries, respectively.

In the second line, $n$ integers $a_1, a_2, \ldots, a_n$ $(a_i \in \{-1, 1\})$ are given~--- the elements of the array $a$.

In the next $q$ lines, the description of the queries is given. The first of the numbers $type_i$ $(1 \le type_i \le 2)$ denotes the type of the query. Queries of the first type are given in the format 1 p $(1 \le p \le n)$, and queries of the second type are given in the format 2 l r $(1 \le l \le r \le n)$.

Output

For each query of the second type, output one integer in a separate line~--- the beauty of the corresponding array.

Note

$^1$ An array $c$ is called a subsequence of an array $b$ if it is possible to remove a certain number of elements from the array $b$ (possibly zero) so that the array $c$ is formed. The empty array is a subsequence of any array.

Example

Input

5 4
1 1 1 -1 1
2 1 5
1 3
2 1 4
2 2 5

Output

5
2
3

Input

4 4
1 1 1 -1
2 1 2
2 2 4
2 3 3
2 3 4

Output

2
2
1
1

Scoring

  1. ($2$ points): $a_i = (-1)^{i+1}$ for $1 \le i \le n$, and there are no type one queries;
  2. ($7$ points): $n \le 16$, and there are no type one queries;
  3. ($21$ points): $n, q \le 100$;
  4. ($20$ points): $n, q \le 3000$;
  5. ($27$ points): $n, q \leq 2 \cdot 10^5$, and there are no type one queries;
  6. ($14$ points): $n, q \leq 2 \cdot 10^5$;
  7. ($9$ points): no additional restrictions.

Назвемо масив цілих чисел $d_1, d_2, \ldots, d_m$ хорошим, якщо його довжина рівна $0$, або для будь-якого $1\le i\le m$ обидва значення $\sum\limits_{j=1}^{i} d_j$ та $\sum\limits_{j=i}^{m} d_j$ є невід'ємними. Тут $\sum\limits_{j=l}^{r} d_j$ позначає $d_l+d_{l+1}+\ldots+d_{r}$.

Визначимо красу масиву як довжину найдовшої його хорошої підпослідовності$^1$.

Задано масив $a$ довжини $n$, що складається з чисел $-1$ та $1$.

Необхідно виконати $q$ запитів. Запити бувають двох типів:

  1. замінити елемент $a_p$ на $-a_p$, де $p$~--- параметр запиту;
  2. знайти красу масиву, що складається з елементів $[a_{l},a_{l+1},\ldots,a_r]$, де $(l, r)$~--- параметри запиту.

Вхідні дані

У першому рядку задано два цілі числа $n$, $q$ $(1\le n, q\le 5 \cdot 10^5)$~--- довжина масиву $a$ та кількість запитів відповідно.

У другому рядку задано $n$ цілих чисел $a_1, a_2, \ldots, a_n$ $(a_i \in \{-1, 1\})$~--- елементи масиву $a$.

У наступних $q$ рядках задано опис запитів. Перше з чисел $type_i$ $(1 \le type_i \le 2)$ позначає тип запиту. Запити першого типу задані у форматі 1 p $(1 \le p \le n)$, а запити другого типу задані у форматі 2 l r $(1 \le l \le r \le n)$.

Вихідні дані

Для кожного запиту другого типу в окремому рядку виведіть одне ціле число~--- красу відповідного масиву.

Примітка

$^1$ Масив $c$ називається підпослідовністю масиву $b$, якщо можливо видалити певну кількість елементів з масиву $b$ (можливо, нульову) так, щоб утворився масив $c$. Порожній масив є підпослідовністю будь-якого масиву.

Приклади

Вхідні дані

5 4
1 1 1 -1 1
2 1 5
1 3
2 1 4
2 2 5

Відповідь

5
2
3

Вхідні дані

4 4
1 1 1 -1
2 1 2
2 2 4
2 3 3
2 3 4

Відповідь

2
2
1
1

Оцінювання

  1. ($2$ бали): $a_i = (-1)^{i+1}$ для $1 \le i \le n$, а також немає запитів першого типу;
  2. ($7$ балів): $n \le 16$, а також немає запитів першого типу;
  3. ($21$ бал): $n, q \le 100$;
  4. ($20$ балів): $n, q \le 3000$;
  5. ($27$ балів): $n, q \leq 2 \cdot 10^5$, а також немає запитів першого типу;
  6. ($14$ балів): $n, q \leq 2 \cdot 10^5$;
  7. ($9$ балів): без додаткових обмежень.

我们称一个整数数组 $d_1, d_2, \ldots, d_m$ 为 good,当且仅当它的长度等于 $0$,或者对于任意的 $1\le i\le m$,两个值 $\sum\limits_{j=1}^{i} d_j$ 和 $\sum\limits_{j=i}^{m} d_j$ 都是非负的。这里,$\sum\limits_{j=l}^{r} d_j$ 表示 $d_l+d_{l+1}+\ldots+d_{r}$。

我们定义数组的 beauty 为其最长 good 子序列的长度$^1$。

给定一个长度为 $n$ 的数组 $a$,其元素 仅由 $-1$ 和 $1$ 组成

你需要处理 $q$ 个查询。查询有两种类型:

  1. 将元素 $a_p$ 替换为 $-a_p$,其中 $p$ 是查询的参数;
  2. 查询由元素 $[a_{l},a_{l+1},\ldots,a_r]$ 组成的数组的 beauty,其中 $(l, r)$ 是查询的参数。

输入

第一行,给出两个整数 $n$, $q$ $(1\le n, q\le 5 \cdot 10^5)$,分别表示数组 $a$ 的长度和查询的数量。

第二行,给出 $n$ 个整数 $a_1, a_2, \ldots, a_n$ $(a_i \in \{-1, 1\})$,表示数组 $a$ 的元素。

接下来的 $q$ 行中,每行描述一个查询。第一个数字 $type_i$ $(1 \le type_i \le 2)$ 表示查询的类型。第一种类型的查询格式为 1 p $(1 \le p \le n)$,第二种类型的查询格式为 2 l r $(1 \le l \le r \le n)$。

输出

对于每一个第二种类型的查询,输出一个整数,表示对应数组的 beauty,每个答案占一行。

注释

$^1$ 如果数组 $c$ 可以通过从数组 $b$ 中删除若干(可能为零)个元素后得到,那么称 $c$ 为 $b$ 的一个子序列。空数组是任何数组的子序列。

示例

输入

5 4
1 1 1 -1 1
2 1 5
1 3
2 1 4
2 2 5

输出

5
2
3

输入

4 4
1 1 1 -1
2 1 2
2 2 4
2 3 3
2 3 4

输出

2
2
1
1

评分

  1. ($2$ 分):$a_i = (-1)^{i+1}$,对于所有 $1 \le i \le n$,并且没有第一种类型的查询;
  2. ($7$ 分):$n \le 16$,并且没有第一种类型的查询;
  3. ($21$ 分):$n, q \le 100$;
  4. ($20$ 分):$n, q \le 3000$;
  5. ($27$ 分):$n, q \leq 2 \cdot 10^5$,并且没有第一种类型的查询;
  6. ($14$ 分):$n, q \leq 2 \cdot 10^5$;
  7. ($9$ 分):无额外限制。