QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#325763#8226. 堆操作练习题2zhouhuanyi10 8ms36764kbC++143.2kb2024-02-11 21:58:162024-02-11 21:58:16

Judging History

你现在查看的是最新测评结果

  • [2024-05-22 20:40:58]
  • hack成功,自动添加数据
  • (/hack/631)
  • [2024-02-11 21:58:16]
  • 评测
  • 测评结果:10
  • 用时:8ms
  • 内存:36764kb
  • [2024-02-11 21:58:16]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cmath>
#define N 18
#define M 1000000
#define mod 1000000007
using namespace std;
int read()
{
	char c=0;
	int sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
void Adder(int &x,int d)
{
	x+=d;
	if (x>=mod) x-=mod;
	return;
}
void Adder2(int &x,int d)
{
	x+=d;
	if (x<0) x+=mod;
	return;
}
int MD(int x)
{
	return x>=mod?x-mod:x;
}
int MD2(int x)
{
	return x<0?x+mod:x;
}
int h,q,leng,ans,lg[1<<N],depth[1<<N],dfn[1<<N],sz[1<<N],num[1<<N],pw2[1<<N],a[1<<N],F[1<<N],inv[3]={0,1,(mod+1)>>1},cnt;
bool used[1<<N],used2[1<<N];
vector<int>p[1<<N];
vector<int>A[1<<N];
vector<int>B[1<<N];
vector<int>dp[1<<N];
bool cmp(int x,int y)
{
	return x>y;
}
bool LENG(int x,int y)
{
	return dfn[x]<=dfn[y]&&dfn[y]<=dfn[x]+sz[x]-1;
}
void dfs(int x)
{
	dfn[x]=++leng;
	if ((x<<1)<=(1<<h)-1)
	{
		depth[x<<1]=depth[x<<1|1]=depth[x]+1,dfs(x<<1),dfs(x<<1|1),sz[x]=sz[x<<1]+sz[x<<1|1]+1;
		for (int i=0;i<sz[x<<1];++i) p[x].push_back(p[x<<1][i]);
		for (int i=0;i<sz[x<<1|1];++i) p[x].push_back(p[x<<1|1][i]);
		p[x].push_back(a[x]);
	}
	else sz[x]=1,p[x].push_back(a[x]);
	A[x].resize(sz[x]+1),B[x].resize(sz[x]+1),dp[x].resize(sz[x]+1),sort(p[x].begin(),p[x].end(),cmp);
	for (int i=0;i<=sz[x];++i) dp[x][i]=1;
	return;
}
int get_rk(int x,int d)
{
	int ps=0;
	for (int i=lg[sz[x]];i>=0;--i)
		if (ps+(1<<i)<sz[x]&&p[x][ps+(1<<i)]>=d)
			ps+=(1<<i);
	return ps;
}
int query(int x,int d)
{
	if (depth[x]>=(h>>1)) return dp[x][d];
	else
	{
		if (d==sz[x]) return 1ll*query(x<<1,sz[x<<1])*query(x<<1|1,sz[x<<1|1])%mod*F[x]%mod;
		else return 1ll*query(x<<1,A[x][d])*query(x<<1|1,B[x][d])%mod;
	}
}
int calc(int x,int d)
{
	d=get_rk(x,d);
	return 1ll*MD2(query(x,d)-(d!=sz[x]?query(x,d+1):0))*pw2[cnt]%mod;
}
void change(int x)
{
	while (depth[x]>=(h>>1))
	{
		if ((x<<1)<=(1<<h)-1)
		{
			for (int i=0;i<=sz[x]-1;++i) dp[x][i]=1ll*dp[x<<1][A[x][i]]*dp[x<<1|1][B[x][i]]%mod;
			dp[x][sz[x]]=1ll*dp[x<<1][sz[x<<1]]*dp[x<<1|1][sz[x<<1|1]]%mod*F[x]%mod;
		}
		else dp[x][0]=1,dp[x][1]=F[x];
		x>>=1;
	}
	return;
}
int main()
{
	int op,x,y,ps,ps2;
	for (int i=2;i<=(1<<N)-1;++i) lg[i]=lg[i>>1]+1;
	pw2[0]=1;
	for (int i=1;i<=(1<<N)-1;++i) pw2[i]=2ll*pw2[i-1]%mod;
	h=read();
	for (int i=1;i<=(1<<h)-1;++i) a[i]=read(),num[a[i]]=i;
	depth[1]=1,dfs(1);
	for (int i=1;(i<<1)<=(1<<h)-1;++i)
	{
		ps=ps2=0;
		for (int j=0;j<=sz[i<<1]+sz[i<<1|1];++j)
		{
			if (j)
			{
				if (ps!=p[i<<1].size()&&(ps2==p[i<<1|1].size()||p[i<<1][ps]>p[i<<1|1][ps2])) ps++;
				else ps2++;
			}
			A[i][j]=ps,B[i][j]=ps2;
		}
		A[i][sz[i]]=sz[i<<1],B[i][sz[i]]=sz[i<<1|1];
	}
	q=read();
	for (int i=1;i<=q;++i)
	{
		op=read(),x=read(),y=read();
		if (op==1)
		{
			if (y==1) used[x]=1;
			else used2[x]=1,cnt++;
			F[x]=1ll*(!used[x])*inv[1+used2[x]]%mod,change(x);
		}
		else if (op==2)
		{
			if (y==1) used[x]=0;
			else used2[x]=0,cnt--;
			F[x]=1ll*(!used[x])*inv[1+used2[x]]%mod,change(x);
		}
		else
		{
			if (!LENG(x,num[y])) puts("0");
			else printf("%d\n",calc(x,y));
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 6ms
memory: 36160kb

input:

2
3 2 1
50
3 3 1
1 1 2
1 2 1
2 2 1
1 2 2
2 1 2
1 1 1
1 3 2
2 1 1
2 2 2
3 1 2
3 1 3
2 3 2
1 3 2
1 2 2
2 2 2
1 2 1
1 1 2
3 1 1
2 1 2
1 1 1
2 1 1
3 1 2
3 1 3
2 3 2
1 3 2
2 2 1
1 2 1
1 1 1
3 1 2
2 1 1
1 1 1
3 3 1
2 1 1
2 3 2
1 3 1
2 3 1
1 1 2
3 1 3
2 1 2
3 3 1
3 1 3
3 1 1
3 1 1
1 3 2
1 1 1
2 1 1
3 1 1
2...

output:

0
1
0
0
0
2
0
1
2
0
1
0
0
0
0

result:

ok 15 numbers

Test #2:

score: 0
Accepted
time: 8ms
memory: 36464kb

input:

2
3 1 2
50
1 2 2
3 3 2
2 2 2
1 3 1
1 1 1
3 3 2
2 1 1
3 1 3
2 3 1
1 3 1
1 1 1
1 2 2
2 1 1
3 1 3
2 3 1
2 2 2
1 1 2
1 2 1
1 3 1
2 1 2
3 3 2
1 1 1
2 3 1
1 3 2
2 3 2
1 3 1
2 2 1
3 3 2
3 1 1
2 3 1
2 1 1
1 3 1
2 3 1
3 1 3
3 1 3
3 1 3
1 1 1
2 1 1
3 1 2
1 2 1
3 1 1
3 3 2
3 1 3
2 2 1
1 1 1
2 1 1
1 2 1
1 1 2
2...

output:

0
1
1
2
1
1
0
0
0
0
0
0
0
0
0

result:

ok 15 numbers

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #3:

score: 10
Accepted
time: 0ms
memory: 35644kb

input:

4
15 14 13 9 10 11 12 2 7 4 5 1 6 8 3
500
3 1 15
3 1 13
1 9 1
1 6 2
1 15 2
1 14 1
1 10 2
1 5 2
3 12 1
1 1 1
1 4 1
3 6 6
3 10 4
1 3 1
3 13 6
1 11 2
2 4 1
2 14 1
3 6 6
3 1 11
3 1 14
3 2 4
2 6 2
2 15 2
3 1 9
3 7 12
1 13 2
2 9 1
2 5 2
3 14 8
1 15 2
1 12 1
3 11 5
1 5 2
3 1 15
3 5 10
3 1 11
1 14 1
2 13 2
...

output:

0
0
0
0
8
0
0
8
0
0
0
0
0
8
16
16
0
0
0
32
0
0
32
16
0
0
16
0
0
0
0
4
0
0
0
0
8
0
0
0
0
0
64
64
0
128
0
0
0
128
64
256
0
64
64
0
0
0
8
4
2
0
2
0
0
0
0
8
4
16
0
0
2
0
2
8
0
0
16
0
8
0
0
16
32
0
32
0
4
4
0
4
0
0
0
0
4
0
0
0
2
0
0
0
0
0
4
2
0
0
0
16
8
0
0
0
0
8
2
4
0
4
4
4
16
0
8
8
16
16
0
0
8
0
0
0
0
...

result:

ok 171 numbers

Test #4:

score: -10
Wrong Answer
time: 7ms
memory: 36052kb

input:

4
15 13 14 7 12 8 11 3 4 9 2 5 6 1 10
500
1 2 1
1 14 2
1 4 1
3 1 12
1 15 2
3 1 15
1 9 2
2 4 1
1 13 1
3 7 1
2 13 1
2 15 2
1 7 2
1 13 1
3 15 10
2 2 1
3 2 2
1 2 2
2 2 2
1 15 2
1 11 1
2 9 2
3 3 11
1 1 1
1 12 2
3 10 9
3 4 3
3 7 1
2 7 2
1 4 1
1 5 1
2 15 2
2 12 2
1 10 2
3 2 7
3 2 13
1 3 1
3 1 15
2 1 1
3 14...

output:

1
2
2
0
0
2
0
0
2
0
2
0
2
0
8
4
0
0
8
4
0
4
4
4
1
0
0
4
0
0
8
0
0
0
32
128
0
0
0
16
0
16
0
0
0
0
8
0
0
0
0
0
16
0
32
0
0
0
8
4
0
0
4
0
0
0
0
0
8
8
0
8
0
0
0
8
0
0
16
0
0
0
0
0
0
2
0
2
0
0
2
0
0
2
1
0
0
0
0
8
16
16
0
0
0
0
0
0
0
0
0
0
0
16
0
0
16
32
0
0
16
0
32
0
0
64
0
4
0
8
0
0
0
4
1
0
0
16
4
0
0
0...

result:

wrong answer 3rd numbers differ - expected: '0', found: '2'

Subtask #3:

score: 0
Wrong Answer

Test #5:

score: 0
Wrong Answer
time: 3ms
memory: 36764kb

input:

9
511 509 510 504 507 505 508 501 503 506 502 494 500 499 493 473 483 495 475 491 497 461 487 490 489 498 496 478 485 480 488 378 469 482 477 462 448 422 470 424 467 421 492 439 454 484 451 376 385 458 464 463 486 411 472 449 474 459 468 479 413 457 455 371 315 432 437 466 453 476 418 433 363 434 38...

output:

0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
1
1
1
0
0
0
0
1
0
0
1
1
1
1
0
1
1
1
1
0
1
1
0
0
0
0
1
0
0
1
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
1
1
0
1
0
0
1
0
0
0
0
1
1
0
0
1
1
1
1
1
0
0
1
1
1
1
1
0
1
0
0
1
1
0
1
0
0
1
0
0
0
0
1
0
1
1
0
1
0
0
1
0
0
1
1
1
0
0
1
1
1
0
0
0
0
1
1
0
1
1
1
1
0
1
0
0
0
1
0
1
1
1
0
0
1
0
0
0
1
1
...

result:

wrong answer 160th numbers differ - expected: '1', found: '0'

Subtask #4:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%

Subtask #5:

score: 0
Skipped

Dependency #3:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%