QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#104534#6322. ForestryHzweiAC ✓1060ms424940kbC++149.4kb2023-05-10 23:16:152023-05-10 23:16:17

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-10 23:16:17]
  • 评测
  • 测评结果:AC
  • 用时:1060ms
  • 内存:424940kb
  • [2023-05-10 23:16:15]
  • 提交

answer

// Words are flowing out like endless rain into a paper cup,
// They slither while they pass, they slip away across the universe
// Pools of sorrow, waves of joy are drifting through my opened mind,
// Possessing and caressing me.
// Jai guru de va om
// Nothing's gonna change my world,
// Nothing's gonna change my world,
//
// Nothing's gonna change my world,
// Nothing's gonna change my world,
//
// Images of broken light which dance before me like a million eyes,
// That call me on and on across the universe,
// Thoughts meander like a restless wind inside a letter box they
// Tumble blindly as they make their way
// Across the universe
// Jai guru de va om
// Nothing's gonna change my world,
// Nothing's gonna change my world,
//
// Nothing's gonna change my world,
// Nothing's gonna change my world,
//
// Sounds of laughter shades of earth are ringing
// Through my open views inciting and inviting me
// Limitless undying love which shines around me like a
// Million suns, it calls me on and on
// Across the universe
// Jai guru de va om
// Nothing's gonna change my world,
// Nothing's gonna change my world,
//
// Nothing's gonna change my world,
// Nothing's gonna change my world,
//
// Jai guru de va

