D. Playoff Tournament - 线段树 -建模

2023-10-25 18:10

本文主要是介绍D. Playoff Tournament - 线段树 -建模,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Problem - D - Codeforces

D. Playoff Tournament

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

2k2k teams participate in a playoff tournament. The tournament consists of 2k−12k−1 games. They are held as follows: first of all, the teams are split into pairs: team 11 plays against team 22, team 33 plays against team 44 (exactly in this order), and so on (so, 2k−12k−1 games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2k−12k−1 teams remain. If only one team remains, it is declared the champion; otherwise, 2k−22k−2 games are played: in the first one of them, the winner of the game "11 vs 22" plays against the winner of the game "33 vs 44", then the winner of the game "55 vs 66" plays against the winner of the game "77 vs 88", and so on. This process repeats until only one team remains.

For example, this picture describes the chronological order of games with k=3k=3:

Let the string ss consisting of 2k−12k−1 characters describe the results of the games in chronological order as follows:

  • if sisi is 0, then the team with lower index wins the ii-th game;
  • if sisi is 1, then the team with greater index wins the ii-th game;
  • if sisi is ?, then the result of the ii-th game is unknown (any team could win this game).

Let f(s)f(s) be the number of possible winners of the tournament described by the string ss. A team ii is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team ii is the champion.

You are given the initial state of the string ss. You have to process qq queries of the following form:

  • pp cc — replace spsp with character cc, and print f(s)f(s) as the result of the query.

Input

The first line contains one integer kk (1≤k≤181≤k≤18).

The second line contains a string consisting of 2k−12k−1 characters — the initial state of the string ss. Each character is either ?, 0, or 1.

The third line contains one integer qq (1≤q≤2⋅1051≤q≤2⋅105) — the number of queries.

Then qq lines follow, the ii-th line contains an integer pp and a character cc (1≤p≤2k−11≤p≤2k−1; cc is either ?, 0, or 1), describing the ii-th query.

Output

For each query, print one integer — f(s)f(s).

Example

input

Copy

3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1

output

Copy

1
2
3
3
5
4

=========================================================================

对全部选手做一个线段树,长度为1的区间代表个人。长度大于等于2的区间,恰好有2^k -1个,对应了2^k -1次比赛, 重新编号比赛, 本题中 ,一共有7场比赛,把第i场变成n-i 场,其中n=(2^k),那么就可以发现, 第七场之余 第五场,第六场的关系,是新编号下 第一场之于 第二场,第三场的关系,也就是 线段树的左右子树关系。

继续考虑 01?对结果的影响, 首先值得关注的一个性质是,我们构造的这个线段树,其左儿子的值一定大于右儿子,如果当前s值对应为 0,那么选择右儿子,1 选择左儿子, ?就选择两者之和即可。

然后是单点修改操作,值得注意的是,我们这个单点修改的这个点,并不是具体的线段树最基础的叶子节点(具体的某个人),而是每场比赛,也就是线段树每个区间的编号,但是传统的自上而下查询的方法并不能帮我们定位到某个具体的区间编号,所以考虑自下而上的更新,获取比赛编号,每次除以2就获得了父亲节点,这样更新时log级别的,方便快捷。

# include<bits/stdc++.h>using namespace std;
typedef long long int ll;
string s;
int id[(1<<19)];
int ans[(1<<19)*4+10];void build(int root,int l,int r)
{if(l==r){ans[root]=1;return ;}int mid=(l+r)>>1;int mproot=id[root];build(root<<1,l,mid);build(root<<1|1,mid+1,r);if(s[mproot]=='?')ans[root]=ans[root<<1]+ans[root<<1|1];else if(s[mproot]=='1')ans[root]=ans[root<<1];elseans[root]=ans[root<<1|1];// cout<<root<<" "<<ans[root]<<endl;
}void change(int root)
{while(root){int mproot=id[root];if(s[mproot]=='?')ans[root]=ans[root<<1]+ans[root<<1|1];else if(s[mproot]=='1')ans[root]=ans[root<<1];elseans[root]=ans[root<<1|1];root>>=1;}}
int main ()
{int n;cin>>n;n=(1<<n);cin>>s;s=" "+s;for(int i=1; i<n; i++){id[i]=n-i;}//左子树更大build(1,1,n);int t;cin>>t;while(t--){int pos;char ch;cin>>pos>>ch;s[pos]=ch;change(id[pos]);cout<<ans[1]<<endl;}return 0;
}

这篇关于D. Playoff Tournament - 线段树 -建模的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/284312

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

poj 1127 线段相交的判定

题意: 有n根木棍,每根的端点坐标分别是 px, py, qx, qy。 判断每对木棍是否相连,当他们之间有公共点时,就认为他们相连。 并且通过相连的木棍相连的木棍也是相连的。 解析: 线段相交的判定。 首先,模板中的线段相交是不判端点的,所以要加一个端点在直线上的判定; 然后,端点在直线上的判定这个函数是不判定两个端点是同一个端点的情况的,所以要加是否端点相等的判断。 最后

HDU4737线段树

题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i,j)<=m的总数。 const int Max_N = 100008 ;int sum[Max_N<<2] , x[Max_N] ;int n , m ;void push_up(int t){sum[t] = sum[t<<1] | sum[t<<1|1] ;}void upd

基于UE5和ROS2的激光雷达+深度RGBD相机小车的仿真指南(五):Blender锥桶建模

前言 本系列教程旨在使用UE5配置一个具备激光雷达+深度摄像机的仿真小车,并使用通过跨平台的方式进行ROS2和UE5仿真的通讯,达到小车自主导航的目的。本教程默认有ROS2导航及其gazebo仿真相关方面基础,Nav2相关的学习教程可以参考本人的其他博客Nav2代价地图实现和原理–Nav2源码解读之CostMap2D(上)-CSDN博客往期教程: 第一期:基于UE5和ROS2的激光雷达+深度RG

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;

圆与线段的交点

poj 3819  给出一条线段的两个端点,再给出n个圆,求出这条线段被所有圆覆盖的部分占了整条线段的百分比。 圆与线段的交点 : 向量AB 的参数方程  P = A + t * (B - A)      0<=t<=1 ; 将点带入圆的方程即可。  注意: 有交点 0 <= t <= 1 ; 此题求覆盖的部分。 则 若求得 t  满足 ; double ask(d