由于数据范围是 1e5,然后每个彗星出现的时间都是一段区间,其实可以想到差分的思想
运用差分对一段区间快速的修改,最后前缀和一下
本题的思路就是定义一个时间轴,对于红彗星,蓝彗星分别存储,每次出现都对区间进行一次修改,最后就可以看出一段区间是不是有彗星出现
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
int a[N], b[N];
int main()
{
int n, t;
cin >> n >> t;
string s;
cin >> s;s = " " + s;
for (int i = 1;i <= n;i++)
{
int st;cin >> st;
if (s[i] == 'B')
a[st]++, a[st + t ]--;
else
b[st]++, b[st + t ]--;
}
//做一波前缀和
for (int i = 1;i < N;i++)
a[i] += a[i - 1], b[i] += b[i - 1];
int res = 0;
for (int i = 1;i < N;i++)
if (b[i] <= 0 && a[i] > 0)
res++;
cout << res;
}