#U2223JB1. Leaders

Leaders

http://www.usaco.org/index.php?page=viewproblem2&cpid=1275

题面翻译

题面描述

FJ 有 NN 头奶牛,每一头奶牛的品种是根西岛 G 或荷斯坦 H 中的一种。

每一头奶牛都有一个名单,第 ii 头奶牛的名单上记录了从第 ii 头奶牛到第 EiE_i 头奶牛的所有奶牛。

每一种奶牛都有且仅有一位“领导者”,对于某一头牛 ii,如果它能成为“领导者”仅当它满足以下条件的至少一个

  • 其记录的名单上包含它的品种的所有奶牛。
  • 其记录的名单上记录了另一品种奶牛的“领导者”。

请求出有多少对奶牛可能成为两种奶牛的领导者,保证存在至少一种。

数据范围

对于 100%100\% 的数据:1N2×105,iEiN1\leq N\leq 2\times 10^5,i\leq E_i\leq N

题目描述

Farmer John has NN cows (2N105)(2 \le N \le 10^5). Each cow has a breed that is either Guernsey or Holstein. As is often the case, the cows are standing in a line, numbered 1N1 \cdots N in this order.

Over the course of the day, each cow writes down a list of cows. Specifically, cow ii 's list contains the range of cows starting with herself (cow ii) up to and including cow Ei(iEiN)E_i(i \le E_i \le N).

FJ has recently discovered that each breed of cow has exactly one distinct leader. FJ does not know who the leaders are, but he knows that each leader must have a list that includes all the cows of their breed, or the other breed's leader (or both).

Help FJ count the number of pairs of cows that could be leaders. It is guaranteed that there is at least one possible pair.

输入格式

The first line contains NN.

The second line contains a string of length NN , with the ith character denoting the breed of the ii-th cow (G meaning Guernsey and H meaning Holstein). It is guaranteed that there is at least one Guernsey and one Holstein.

The third line contains E1ENE_1 \cdots E_N.

输出格式

Output the number of possible pairs of leaders.

样例 #1

样例输入 #1

4
GHHG
2 4 3 4

样例输出 #1

1

样例 #2

样例输入 #2

3
GGH
2 3 3

样例输出 #2

2

提示

Explanation for Sample 1

The only valid leader pair is (1,2)(1,2). Cow 11's list contains the other breed's leader (cow 22). Cow 22's list contains all cows of her breed (Holstein).

No other pairs are valid. For example, (2,4)(2,4) is invalid since cow 44's list does not contain the other breed's leader, and it also does not contain all cows of her breed.

Explanation for Sample 2

There are two valid leader pairs, (1,3)(1,3) and (2,3)(2,3).

Scoring

  • Inputs 353-5: N100N \le 100
  • Inputs 6106-10: N3000N \le 3000
  • Inputs 111711-17: No additional constraints.