n

players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw.

At the start of the tournament all players will stand in a row, with their numbers increasing from 1

for the leftmost player, to n

for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted:

  • If there is only one player left, he is declared the champion.
  • Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss.

The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request?

Input

The first line contains two integers n

and q — the number of players and requests respectively (1≤n≤2⋅105, 0≤q≤2⋅105

).

The second line contains a string of n

characters. The i-th of these characters is "R", "P", or "S" if the player i

was going to play "rock", "paper", or "scissors" before all requests respectively.

The following q

lines describe the requests. The j-th of these lines contain an integer pj and a character cj meaning that the player pj is going to use the shape described by the character cj from this moment (1≤pj≤n

).

Output

Print q+1

integers r0,…,rq, where rk is the number of possible champions after processing k

requests.

Example

Input
3 5
RPS
1 S
2 R
3 P
1 P
2 P
Output
2
2
1
2
2
3

题意:给定一排的人,每一轮可以人为决定两个人划拳,如果是平局,人为决定其中一个赢。问每次修改一个人的出拳方式,又多少个人可以win。

思路:一个人win的充要条件是左右同时满足:或没有可以打败他的,或者至少一个他可以打败的。

区间人数,我们可以用BIT维护。分三种情况累加即可。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
char c[maxn]; set<int>s[]; int sum[][maxn],N;
int id(char p){
if(p=='R') return ; if(p=='S') return ; return ;
}
void add(int opt,int x,int val)
{
for(;x<=N;x+=(-x)&x) sum[opt][x]+=val;
}
int query(int opt,int x){
int res=; for(;x;x-=(-x)&x) res+=sum[opt][x]; return res;
}
int cal(int p)
{
int lat=(p+)%,pre=(p+)%;
if(s[lat].empty()) return query(p,N); //set为空时不能用rbegin
if(s[pre].empty()) return ;
return query(p,N)-query(p,max(*s[pre].rbegin(),*s[lat].rbegin()))+//左边有可以被p打败的,右边无可以打败p的
query(p,min(*s[pre].begin(),*s[lat].begin()))+//右边有可以被p打败的,左边无可以打败p的
query(p,*s[pre].rbegin())-query(p,*s[pre].begin());//左右都有可以被p打败的
}
int main()
{
int M,pos,ans,p; char cc[];
scanf("%d%d%s",&N,&M,c+);
rep(i,,N){
p=id(c[i]);
s[p].insert(i); add(p,i,);
}
ans=cal()+cal()+cal();
printf("%d\n",ans);
rep(i,,M){
scanf("%d%s",&pos,cc+);
if(c[pos]==cc[]){ printf("%d\n",ans);continue;}
p=id(c[pos]); s[p].erase(pos); add(p,pos,-); //删
c[pos]=cc[]; p=id(c[pos]); s[p].insert(pos); add(p,pos,); //加
ans=cal()+cal()+cal();
printf("%d\n",ans);
}
return ;
}

CodeForces - 1087F:Rock-Paper-Scissors Champion(set&数状数组)的更多相关文章

  1. 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 H题 Rock Paper Scissors Lizard Spock.(FFT字符串匹配)

    2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227 题目链接:https://nanti.jisuanke.com/t ...

  2. wmz的数数(数状数组)

    wmz的数数(数状数组) 题目描述 \(wmz\)从小就显现出了过人的天赋,他出生的第三天就证明了哥德巴赫猜想,第五天就证明了质能方程,出生一星期之后,他觉得\(P\)是否等于\(NP\)这个问题比前 ...

  3. HDU 1166 敌兵布阵 (数状数组,或线段树)

    题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

  4. poj 2481 Cows(数状数组 或 线段树)

    题意:对于两个区间,[si,ei] 和 [sj,ej],若 si <= sj and ei >= ej and ei - si > ej - sj 则说明区间 [si,ei] 比 [ ...

  5. BZOJ2120:数颜色(数状数组套主席树)(带修改的莫对)

    墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...

  6. HDU-3015 Disharmony Trees [数状数组]

    Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...

  7. Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)

    [题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...

  8. Codeforces 703D Mishka and Interesting sum 离线+树状数组

    链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...

  9. HDU 1394Minimum Inversion Number 数状数组 逆序对数量和

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

随机推荐

  1. 使用uWSGI和nginx来设置Django和你的web服务器

    本教程针对那些想要设置一个生产web服务器的Django用户.它介绍了设置Django以使得其与uWSGI和nginx工作良好的必要步骤.它涵盖了所有三个组成部分,提供了一个web应用和服务器软件的完 ...

  2. Spark与Flink大数据处理引擎对比分析!

    大数据技术正飞速地发展着,催生出一代又一代快速便捷的大数据处理引擎,无论是Hadoop.Storm,还是后来的Spark.Flink.然而,毕竟没有哪一个框架可以完全支持所有的应用场景,也就说明不可能 ...

  3. 【Jmeter】Linux(Mac)上使用最新版本Jmeter(5.0)做性能测试

    本文我们一起来学习在Linux(Mac)上利用Jmeter进行性能测试并生成测试报告的方法. 环境准备 JDK 访问这个地址 [JDK11.01],根据实际环境下载一个JDK. Jmeter Jmet ...

  4. Codeforces 919D - Substring

    919D - Substring 思路: 拓扑排序判环+DAG上dp+记忆化搜索 状态:dp[i][j]表示以i为起点的路径中j的最大出现次数 初始状态:dp[i][j]=1(i have no so ...

  5. unity自义定摇杆

    写在前面,摇杆控制人物的移动,摄像机跟随人物移动,且滑动屏幕可以控制摄像机观察人物的角度. 需要考虑的问题 1.摇杆滑动角度的计算. 2.摇杆控制效果程度的计算(即:摇杆距离中心位置越远人物的移动速度 ...

  6. C# 中的浅表副本与深表副本

    public class Student { public int age; public Student(int age) { this.age = age; } } public class Gr ...

  7. linux 不解压日志压缩包直接搜索里面的内容

  8. 10个CSS简写/优化技巧-摘自网友

    10个CSS简写/优化技巧23来源/作者:未知 类别:前端开发 字体大小:大|中|小 背景颜色:蓝|白|灰 ? ? CSS简写就是指将多行的CSS属性简写成一行,又称为CSS代码优化或CSS缩写.CS ...

  9. idea列表

    Idea列表  

  10. Css的向左浮动、先右浮动、绝对定位、相对定位的简单使用

    1.div层的浮动 1)div向左浮动.向右浮动 <!doctype html> <html> <head> <meta charset="utf- ...