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 ...
随机推荐
- 《F4+2团队项目需求改进与系统设计》
任务一 a.分析<动态的太阳系模型项目需求规格说明书>初稿的不足. 任务概述描述的有些不具体,功能的规定不详细,在此次作业进行了修改. b.参考<构建之法>8.5节功能的定位和 ...
- viewpager实现画廊(一屏多个Fragment)效果
本文主要介绍如何利用ViewPager实现Gallery的画廊效果,即一屏显示多个Fragment. 效果图如下: 在 使用Gallery的时候大家会发现有几个问题(1). 无法控制每次滑动只滑动一页 ...
- Redis 安装到linux系统
下载地址 : http://download.redis.io/releases/redis-3.0.3.tar.gz ). tar -zxvf redis-.tar.gz -C /usr/share ...
- sgu 126 Boxes
题意:较大的容量减较小的容量,较小的容量翻倍.问操作几回其中一个空. 开始用set判重,重复就不可行.不过状态最多有2e18种.不仅爆内存,还超时.然后找规律.发现只有比例为1:1,1:3,1:7,3 ...
- linux 检查补丁包是否安装 名称 版本 release号
To determine whether the required packages are installed, enter commands similar to the following: # ...
- Leetcode 96
class Solution { public: int numTrees(int n) { ]; dp[] = ; dp[] = ; dp[] = ; ;i <= n;i++){ ; ;j & ...
- 数据库到jsp页面报错(一)
数据库到jsp页面报错(一) 这个错误的确比较坑. 控制台: 页面: 解决: 神坑啊!!!
- JavaScript学习总结(十三)——极简主义法编写JavaScript类
前两天在网上无意中发现了一篇使用极简主义法定义JavaScript类的文章,原文链接,这个所谓的"极简主义法"我还是第一次听说,是荷兰程序员Gabor de Mooij提出来的,这 ...
- spring boot 学习番外篇:超快速项目初始化
超快速完成 Spring Boot 项目初始化 最近,在浏览 SPRING 官网时,发现一个超级方便的小工具,可以帮助我们快速创建一个 Spring Boot 项目,前提就是你能连接互联网. 依赖 支 ...
- Windows环境搭建ElasticSearch 5.*并配置head
前言: ES5*以上版本需要jdk1.8,jdk1.8,jdk1.8.重要的事情说三遍 1.下载ElasticSearch https://www.elastic.co/cn/downloads/el ...