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 ...
随机推荐
- 源代码方式调试Mycat
如果是第一次刚接触MyCat建议下载源码在本地通过eclipse等工具进行配置和运行,便于深入了解和调试程序运行逻辑. 1)源代码方式调试与配置 由于MyCat源代码目前主要托管在github上,大家 ...
- java final修饰变量时的一种情况
有如下一种场景. 1.在文件PaymentConfig.java中存在如下变量public static final desc="描述" 2.类Test.java中使用了desc变 ...
- EBS 中iSupplier模块中的MAPPING_ID
在EBS的供应商模块中,有一个非常有意思的表 POS_SUPPLIER_MAPPINGS, 这个表中建立了supplier_reg_id,vendor_id,party_id之间的映射关系. 这个表中 ...
- darktrace 亮点是使用的无监督学习(贝叶斯网络、聚类、递归贝叶斯估计)发现未知威胁——使用无人监督 机器学习反而允许系统发现罕见的和以前看不见的威胁,这些威胁本身并不依赖 不完善的训练数据集。 学习正常数据,发现异常!
先说说他们的产品:企业免疫系统(基于异常发现来识别威胁) 可以看到是面向企业内部安全的! 优点整个网络拓扑的三维可视化企业威胁级别的实时全局概述智能地聚类异常泛频谱观测 - 高阶网络拓扑;特定群集,子 ...
- JavaScript学习总结(十八)——JavaScript获取浏览器类型与版本
从网上找到一段使用JavaScript判断浏览器以及浏览器版本的比较好的代码,在此记录一下: 1 <script type="text/javascript"> 2 v ...
- logistic 回归与线性回归的比较
可以参考如下文章 https://blog.csdn.net/sinat_37965706/article/details/69204397 第一节中说了,logistic 回归和线性回归的区别是:线 ...
- python数据类型高阶
python是近年来使用最广泛的一种编程语言,不管是做web开发,还是网络爬虫,亦或是数据分析等,大家都在选择python来完成这些任务:我想最重要一点就是python学起来很简单,另一个点就是pyt ...
- adb命令(笔记)
1.adb shell su 进入root管理员权限(前提是手机已root) 2.chmod 可以修改文件夹的权限: $ adb shell $ su # chmod -R 777 /data/ ...
- Python Django 前后端数据交互 之 前端向后端发送数据
Python Django 之 前端向后端发送数据
- srtvlet filter
Filter,过滤器,顾名思义,即是对数据等的过滤,预处理过程.为什么要引入过滤器呢?在平常访问网站的时候,有时候发一些敏感的信息,发出后显示时 就会将敏感信息用*等字符替代,这就是用过滤器对信息进行 ...