QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#90381#5830. 树lmeowdn20 2159ms733304kbC++142.6kb2023-03-22 20:35:322023-03-22 20:35:35

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-22 20:35:35]
  • 评测
  • 测评结果:20
  • 用时:2159ms
  • 内存:733304kb
  • [2023-03-22 20:35:32]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define fi first
#define se second
#define eb emplace_back
#define popc __builtin_popcount
#define clz __builtin_clz
#define ctz __builtin_ctz
#define sgn(x) ((x)&1?-1:1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tiii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef unsigned long long ull;
typedef long double ld;
 
int read() {
	int x=0,w=1; char c=getchar(); 
	while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
	while(isdigit(c)) {x=x*10+c-'0'; c=getchar();}
	return x*w;
}

const int N=1e6+9;

int n,m,tick,yc[N],c[N],fa[N],h[N],len[N],son[N],id[N],s[N][33],f[N][29],ans[N],top[N],bl[N];
vi e[N];
vp q[N];

void dfs1(int u) {
	for(int v:e[u]) {
		dfs1(v);
		if(h[v]+1>h[u]) h[u]=h[v]+1, son[u]=v;
	}
}
void dfs2(int u) {
	if(!top[u]) top[u]=u;
	id[u]=++tick; if(son[u]) top[son[u]]=top[u], dfs2(son[u]);
	for(int v:e[u]) if(v!=son[u]) top[v]=v, dfs2(v);
}

