QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#542374#8133. When Anton Saw This Task He Reacted With 😩PhantomThresholdTL 4000ms76796kbC++205.3kb2024-09-01 00:56:162024-09-01 00:56:17

Judging History

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

  • [2024-09-01 00:56:17]
  • 评测
  • 测评结果:TL
  • 用时:4000ms
  • 内存:76796kb
  • [2024-09-01 00:56:16]
  • 提交

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]].x,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)
				{
					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<<' '<<tt[j]<<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: 1ms
memory: 3576kb

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: 0
Accepted
time: 3559ms
memory: 76796kb

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:

393120558 773766615 387297348
759959566 981774500 -870231856
-703632542 -18232745 533642029
-593864779 -252947501 -944750793
-593678852 -169374593 -920223197
-405749495 -350493049 -116816620
-808225886 -483001218 -480063798
628180500 509984554 257430135
-984507108 352087791 917410487
932051309 -6316...

result:

ok 300000 numbers

Test #3:

score: 0
Accepted
time: 3775ms
memory: 76776kb

input:

199999 100000
x 154525 80092
v 450407262 725458410 590777520
x 24720 135901
v 719242117 114943104 186796011
v 382530926 89156744 943939337
x 183376 26042
x 109984 157873
x 151637 150600
x 4115 27454
x 163135 92648
x 16764 33212
v 849210403 945083972 689295397
v 471196117 68587428 225597765
x 24643 5...

output:

-321176892 -1730057 -549077502
810403092 258196842 -144784620
-587488197 -744895835 912664471
-671109463 -478998570 922528759
-680876795 -461355816 506214109
-513490823 -119198571 772404948
422920052 152084658 -480903896
-997036451 -649457191 320821077
776293878 699474926 -287129823
-126385880 -5297...

result:

ok 300000 numbers

Test #4:

score: 0
Accepted
time: 4000ms
memory: 76752kb

input:

199999 100000
x 72889 193806
x 35339 33069
v 314802407 406980523 492377265
x 108307 60027
x 144922 140917
v 328481079 117663280 644171354
v 482028404 951751561 166221217
v 936461869 436114879 421819757
x 152919 99965
x 61168 150607
v 56403640 743462679 134896557
v 24809217 462947115 966139991
v 7828...

output:

23709876 380448367 629159667
760678420 -458875163 -386466249
114926687 -344551438 -58366939
674199470 -693498618 544123803
953800112 -812226992 -949043816
327282782 -127242676 -704263640
588783157 502130649 190564297
-895563447 -4355337 -34765598
510012481 -892827456 -716473378
811082806 -631104535 ...

result:

ok 300000 numbers

Test #5:

score: -100
Time Limit Exceeded

input:

199999 100000
x 134204 79203
v 152855933 152545887 271660214
v 393332175 182708769 115884220
v 731792305 965028257 676889584
x 89631 14495
x 142016 85686
v 600051847 172947969 906920949
v 846126215 214556203 657929902
x 125721 133526
x 93179 35713
v 330716449 450417250 611411649
v 114397688 58644961...

output:

139597616 375474977 14619793
-108915893 79727434 363703631
-600893251 -120282751 -569198163
-409876008 819425899 502148739
-477665442 -811836281 -513870808
997888597 816534316 -659833074
334166269 288211584 -389991713
509280845 362972392 286415695
-634847393 -119363102 3658455
711270872 -903427822 -...

result: