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 ...
随机推荐
- php 获取自己的公网IP
<?php $externalContent = file_get_contents('http://checkip.dyndns.com/'); preg_match('/Current IP ...
- Spring Cloud 学习网址
1. https://blog.csdn.net/forezp/article/details/70148833 史上最简单的 SpringCloud 教程 (非常适合新手快速上手教程)2.http ...
- string类的用法笔记
要想使用标准C++中string类,必须要包含 #include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 ...
- thinkphp3.2 jquery ajax巧妙使用
1.做帐号管理的时候,我们去除一些重复的帐号是有必要的. 我使用的是jquery ajax 来和控制器进行传值.当我们跳转到我们要验证方法返回结果的时候,我们就可以使用php里的一个 0 为false ...
- OC 文件基本操作
// // main.m // oc_13 // // Created by ma c on 15/12/17. // Copyright (c) 2015年 博文科技. All rights res ...
- 【VUE】@click加上v-bind绑定切换类名及动画事件
好长的名字... 效果是 点击元素,通过改变类名的方式让其改变颜色+移动动画效果,这里用的是v-bind和@click 废话不说 show me the code! <div id=" ...
- Sql Server约束的学习二(检查约束、默认约束、禁用约束)
接上一篇的Sql Server约束学习一(主键约束.外键约束.唯一约束) 4.检查约束 1)检查约束的定义 检查约束可以和一个列关联,也可以和一个表关联,因为它们可以检查一个列的值相对于另一个列的值, ...
- jsp jsp九个内置对象
每个JSP页面在第一次被访问的时候,WEB容器都会把请求交给JSP引擎(即一个JAVA)程序.JSP引擎先将JSP翻译成一个_jspServlet实质也是一次servlet,然后按照 Servl ...
- linux abstract model of virtual memory
- json批量设置DIV属性
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...