QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#631#329481#8226. 堆操作练习题2MilmonLarunatrecySuccess!2024-05-22 20:40:352024-05-22 20:40:40

详细

Extra Test:

Time Limit Exceeded

input:

18
999993 999992 999989 999984 999981 999978 999963 999962 999955 999945 999942 999941 999938 999932 999927 999925 999922 999920 999919 999911 999905 999904 999901 999900 999898 999895 999887 999886 999885 999875 999870 999869 999865 999863 999862 999860 999859 999858 999855 999850 999846 999845 999...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:


ID题目提交者结果用时内存语言文件大小提交时间测评时间
#329481#8226. 堆操作练习题2Larunatrecy97 299ms46904kbC++141.5kb2024-02-16 19:29:412024-05-22 20:43:01

answer

#include<bits/stdc++.h>
using namespace std;
const int N = 3e5+7;
const int M = 19;
int h,n,m;
int a[N],dep[N],f[N][M],fa[N][M];
void SiftDown()
{
	int x=1;
	while((x<<1)<=n)
	{
		int p;
		if(a[x<<1]>a[x<<1|1])p=(x<<1);
		else p=(x<<1|1);
		if(a[p]==0)break;
		x=p;
	}
	for(int i=0;i<=dep[x];i++)
	f[x][i]=a[fa[x][i]];
	x=1;
	a[1]=0;
	while((x<<1)<=n)
	{
		int p;
		if(a[x<<1]>a[x<<1|1])p=(x<<1);
		else p=(x<<1|1);
		if(a[p]==0)break;
		swap(a[x],a[p]);
		x=p;
	}
}
int col[N],S=0,pw[N];
const int mod = 1e9+7;
int C=0,R=0,E=0,F=0,G=0; 
void dfs(int x,int z,int d)
{
	if(x>n)return;
	if(col[x]==1)
	{
		if(f[x][d]>z)F=1;
		if(f[x][d]==z)G=1;
	}
	else if(col[x]==2)
	{
		R--;
		if(f[x][d]==z) C++;
		if(f[x][d]<z) E++;
	}
	dfs(x<<1,z,d);dfs(x<<1|1,z,d);
}
int main()
{
	cin>>h;n=(1<<h)-1;
	pw[0]=1;
	for(int i=1;i<=n;i++)pw[i]=1ll*pw[i-1]*2%mod;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=2;i<=n;i++)dep[i]=dep[i>>1]+1;
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<=dep[i>>1];j++)
		fa[i][j]=fa[i>>1][j];
		fa[i][dep[i]]=i;
	}
	for(int i=1;i<=n;i++)SiftDown();
	cin>>m;
	while(m--)
	{
		int op,x,y;
		scanf("%d %d %d",&op,&x,&y);
		if(op==1)
		{
			col[x]=y;
			if(col[x]==2)S++;
			continue;
		}
		else if(op==2)
		{
			if(col[x]==2)S--;
			col[x]=0;
			continue;
		}
		C=0;R=S;E=0;F=0;G=0;
		dfs(x,y,dep[x]);
		int ans=0;
		if(F==1)ans=0;
		else 
		{
			if(G)ans=pw[C+E];
			else ans=1ll*pw[E]*(pw[C]-1+mod)%mod;
		}
		ans=1ll*ans*pw[R]%mod;
		printf("%d\n",ans);
	}
	return 0;
}