前言:中下位蓝。但是这道题能教会你不要在没有绝对的思路清晰前动代码。
思路
看起来非常恐怖的一道计数,变色龙在行进的过程中变换颜色看起来非常难处理。首先你容易发现向右移动的变色龙将会一路走到底,因为撞到他的变色龙将会变成他的颜色继续走下去。然后我们发现,其实对于每一只向左走的变色龙,他的贡献其实只有三种,第一种是对于一个 L 来说到达他左边第一个 D 会产生 的贡献,对于中间的相邻两个 D 会产生 的贡献,最后在他遇到最后一个 D 之后离开时还会产生一定贡献,这里我们需要对左边是否有 D 进行分讨。
发现第一三类贡献可以在 的时间复杂度内暴力求解。因此只需要考虑第二类贡献的计算。
下文令 表示第 个 L 前面的第一个 D 的位置, 表示前 个 D 的颜色变换数(即前缀和)。
继续思考下去,发现对于一个 D,他会造成贡献的区间为 L 的一段后缀,因此我们可以差分处理。我们记 表示到了第 个 L,颜色的变换数为 的第二类贡献贡献。然后每次对于一个 D,找到第一个在他后面的那个 D 后面的 L,设这个 L 的位置为 。此时我们需要将 的值加上 。 是对于我们 DDDL 这样的形态时当我们选择了第一个 D 时将第三个 D 的颜色加上。到这里,我们这道题的关键部分都解决了,接下来的实现详见代码。
代码
#include<bits/stdc++.h>
#define K 40 + 39
#define N 100000 + 39
#define int long long
using namespace std;
int n, k, l, tot[2], ans[K], las[N], Color[N], sum[N][K];
struct Node{
int x, color;
}a[2][N];
signed main(){
// freopen("glow.in", "r", stdin);
// freopen("glow.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> k >> l;
l <<= 1;//将位置全部翻倍就不用开double了。qwq
for(int i = 1; i <= n; i ++){
int x, color;
char op;
cin >> x >> color >> op;
x <<= 1;
if(op == 'D'){
a[0][++ tot[0]] = {x, color};//将D,L分类
ans[color] += l - x;
}
else{
a[1][++ tot[1]] = {x, color};
}
}
int tmp = 0;
for(int i = 1; i <= tot[1]; i ++){//处理las数组
while(tmp != tot[0] && a[0][tmp + 1].x < a[1][i].x){
tmp ++;
}
las[i] = tmp;
}
for(int i = 1; i <= tot[0]; i ++){//前缀和颜色
Color[i] = a[0][i].color + Color[i - 1];
}
for(int i = 1; i <= tot[1]; i ++){//处理第一类贡献
if(las[i]){
ans[a[1][i].color] += (a[1][i].x - a[0][las[i]].x) / 2;
}
}
for(int i = 1; i <= tot[1]; i ++){//第三类贡献
if(a[1][i].x > a[0][1].x && tot[0]){//前面有 D,遇到最前面的 D 之后离开。
ans[(a[1][i].color + Color[las[i]]) % k] += (a[1][i].x + a[0][1].x) / 2;
}
else{//前面没 D 直接离开
ans[a[1][i].color] += a[1][i].x;
}
}
tmp = 1;
for(int i = 1; i < tot[0]; i ++){
while(tmp <= tot[1] && a[1][tmp].x < a[0][i + 1].x){
tmp ++;
}//找到第一个在后面那个 D 的 L
if(tmp > tot[1]){
continue;
}
sum[tmp][(Color[las[tmp]] - Color[i]) % k] += (a[0][i + 1].x - a[0][i].x) / 2;//差分
}
for(int i = 1; i <= tot[1]; i ++){
for(int j = 0; j < k; j ++){
if(las[i] == las[i - 1]){
sum[i][j] += sum[i - 1][j];
}
else{
sum[i][j] += sum[i - 1][((j - Color[las[i]] + Color[las[i - 1]]) % k + k) % k];//实现的重点,加上两个 L 之间不同的 D 的颜色。
}
}
for(int j = 0; j < k; j ++){
ans[(a[1][i].color + j) % k] += sum[i][j];//记录答案,当前颜色加上自己本身的颜色。
}
}
for(int i = 0; i < k; i ++){
cout << ans[i] / 2;
if(ans[i] % 2){
cout << ".5\n";
}
else{
cout << ".0\n";
}
}
return 0;
}