D. Playoff Tournament

D. Playoff Tournament
Example
inputCopy
3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1
outputCopy
1
2
3
3
5
4
线段树nlogn
遇到 ? 该节点的值为左子树加右子树
遇到 1 该节点的值为右子树
遇到 0 该节点的值为左子树

#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int maxn=(1<<18)+100;
int a[maxn];
char s[maxn];
map<int,int>mp,mp2;//映射:最后一场比赛对应线段树节点1 最后两场对应 2 3 以此类推
struct node
{
	int l,r,sum;
	char op;
} t[maxn*4];
void build(int l,int r,int k)
{
	t[k].l=l;
	t[k].r=r;
	t[k].op=s[mp2[k]];
	if(l==r)
	{
		t[k].sum=1;
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,k<<1);
	build(mid+1,r,k<<1|1);
	if(t[k].op=='?')t[k].sum=t[k<<1|1].sum+t[k<<1].sum;
	if(t[k].op=='1')t[k].sum=t[k<<1|1].sum;
	if(t[k].op=='0')t[k].sum=t[k<<1].sum;
}
signed main()
{
	IOS
	int k,q;
	cin>>k;;
	cin>>(s+1);
	cin>>q;
	int n=(1<<k)-1;
	int cnt=1;
	int num=0;
	int sm=1;
	while(sm!=n)
	{
		for(int i=n-sm+1; i<=n-sm+1+cnt-1; i++)
		{
			mp[i]=++num;
			mp2[num]=i;
		}
		cnt=cnt*2;
		sm+=cnt;
	}
	for(int i=n-sm+1; i<=n-sm+1+cnt-1; i++)
	{
		mp[i]=++num;
		mp2[num]=i;
	}
	build(1,n+1,1);
	//for(int i=1;i<=7;i++)cout<<t[i].sum<<" ";
	//for(int i=8;i<=15;i++)cout<<t[i].sum<<" ";
	while(q--)
	{
		int id;
		string str;
		cin>>id>>str;
		s[id]=str[0];
		t[mp[id]].op=str[0];
		int p=mp[id];
		while(p!=1)
		{
			if(t[p].op=='?')t[p].sum=t[p<<1|1].sum+t[p<<1].sum;
			if(t[p].op=='1')t[p].sum=t[p<<1|1].sum;
			if(t[p].op=='0')t[p].sum=t[p<<1].sum;
			p=p>>1;
		}
		if(t[p].op=='?')t[p].sum=t[p<<1|1].sum+t[p<<1].sum;
		if(t[p].op=='1')t[p].sum=t[p<<1|1].sum;
		if(t[p].op=='0')t[p].sum=t[p<<1].sum;
		cout<<t[1].sum<<"\n";
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

thusloop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值