icontofig | 发布于 2018-12-26 11:18:55 | 阅读量 520 | 线段树

## F. Rock-Paper-Scissors Champion

time limit:per test 2 seconds
memory limit:per test 256 megabytes
input:standard input
output:standard output

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:

1.If there is only one player left, he is declared the champion.
2.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⋅10^5, 0≤q≤2⋅10^5).

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.

```3 5
RPS
1 S
2 R
3 P
1 P
2 P```
```2
2
1
2
2
3```

## 题解代码（线段树）

```#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
set<int>x[3];
vector<int>v;
int n,q;
const int maxn = 2e5+5;
struct seg{
int l,r,val[3];
}tr[maxn<<2];
void push_up(int now){
for(int i = 0;i < 3;++i){
tr[now].val[i] = tr[now<<1].val[i] + tr[now<<1|1].val[i];
}
return;
}
void build(int now,int l,int r){
tr[now].l = l;
tr[now].r = r;
for(int i = 0;i < 3;++i)
tr[now].val[i] = 0;
if(l == r){
tr[now].val[v[l]] = 1;
return;
}
int mid = (l + r) >> 1;
build(now<<1,l,mid);
build(now<<1|1,mid+1,r);
push_up(now);
return;
}
void update(int now,int pos,int vc){
if(tr[now].l == tr[now].r && tr[now].l == pos){
for(int i = 0;i < 3;++i)
tr[now].val[i] = 0;
tr[now].val[vc] = 1;
return;
}
int mid = (tr[now].l + tr[now].r) >> 1;
if(pos <= mid)update(now<<1,pos,vc);
else update(now<<1|1,pos,vc);
push_up(now);
return;
}
int query(int now,int ql,int qr,int vc){
if(qr < ql)return 0;
if(tr[now].l >= ql && tr[now].r <= qr){
return tr[now].val[vc];
}
int mid = (tr[now].l + tr[now].r) >> 1;
int ans = 0;
if(ql <= mid)ans += query(now<<1,ql,qr,vc);
if(qr > mid)ans += query(now<<1|1,ql,qr,vc);
return ans;
}
int get_ans(){
int ans = n;
for(int i = 0;i < 3;++i){
int j = (i + 1) % 3;
int k = (j + 1) % 3;
if(!x[j].empty()){
if(!x[k].empty()){
ans -= query(1,*x[j].begin(),*x[k].begin(),i);
ans -= query(1,*(--x[k].end()),*(--x[j].end()),i);
}
else ans -= query(1,0,n-1,i);
}
}
return ans;
}
int change(char c){
int ret;
switch(c){
case 'R' : ret = 0;break;
case 'P' : ret = 1;break;
case 'S' : ret = 2;break;
default : break;
}
return ret;
}
string s;
int a;
char ch;
int main(){
cin >> n >> q;
cin >> s;
for(int i = 0;i < n;++i){
ch = s[i];
v.push_back(change(ch));
x[change(ch)].insert(i);
}
build(1,0,n-1);
cout << get_ans() << endl;
while(q--){
cin >> a >> ch;
a--;
x[v[a]].erase(x[v[a]].find(a));
v[a] = change(ch);
x[change(ch)].insert(a);
update(1,a,change(ch));
cout << get_ans() << endl;
}
return 0;
}﻿​```

520 人读过

0 条评论