#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC target("avx,avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("Ofast,fast-math")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#include<iostream>
//#include<string>
//#include<cmath>
//#include<cstdio>
//#include<cctype>
//#include<cstring>
//#include<iomanip>
//#include<cstdlib>
//#include<ctime>
//#include<set>
//#include<map>
//#include<utility>
//#include<queue>
//#include<vector>
//#include<stack>
//#include<sstream>
//#include<algorithm>
using namespace std;
/*=====================================================================*/
#define ll long long
#define int ll
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define pdd pair<double,double>
#define ull unsigned long long
#define pb push_back
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(s) (s).begin(),(s).end()
#define repd(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define forr(i,a,b,c) for(int i=(int)(a);i<=(int)(b);i+=(int)(c))
#define forn(i,p,n) for(int i=(int)(p);i<=(int)(n);++i)
#define ford(i,p,n) for(int i=(int)(n);i>=(int)(p);--i)
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();++i)
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define Endl(x) cout<<x<<endl;
#define Blank(x) cout<<x<<" ";
#define modcg(x) if(x>=mod)x-=mod;
#define modcl(x) if(x<0)x+=mod;
#define lowbit(x) x&(-x)
/*=====================================================================*/
string int_to_string(ll n)
{
	string s="";
	while(n)
	{
		ll now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
ll string_to_int(string s)
{
	ll n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
mt19937 GeN(chrono::system_clock::now().time_since_epoch().count());
int Rand(int l,int r)
{
	uniform_int_distribution<>RAND1(l,r);
	return RAND1(GeN);
}
struct Fastmod
{
	int mod,b;
	typedef __int128 lll;
	void init(int m)
	{
		mod=m;
		b=((lll)1<<64)/mod;
	}
	int operator()(ull a)
	{
		int q=((lll)a*b)>>64,r=a-q*mod;
		modcg(r);
		return r;
	}
}MOD;
int mul(int a,int b)
{
	return MOD(a*b);
}
/*======================================================================*/
const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};
const int month[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
//think twice,code once
//think once,debug forever
const int MAXN=3e5+10,mod=998244353,inv2=499122177;
struct SegT
{
	struct node
	{
		int ls=0,rs=0;
		int val1=0,val2=0;
		int tag=1;
	}seg[MAXN<<5];
	int head;
	void push_up(int now)
	{
		seg[now].val1=seg[seg[now].ls].val1+seg[seg[now].rs].val1;modcg(seg[now].val1);
		seg[now].val2=seg[seg[now].ls].val2+seg[seg[now].rs].val2;modcg(seg[now].val2);
	}
	void update(int &now,int l,int r,int down,int val)
	{
		if(!now)
		{
			now=++head;
		}
		if(l==r)
		{
			seg[now].val1+=val;
			modcg(seg[now].val1);
//			cout<<val<<" "<<down<<endl;
			seg[now].val2+=mul(val,down);
			modcg(seg[now].val2);
//			cout<<seg[now].val2<<endl;
			return;
		}
		int mid=(l+r)>>1;
		if(down<=mid)
		{
			update(seg[now].ls,l,mid,down,val);
		}
		else
		{
			update(seg[now].rs,mid+1,r,down,val);
		}
		push_up(now);
	}
	int qurey(int now,int l,int r,int down)
	{
		if(l==r)
		{
			return seg[now].val1;
		}
		int mid=(l+r)>>1;
		if(down<=mid)
		{
			return qurey(seg[now].ls,l,mid,down);
		}
		else
		{
			return qurey(seg[now].rs,mid+1,r,down);
		}
	} 
	void add_tag(int now,int val)
	{
		seg[now].tag=mul(seg[now].tag,val);
		seg[now].val1=mul(seg[now].val1,val);
		seg[now].val2=mul(seg[now].val2,val);
	}
	void push_down(int now)
	{
		if(seg[now].tag!=1)
		{
			if(seg[now].ls)
			{
				add_tag(seg[now].ls,seg[now].tag);
			}
			if(seg[now].rs)
			{
				add_tag(seg[now].rs,seg[now].tag);
			}
			seg[now].tag=1;
		}
	}
	int sum1=0,sum2=1;
	int merge(int x,int y,int l,int r)
	{
//		cout<<sum1<<" "<<sum2<<endl;
		if(!y)
		{
			sum1+=seg[x].val1;modcg(sum1);
//			cout<<x<<" "<<sum1<<" "<<seg[x].val1<<endl;
			add_tag(x,sum2);
			return x;
		}
		if(!x)
		{
			sum2+=seg[y].val1;modcg(sum2);
//			cout<<sum1<<endl;
			add_tag(y,sum1);
			return y;
		}
		if(l==r)
		{
//			cout<<x<<" "<<y<<endl;
//			int p=seg[x].val1,q=seg[y].val1;
//			seg[x].val1=mul(p,sum2)+mul(q,sum1)+mul(p,q);modcg(seg[x].val1);modcg(seg[x].val1); 
//			seg[x].val2=mul(seg[x].val1,l);
//			sum1+=p,sum2+=q;modcg(sum1);modcg(sum2);
			sum1+=seg[x].val1,sum2+=seg[y].val1;modcg(sum1);modcg(sum2);
			seg[x].val1=mul(seg[x].val1,sum2)+mul(seg[y].val1,sum1)-mul(seg[x].val1,seg[y].val1);modcg(seg[x].val1);modcl(seg[x].val1);
			seg[x].val2=mul(seg[x].val1,l);
			return x;
		}
		push_down(x),push_down(y);
		int mid=(l+r)>>1;
		seg[x].rs=merge(seg[x].rs,seg[y].rs,mid+1,r);
		seg[x].ls=merge(seg[x].ls,seg[y].ls,l,mid);
		push_up(x);
		return x;
	}
}T;
int n;
int a[MAXN];
int root[MAXN];
vi v[MAXN];
int p2[MAXN];
int ans=0;
int sz[MAXN];
void dfs(int now,int fa)
{
	T.update(root[now],1,1e9,a[now],1);
//	cout<<" "<<T.seg[root[now]].val1<<T.seg[root[now]].val2<<endl;
	sz[now]=1;
	for(int u:v[now])
	{
		if(u==fa)
		{
			continue;
		}
		dfs(u,now);
		sz[now]+=sz[u];
		T.sum1=0,T.sum2=p2[sz[u]-1];
		root[now]=T.merge(root[now],root[u],1,1e9);
	}
//	forn(i,1,n)
//	{
//		cout<<T.qurey(root[now],1,1e9,a[i])<<" ";
//	}
//	cout<<endl;
//	forn(i,1,T.head)
//	{
//		cout<<T.seg[i].ls<<" "<<T.seg[i].rs<<" "<<T.seg[i].val1<<" "<<T.seg[i].val2<<endl;
//	}
//	cout<<now<<" "<<root[now]<<endl;
//	cout<<now<<" "<<T.seg[root[now]].val1<<" "<<T.seg[root[now]].val2<<endl;
	ans+=mul(T.seg[root[now]].val2,p2[max(n-sz[now]-1,0ll)]);modcg(ans);
}
void solve()
{
	MOD.init(mod);
	cin>>n;
	p2[0]=1;
	forn(i,1,n)
	{
		cin>>a[i];
		p2[i]=p2[i-1]+p2[i-1];modcg(p2[i]);
	}
	rep(i,n-1)
	{
		int x,y;
		cin>>x>>y;
		v[x].pb(y);
		v[y].pb(x);
	}
	dfs(1,0);
	cout<<ans<<endl;
}
/*======================================================================*/
signed main()
{
    cin.tie(0);
    cout.tie(0);
	std::ios::sync_with_stdio(false);
//#define Hank2007
#ifdef Hank2007
	freopen("input.txt","r",stdin);
	freopen("output1.txt","w",stdout);
#endif
  	/*====================================================================*/
  	int TEST_CASE=1;
//	cin>>TEST_CASE;
  	while(TEST_CASE--)
  	{
  		solve();
  	}
  	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 88ms
memory: 385452kb

input:

4
1 2 3 4
1 2
2 4
3 2

output:

44

result:

ok 1 number(s): "44"

Test #2:

score: 0
Accepted
time: 114ms
memory: 386688kb

input:

5
3 5 6 5 1
4 1
2 3
3 5
1 3

output:

154

result:

ok 1 number(s): "154"

Test #3:

score: 0
Accepted
time: 797ms
memory: 404844kb

input:

278989
864394090 384799247 186936242 598547507 962916136 540758021 158527118 688236322 682301622 948660645 881768181 481432818 557201870 794956026 205262301 920739629 141926922 990361777 818811172 150579096 1032726 328924563 638044961 21740781 484574329 737977343 113738003 345289940 363021157 582495...

output:

434293031

result:

ok 1 number(s): "434293031"

Test #4:

score: 0
Accepted
time: 817ms
memory: 405296kb

input:

287397
510502405 166707542 6543103 963754822 492178400 668790842 47929394 688150962 147460907 412966210 837275339 435773111 138898710 968087621 746522431 948665731 920170470 717898411 411586421 148909638 659985934 484520123 95176393 425866444 512660652 511428439 958021520 150660147 292591135 7320851...

output:

878336347

result:

ok 1 number(s): "878336347"

Test #5:

score: 0
Accepted
time: 874ms
memory: 405696kb

input:

294100
74665954 206916315 273249696 386095808 903673441 622958780 983022845 464633239 86441267 729372644 689111719 644187811 334408092 398907453 597161881 558735093 189557855 477930470 242405983 95068877 16413783 608746253 761602925 18584327 934502624 975194906 309225637 343248456 653012994 23118382...

output:

712256085

result:

ok 1 number(s): "712256085"

Test #6:

score: 0
Accepted
time: 837ms
memory: 420692kb

input:

300000
606908308 171374011 595912591 940182632 431995844 726762958 256169397 447385236 984023043 528258150 35565776 27503639 983714715 313579533 8771592 967838717 206597371 821802152 910792624 238535529 923693575 376608809 519872194 981808980 991017798 212487460 780752406 913659452 464143746 1561438...

output:

847230777

result:

ok 1 number(s): "847230777"

Test #7:

score: 0
Accepted
time: 682ms
memory: 421340kb

input:

299999
782121216 968610368 25009662 478183585 274702257 773444727 687702634 507153995 474335571 202223165 211518854 313706139 936298699 536896304 702242243 590986189 912655919 596226813 737786290 917153157 990146015 268573292 418734072 3093396 94105534 834508206 491041607 939287910 88847044 81241220...

output:

41386982

result:

ok 1 number(s): "41386982"

Test #8:

score: 0
Accepted
time: 671ms
memory: 424940kb

input:

299999
471542727 799095206 509787451 592650193 362215487 588125595 604003616 881525850 755688267 236898505 532366496 67948692 232297132 929458994 431732351 274008699 164112665 690178118 571148044 408207186 453143828 959612302 878551481 584372849 894288979 552607930 610390281 409926978 15831307 61533...

output:

479741390

result:

ok 1 number(s): "479741390"

Test #9:

score: 0
Accepted
time: 629ms
memory: 421676kb

input:

299997
25665195 607081146 2312642 326822363 385524999 209349813 211617900 372403090 573561165 779096327 304738950 525680732 809703693 740379205 642786351 334319127 24588707 570042249 298675391 92525045 882182405 255387261 629708545 662677567 102298185 416640032 800837034 517838306 180083897 65300699...

output:

756252561

result:

ok 1 number(s): "756252561"

Test #10:

score: 0
Accepted
time: 610ms
memory: 420116kb

input:

299997
772601140 677850510 486293752 38381420 713140958 613588331 933478663 892594986 40965372 719820852 371692421 950213365 627891743 923082750 330248475 635152312 968671306 91954505 646645243 266673541 419892861 468231632 676898301 475369890 173394153 206234682 614218712 386714613 691986706 401543...

output:

909256779

result:

ok 1 number(s): "909256779"

Test #11:

score: 0
Accepted
time: 969ms
memory: 406392kb

input:

300000
489134030 28527536 706816680 898196399 298100900 573504624 817242924 202561288 93577170 123268581 618798194 660515267 184003900 797853604 19557769 572066362 795291782 626861802 147727524 877230625 221524908 456496916 339550982 405198767 983242265 794896971 192309413 938450729 890444371 624597...

output:

469866382

result:

ok 1 number(s): "469866382"

Test #12:

score: 0
Accepted
time: 1060ms
memory: 406112kb

input:

294048
940092908 294031597 203789774 965832063 294368312 530860757 29650208 19058325 908258800 103593701 42838422 924175435 729123020 53690401 645412066 388715095 613747643 989874415 525317950 992107991 110671090 174219963 334698216 629961584 722552204 684114784 157441276 972553266 734116607 6480471...

output:

869124246

result:

ok 1 number(s): "869124246"

Test #13:

score: 0
Accepted
time: 1001ms
memory: 406220kb

input:

294169
829764052 27629554 569458841 732262994 789440677 560991159 427506198 382533265 982599986 867072676 292427700 863455778 358352325 895409930 503249204 713052609 50315636 478399531 724940834 394303205 658023052 804043479 566808951 672917322 636437946 905749010 875678660 8659223 349546005 8117721...

output:

603293934

result:

ok 1 number(s): "603293934"

Test #14:

score: 0
Accepted
time: 943ms
memory: 404896kb

input:

274214
936267909 35123462 801828019 44784239 618602008 669685848 531693333 374691550 641907521 460643008 895242266 178217314 865256487 210105123 791635414 102380409 605115311 319289854 173186400 301848535 470317014 68928307 385895216 80699081 351459001 505538791 208284669 800278979 952086428 4182918...

output:

344857557

result:

ok 1 number(s): "344857557"

Test #15:

score: 0
Accepted
time: 980ms
memory: 406228kb

input:

300000
300000 299999 299998 299997 299996 299995 299994 299993 299992 299991 299990 299989 299988 299987 299986 299985 299984 299983 299982 299981 299980 299979 299978 299977 299976 299975 299974 299973 299972 299971 299970 299969 299968 299967 299966 299965 299964 299963 299962 299961 299960 299959...

output:

8100800

result:

ok 1 number(s): "8100800"