signed main() {
	n=read();
	rep(i,1,n) yc[i]=read();
	rep(i,2,n) fa[i]=read(), e[fa[i]].eb(i);
	dfs1(1), dfs2(1);
	rep(i,1,n) e[i].clear();
	rep(i,1,n) if(i!=son[fa[i]]) e[id[fa[i]]].eb(id[i]);
	rep(i,1,n) len[id[i]]=h[i]+1, c[id[i]]=yc[i], bl[id[i]]=top[i];
	m=read();
	rep(i,1,m) {
		int u=read(), k=read(), x=id[u];
		q[x].eb(k+1,i);
	}
	per(i,n,1) {
		rep(h,0,30) {
			if((c[i]>>h)&1) s[i][h]=(bl[i+1]==bl[i]?s[i+1][h]:0)-1;
			else s[i][h]=(bl[i+1]==bl[i]?s[i+1][h]:0)+1;
		}
	}
	per(i,n,1) {
		//cout<<"WORK "<<i<<endl;
		f[i][0]=c[i];
		//cout<<i<<" "<<len[i]<<" "; puts("");
		for(int j:e[i]) {
			//cout<<"FUCK\n";
			rep(k,1,len[j]) rep(h,0,20) f[i+k][h]+=f[j+k-1][h];
			rep(k,1,len[j]) rep(h,0,30) s[i+k][h]+=s[j+k-1][h];
			rep(h,0,30) s[i][h]+=s[j][h];
		}
		rep(h,1,20) if((1<<h)<=len[i]) {
			int ad=s[i+(1<<h-1)][h-1]-(bl[i+(1<<h)]==bl[i]?s[i+(1<<h)][h-1]:0);
			f[i][h]=f[i][h-1]+f[i+(1<<h-1)][h-1]+ad*(1<<h-1);
			//cout<<i<<" "<<h<<" "<<f[i][h]<<" "<<f[i+(1<<h-1)][h-1]<<" "<<ad<<endl;
		} else f[i][h]=f[i][h-1];
		for(auto [k,id]:q[i]) {
			//cout<<"QIUERY "<<endl;
			k=min(k,len[i]); int cur=0, j=i;
			per(h,20,0) if(j+(1<<h)<=i+k) {
				//cout<<h<<" "<<f[j][h]<<endl;
				ans[id]+=f[j][h]; int tc=cur;
				while(tc) {
					int x=ctz(tc); tc^=(1<<x);
					int ad=s[j][x]-(bl[j+(1<<h)]==bl[j]?s[j+(1<<h)][x]:0);
					//cout<<"	"<<ad<<" "<<s[j+(1<<h)-1][x]<<" "<<s[j-1][x]<<endl;
					ans[id]+=ad*(1<<x);
				} j+=(1<<h), cur+=(1<<h);
			}
		}
	}
	rep(i,1,m) printf("%lld\n",ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 8ms
memory: 51680kb

input:

2000
946347867 341162491 202762650 295215762 254064439 66267204 693162580 357612557 492940637 939526638 59775103 374919339 144042807 861369313 651043728 999024805 439554916 167782038 597475252 56704409 69846137 22185655 79439847 769194737 145071391 226046618 915359433 392527325 84982946 54584098 827...

output:

31428191911
558747809
12352523
750054023682
89652837622
585197158
1628586765
922104954
8179034000
54544278609
1832814618
1719999173
56509757478
2418952901
116961311603
85980152161
387569167282
1458062157
135519738068
708763390
974312741
22160481380
541384937
109109437764
755479297
465075002
51201629...

result:

wrong answer 1st numbers differ - expected: '31727996563', found: '31428191911'

Test #2:

score: 0
Wrong Answer
time: 109ms
memory: 113152kb

input:

99999
792064428 473106195 799314986 65440734 948726345 442414256 280245405 873012700 466192412 899092866 283555341 657824017 963883713 793944180 767444438 105576842 542107696 580140098 65321660 381184238 584604194 397414881 861590124 309323011 217641157 120832524 303744205 961590116 110259426 380351...

output:

2509771019
101553245067
528899856
125427731
1953894159
2395530466227
4890787446669
586671581
3891261424483
798898621
468901916
1620113250
25990778
1594379152
3212684887662
29666259309277
1721547881
1491500623
2520645821705
14831010408938
263989598
1350176621
1430509596096
727716410
2174007370094
184...

result:

wrong answer 2nd numbers differ - expected: '102808896745', found: '101553245067'

Test #3:

score: 10
Accepted
time: 2159ms
memory: 733304kb

input:

1000000
947727010 179844140 923847675 171881267 5552129 974443359 989307850 869400987 126992154 527448411 141137718 136124474 917813105 392020809 79012342 473860575 969007624 833563354 90169336 878445705 84352622 403307122 733751738 670851448 942399068 731541999 101293644 545785337 964751520 9168003...

output:

322288180595345
64144900444023
131621992874362
185914449206495
4835864536999
52498320047300
91107191167946
133549265294890
287851078359140
123171067016186
128593134440538
8701280311335
14021811618693
65043919486113
310510908841731
15605852792829
75072265994522
73630260235120
66821009550818
217424650...

result:

ok 1000000 numbers

Test #4:

score: 10
Accepted
time: 2059ms
memory: 733264kb

input:

1000000
264862971 751386013 921867736 711577153 262726588 565608444 975324815 440219681 107888226 928241413 729126923 283912914 86248857 896446999 12839598 651796991 139813366 105131395 341646170 839485925 939265720 844548518 102280410 457829889 8602879 737140565 17206920 974175632 535833885 8373832...

output:

1437301063221
97809309869545
338628270305337
233680181746714
7398781587300
271306073103235
20877473367084
90775179215463
2535173725689
66604505519276
211534325021331
84757341592614
17886216279466
132051595671397
1860377236584
46253790496717
83950882453932
257274371637175
240943229927301
118446604805...

result:

ok 1000000 numbers

Test #5:

score: 0
Wrong Answer
time: 1142ms
memory: 673272kb

input:

1000000
978606419 773027389 111265179 979476504 280718851 476857501 751854950 579263969 848569548 781051974 31782627 533831186 812822170 111553645 297770650 331144396 676977983 2236128 258203325 75591120 676466973 60056446 494411414 286185093 92474576 173276071 535648669 87210101 355790411 880267291...

output:

2258826661
938786622
1803221827
19726432892
11943681463
2783437365
15861855784
2720451454
1859550937
476794111
986063591
1579646962
14140813386
38176990
781099105
527479272
2392930134
2519423500
1620138021
23553905555
5107065070
6903424070
1206547828
17830051052
49589751
499629803
4275328269
1379311...

result:

wrong answer 4th numbers differ - expected: '20412967786', found: '19726432892'

Test #6:

score: 0
Wrong Answer
time: 1202ms
memory: 674288kb

input:

1000000
952470566 585754087 120174600 401525004 458588768 5487567 31210348 446333263 231409083 521960132 457721893 866842852 925207283 16805978 4706826 99640835 619272676 136536623 459247161 308807462 633687300 717271369 23906473 865522890 173799280 424309108 719410673 118906385 110627845 730629403 ...

output:

994051214
904057442
2995278795
3850025527537
34330846595774
976251948
369701890
43760580
1510389651
3077329408
64314525360261
716198149
190685967
102049156220040
13375040526050
557364187
1084839575
3104092
276690929
95489843718461
467852673
33049527
425554649
22123421221998
94852903953107
426018723
...

result:

wrong answer 4th numbers differ - expected: '3980490219156', found: '3850025527537'

Test #7:

score: 0
Wrong Answer
time: 1273ms
memory: 677172kb

input:

1000000
732367509 105027907 958920212 886798715 102486738 813075884 301085392 242303497 979657287 944859684 307768 438158233 561755409 740706505 791145209 283862713 828081846 771569552 59044985 600549571 191330226 438693570 36976319 810654215 220068818 771875421 740642902 839964155 206129566 2065543...

output:

999908753
401770479
198184534828242
170121931881780
55312766374353
104556123916880
1146711843
97869157575389
2910590128
4981801495
1079679207
2140524944
75514756
9126833291826
72018517700417
167205592
56528298118687
1085743066
50318880027994
366357028550746
96252166119690
990736484
4798942062
361846...

result:

wrong answer 3rd numbers differ - expected: '206278425790162', found: '198184534828242'

Test #8:

score: 0
Wrong Answer
time: 1179ms
memory: 676048kb

input:

1000000
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 ...

output:

1534077773
1951493092
9972941
27
2883665952
25768883
0
133202688
23929
1
3
1
1
1
1
0
-7892367
1
0
1234891124
8
0
0
3
0
127169644
1
0
0
0
0
170659876
276780218
0
121455
-432787177
10
14225295
14748039
1
1
11534201
205822404
3
1056526655
0
-1177159744
2
1
3
7438722939
6
2471603999
0
7301145375
3774318...

result:

wrong answer 1st numbers differ - expected: '3826404725', found: '1534077773'

Test #9:

score: 0
Wrong Answer
time: 1417ms
memory: 677452kb

input:

1000000
465660691 982007525 816592310 377030959 572981469 679249520 86377999 709561525 940473306 35102782 886143915 792819787 903287397 264564177 857982095 91486434 217197704 123118964 383387342 820268798 497623987 255010796 607884194 848568529 38169627 197987657 421323589 664004905 485409127 696844...

output:

93815969031
8007323138529
80452546454206
33197437489829
275400654
1384234653
64716568
11407248918750
9818991313588
257344953691
15143296144205
528398853
1456723425
1214744688
12569010553116
23475037
27593388777438
245789356
109738951
47123447348349
78249949933418
102619945
17095478887850
1556076353
...

result:

wrong answer 1st numbers differ - expected: '96094116015', found: '93815969031'

Test #10:

score: 0
Wrong Answer
time: 1408ms
memory: 679624kb

input:

1000000
665830082 788228483 245541444 289601309 641764988 150723484 925214020 557415731 310210969 379707835 517820381 883917428 134445288 775557009 444476671 89856268 655841087 888410254 37788122 694551869 563331754 488108584 839551943 415095075 445425438 35452604 562044723 640544531 146258096 66852...

output:

431856043
520373616
75230587471625
6856706249064
61253165129235
13401490318523
762015332
4830867800606
1184478776
594507511
2612939894957
24897029063262
306250439
62500137
72787192367505
786078000
30471066191771
4162775187
4836689669018
877965179
18054999952634
1013123764
8686123221313
5512782583331...

result:

wrong answer 3rd numbers differ - expected: '77562434895287', found: '75230587471625'