CodeForces - 1087F:Rock-Paper-Scissors Champion(set&数状数组)
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
3 5
RPS
1 S
2 R
3 P
1 P
2 P
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&数状数组)的更多相关文章
- 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 H题 Rock Paper Scissors Lizard Spock.(FFT字符串匹配)
2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227 题目链接:https://nanti.jisuanke.com/t ...
- wmz的数数(数状数组)
wmz的数数(数状数组) 题目描述 \(wmz\)从小就显现出了过人的天赋,他出生的第三天就证明了哥德巴赫猜想,第五天就证明了质能方程,出生一星期之后,他觉得\(P\)是否等于\(NP\)这个问题比前 ...
- HDU 1166 敌兵布阵 (数状数组,或线段树)
题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...
- poj 2481 Cows(数状数组 或 线段树)
题意:对于两个区间,[si,ei] 和 [sj,ej],若 si <= sj and ei >= ej and ei - si > ej - sj 则说明区间 [si,ei] 比 [ ...
- BZOJ2120:数颜色(数状数组套主席树)(带修改的莫对)
墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...
- HDU-3015 Disharmony Trees [数状数组]
Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
[题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...
- HDU 1394Minimum Inversion Number 数状数组 逆序对数量和
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- [设计模式]适配器模式Adapter
将一个类的接口转换成客户希望的另外一个接口. A d a p t e r模式使得原本 由于接口不兼容而不能一起工作的那些类可以一起工作.
- unity中实现静态的3D对象对其他对象的跟随
using UnityEngine; public class FollowPosition : MonoBehaviour { public Transform targetTrans; publi ...
- Python string常用函数
2017-07-03 23:26:08 1..replace(self, old, new, count=-1) replace()函数将旧字符串替换为新字符串,最后一个参数count为可选项,表示替 ...
- 很实用且容易忘记的小命令 for Linux(更新中...)
系统相关 # 系统安装日期 sudo tune2fs -l /dev/sda1 |grep create # 查看centos版本命令 rpm -q centos-release #查看centos版 ...
- linux下修改mysql登录密码
一.修改mysql密码 1.停止服务 /etc/init.d/mysqld stop 2.以不检查权限的方式启动 /etc/init.d/mysqld --skip-grant- ...
- Java基础-集合(12)
存储数据的容器有数组和StringBuilder.StringBuilder的结果是一个字符串,不满足要求,所以只能选择数组,这就是对象数组.而对象数组又不能适应变化的需求,因为数组的长度是固定的,这 ...
- Nginx+uWsgi生产部署Django
部署得过程很简单,部署得核心在于,为什么要这样做,每一个参数代表什么意思,最终的目的是得了解,一些基概念性的东西. uWsgi简介 说Uwsgi之前,先说一下Wsgi. 什么是Wsgi? WSGI: ...
- mysql 随机获取数据并插入到数据库中
insert into result (user_id, activity_id, number) select user_id, activity_id from `activity_record` ...
- 在oaf中集成SpringLoaded实现热部署
首先声明:其实JRebel和Spring-Loaded就是一个开发环境下的利器,skip build and redeploy process,大大提升了工作效率!而非生产环境的利器... 不要在生产 ...
- AIX5L内存监控和调整
1.ps ps gv | head -n 1; ps gv | egrep -v "RSS" | sort +6b -7 -n -r PID TTY STAT ...