#include #include #include #include #include #include #include struct BIT { std::vector data_; int64_t total_; BIT(int32_t n): data_(n + 1, 0), total_(0) {} int64_t query(int64_t x) { int64_t res = 0; for (++x; x > 0; x -= (x & -x)) { res += data_[x]; } return res; } void update(int64_t x, int64_t d) { total_ += d; for (++x; x < data_.size(); x += (x & -x)) { data_[x] += d; } } }; std::vector DYNAMICS = { "ppp", "pp", "p", "mp", "mf", "f", "ff", "fff" }; int main() { int32_t n, m; std::cin >> n >> m; std::vector a(n); for (auto& x : a) std::cin >> x; std::vector vals = a; std::ranges::sort(vals); vals.erase(std::unique(vals.begin(), vals.end()), vals.end()); for (auto& x : a) { x = std::lower_bound(vals.begin(), vals.end(), x) - vals.begin(); } std::vector b(n, -1); std::string dynamic; for (int32_t i = 0; i < m; ++i) { int32_t j; std::cin >> j >> dynamic; --j; int32_t v = std::ranges::find(DYNAMICS, dynamic) - DYNAMICS.begin(); b[j] = v; } assert(b[0] != -1); for (int32_t i = 1; i < n; ++i) { if (b[i] == -1) b[i] = b[i - 1]; } std::vector bits(8, BIT(vals.size())); int64_t ans = 0; for (int32_t i = 0; i < n; ++i) { for (int32_t j = 0; j < 8; ++j) { if (j < b[i]) { ans += bits[j].total_ - bits[j].query(a[i] - 1); } else if (j > b[i]) { ans += bits[j].query(a[i]); } } bits[b[i]].update(a[i], +1); } std::cout << ans << '\n'; return 0; }