QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#542355#8133. When Anton Saw This Task He Reacted With 😩PhantomThresholdWA 2801ms76728kbC++205.3kb2024-09-01 00:44:482024-09-01 00:44:48

Judging History

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

  • [2024-09-01 00:44:48]
  • 评测
  • 测评结果:WA
  • 用时:2801ms
  • 内存:76728kb
  • [2024-09-01 00:44:48]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;
const int MOD=998244353;

signed main()
{
	ios_base::sync_with_stdio(false);
	const int B=600,maxd=17;
	struct vc
	{
		int x,y,z;
		vc operator+(const vc &k)const{return (vc){(x+k.x)%MOD,(y+k.y)%MOD,(z+k.z)%MOD};}
		vc operator-(const vc &k)const{return (vc){(x-k.x)%MOD,(y-k.y)%MOD,(z-k.z)%MOD};}
		vc operator*(const int k)const{return (vc){(x*k)%MOD,(y*k)%MOD,(z*k)%MOD};}
		vc operator*(const vc &k)const{return (vc){(y*k.z-z*k.y)%MOD,(z*k.x-x*k.z)%MOD,(x*k.y-y*k.x)%MOD};}
//		vc operator^(const vc &k)const{return (vc){(x*k.x)%MOD,(y*k.y)%MOD,(z*k.z)%MOD};}
	};
	int n,q;
	
	cin>>n>>q;
	vector<vc> a(n+5);
	vector<int> L(n+5),R(n+5),pa(n+5);
	vector<vector<int>> jmp(maxd+5,vector<int>(n+5));
	for(int i=1;i<=n;i++)
	{
		string ty;
		cin>>ty;
		if(ty=="x")
		{
			cin>>L[i]>>R[i];
			pa[L[i]]=i;pa[R[i]]=i;
		}
		else
		{
			cin>>a[i].x>>a[i].y>>a[i].z;
		}
	}
	vector<pair<int,vc>> mods(q+5);
	for(int i=1;i<=q;i++)
	{
		int pos;
		vc d;
		cin>>pos>>d.x>>d.y>>d.z;
		mods[i]=make_pair(pos,d);
	}
	vector<int> dfn(n+5),dfne(n+5),dep(n+5);
	int idx=0;
	function<void(int)> dfs0=[&](int x)
	{
		dfn[x]=++idx;
		if(L[x])
		{
			jmp[0][L[x]]=jmp[0][R[x]]=x;
			dep[L[x]]=dep[R[x]]=dep[x]+1;
			dfs0(L[x]);
			dfs0(R[x]);
		}
		dfne[x]=idx;
	};
	dep[1]=1;
	dfs0(1);
	for(int d=1;d<=maxd;d++)
	{
		for(int i=1;i<=n;i++)
			jmp[d][i]=jmp[d-1][jmp[d-1][i]];
	}
	
	auto lca=[&](int x,int y)
	{
		if(dep[x]>dep[y])swap(x,y);
		for(int d=maxd;d>=0;d--)
		{
			if(dep[y]-(1<<d)>=dep[x])
				y=jmp[d][y];
		}
		if(x==y)return x;
		for(int d=maxd;d>=0;d--)
		{
			if(jmp[d][x]!=jmp[d][y])
				x=jmp[d][x],y=jmp[d][y];
		}
		return jmp[0][x];
	};
	auto kthp=[&](int x,int k)
	{
		for(int d=maxd;d>=0;d--)
		{
			if((k>>d)&1)
				x=jmp[d][x];
		}
		return x;
	};
//	double dt1=0,dt2=0,dt3=0;
	const vc X=(vc){1,0,0},Y=(vc){0,1,0},Z=(vc){0,0,1};
	for(int l=1;l<=q;l+=B)
	{
		vector<int> gop(n+5),gopm(n+5),sp(n+5),csp(n+5);
		vector<int> tt(n+5);
		vector<vc> cfx(n+5),cfy(n+5),cfz(n+5);
		
		auto apply=[&](int u,const vc &tx)
		{
			return (vc){(cfx[u].x*tx.x+cfy[u].x*tx.y+cfz[u].x*tx.z)%MOD,(cfx[u].y*tx.x+cfy[u].y*tx.y+cfz[u].y*tx.z)%MOD,(cfx[u].z*tx.x+cfy[u].z*tx.y+cfz[u].z*tx.z)%MOD};
		};
		
		int r=min(q,l+B-1);
		vector<int> tmp,tmp2;
		tmp2.push_back(1);
		for(int i=l;i<=r;i++)
		{
			tmp2.push_back(mods[i].first);
		}
		sort(tmp2.begin(),tmp2.end(),[&](int x,int y){return dfn[x]<dfn[y];});
		tmp=tmp2;
		for(int i=0;i+1<(int)tmp2.size();i++)
		{
			tmp.push_back(lca(tmp2[i],tmp2[i+1]));
		}
		sort(tmp.begin(),tmp.end(),[&](int x,int y){return dfn[x]<dfn[y];});
		tmp.erase(unique(tmp.begin(),tmp.end()),tmp.end());
		stack<int> s;
		for(auto z:tmp)
		{
			sp[z]=1;
			if(s.empty())s.push(z);
			else
			{
				int pp=s.top();
				while(not(dfn[pp]<=dfn[z] and dfne[z]<=dfne[pp]))
				{
					s.pop();
					pp=s.top();
				}
				gop[z]=pp;
				gopm[z]=kthp(z,dep[z]-dep[gop[z]]-1);
				s.push(z);
			}
		}
		function<void(int)> dfs1=[&](int u)
		{
			csp[u]=sp[u];
			if(L[u])
			{
				dfs1(L[u]);
				dfs1(R[u]);
				if(csp[L[u]] or csp[R[u]])
					csp[u]=1;
				a[u]=a[L[u]]*a[R[u]];
			}
		};
		function<void(int)> dfs2=[&](int u)
		{
			if(sp[pa[u]])
			{
				cfx[u]=X;
				cfy[u]=Y;
				cfz[u]=Z;
			}
			if(not sp[u] and csp[u])
			{
				if(csp[L[u]])
				{
					//cerr<<"?? "<<u<<' '<<(X*a[R[u]]).x<<' '<<(X*a[R[u]]).y<<' '<<(X*a[R[u]]).z<<endl;
					
					//(y*k.z-z*k.y),(z*k.x-x*k.z),(x*k.y-y*k.x)
					cfx[L[u]]=apply(u,{0,-a[R[u]].z,a[R[u]].y});
					cfy[L[u]]=apply(u,{a[R[u]].z,0,-a[R[u]].x});
					cfz[L[u]]=apply(u,{-a[R[u]].y,a[R[u]].z,0});
				}
				else
				{
					cfx[R[u]]=apply(u,{0,a[L[u]].z,-a[L[u]].y});
					cfy[R[u]]=apply(u,{-a[L[u]].z,0,a[L[u]].x});
					cfz[R[u]]=apply(u,{a[L[u]].y,-a[L[u]].x,0});
				}
			}
			if(L[u])
			{
				dfs2(L[u]);
				dfs2(R[u]);
			}
		};
//		time_t start=clock();
		dfs1(1);
//		dt1+=1.0*(clock()-start)/CLOCKS_PER_SEC;
//		start=clock();
		dfs2(1);
//		dt2+=1.0*(clock()-start)/CLOCKS_PER_SEC;
//		start=clock();
		/*
		cerr<<"---\n";
		for(int j=1;j<=n;j++)
			cerr<<a[j].x<<' '<<a[j].y<<' '<<a[j].z<<endl;
		for(int j=1;j<=n;j++)
			cerr<<cfx[j].x<<' '<<cfx[j].y<<' '<<cfx[j].z<<endl;
		for(int j=1;j<=n;j++)
			cerr<<cfy[j].x<<' '<<cfy[j].y<<' '<<cfy[j].z<<endl;
		for(int j=1;j<=n;j++)
			cerr<<cfz[j].x<<' '<<cfz[j].y<<' '<<cfz[j].z<<endl;
		cerr<<"---\n";
		for(auto z:tmp)cerr<<z<<' '<<gopm[z]<<' '<<gop[z]<<endl;
		cerr<<endl;
		*/
		
		reverse(tmp.begin(),tmp.end());
		for(int i=l;i<=r;i++)
		{
			a[mods[i].first]=mods[i].second;
			tt[mods[i].first]=i;
			for(auto z:tmp)
			{
				if(L[z])
				{
					if(tt[L[z]]==i or tt[R[z]]==i)
					{
						a[z]=a[L[z]]*a[R[z]];
						tt[z]=i;
					}
				}
				if(gopm[z]!=z and tt[z]==i)
				{
//					cerr<<z<<' '<<tmp3.x<<' '<<tmp3.y<<' '<<tmp3.z<<endl;
					a[gopm[z]]=apply(z,a[z]);
					tt[gopm[z]]=i;
				}
			}
			
			cout<<a[1].x<<' '<<a[1].y<<' '<<a[1].z<<"\n";
			/*
			cerr<<"---\n";
			for(int j=1;j<=n;j++)
				cerr<<a[j].x<<' '<<a[j].y<<' '<<a[j].z<<endl;
			cerr<<"---\n";
			*/
		}
//		dt3+=1.0*(clock()-start)/CLOCKS_PER_SEC;
	}
	
//	cerr<<clock()<<endl;
//	cerr<<dt1<<' '<<dt2<<' '<<dt3<<endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3832kb

input:

5 3
x 2 3
v 1 0 1
x 4 5
v 0 2 1
v 1 1 1
4 1 2 3
5 0 1 1
4 0 2 2

output:

-2 0 2
1 -2 -1
0 0 0

result:

ok 9 numbers

Test #2:

score: -100
Wrong Answer
time: 2801ms
memory: 76728kb

input:

199999 100000
x 137025 65661
v 572518668 158967010 74946561
x 129836 192657
x 141948 187810
v 574918069 328924434 141474729
x 143312 111002
x 52772 148497
v 922857701 690080961 651915759
v 656198340 28002884 129579416
v 639893144 265359784 646791226
v 796871409 411409966 598676495
v 882562617 224394...

output:

-883060969 -569140098 750343644
380211686 489946179 -184391312
-366700998 -167223180 -590712160
449183018 441516373 -483608155
-424852568 -376426432 -843214464
-340395440 867160954 -799340899
-14105526 479693783 -231315935
-52665025 -481179582 -659655945
-631386237 -816181630 631410119
863616967 494...

result:

wrong answer 1st numbers differ by non-multiple of MOD, - expected: '393120558', found: '-883060969'