题意
一个长度为 n n n的石头剪刀布序列 S S S代表剪刀, R R R代表石头, P P P代表布,每次选择相邻的两个人进行比赛,直到只剩下一个人,有 m m m次修改,每次把某一个位置的人值的修改。输出 m + 1 m+1 m+1行,分别为这个序列有多少个人有可能会成为最后剩下那个人。
解析
对于某个人(假设为 S S S)如果他不可能是最后的胜者,那么他一定会被左边所有可能的胜者或者右边所有可能的胜者打败,只会有两种情况:
- 左边只有 S S S和 R R R
- 右边只有 S S S和 R R R
用 s e t set set和树状数组维护一下就行
#include <bits/stdc++.h>
using namespace std;
char s[200010];
int n,m,into[1000];
set<int> S[3];
int bit[200010][3];
int lowbit(int x){return x&(-x);}
void update(int x,int z,int t){for(x;x<200010;x+=lowbit(x))bit[x][t]+=z;}
int ask(int x,int t){int ans=0;for(x;x;x-=lowbit(x))ans+=bit[x][t];return ans;}
int calc(int x)
{
int y = (x+1)%3;//bei da
int z = (x+2)%3;
int ans = ask(200000,x);
if(S[y].size() == 2)return ans;
if(S[z].size() == 2)return 0;
set<int>::iterator a = S[y].upper_bound(n);a--;
set<int>::iterator b = S[z].upper_bound(n);b--;
if(*a>*b)ans-= ask(*a,x)-ask(*b,x);
a = S[y].lower_bound(1);
b = S[z].lower_bound(1);
if(*a<*b)ans-= ask(*b,x)-ask(*a,x);
return ans;
}
int main(int argc, char const *argv[])
{
scanf("%d%d",&n,&m);
into['S'] = 0;into['R'] = 1;into['P'] = 2;
scanf("%s",s+1);
for(int i=0;i<3;i++)S[i].insert(0),S[i].insert(n+1);
for(int i=1;i<=n;i++)
{
S[into[s[i]]].insert(i);
update(i,1,into[s[i]]);
}
int op;char q[10];
for(int i=1;i<=m;i++)
{
printf("%d\n",calc(0)+calc(1)+calc(2));
scanf("%d%s",&op,q);
char y = q[0];
S[into[s[op]]].erase(op);
update(op,-1,into[s[op]]);
s[op] = y;
S[into[s[op]]].insert(op);
update(op,1,into[s[op]]);
}
printf("%d\n",calc(0)+calc(1)+calc(2));
return 0;
}