QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#182872#7045. XOR TreePetroTarnavskyiAC ✓2442ms385252kbC++172.7kb2023-09-18 17:44:012023-09-18 17:44:02

Judging History

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

  • [2023-09-18 17:44:02]
  • 评测
  • 测评结果:AC
  • 用时:2442ms
  • 内存:385252kb
  • [2023-09-18 17:44:01]
  • 提交

answer

#include <vector>
#include <iostream>
#include <cstring>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FILL(a, b) memset(a, b, sizeof(a))
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef unsigned long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int N = 100447;
const int B = 30;

struct node
{
	int cnt;
	int b[B];
	int m[B][B];
	
	node() {}
	node(int x)
	{
		FILL(b, 0);
		FILL(m, 0);
		cnt = 1;
		FOR (i, 0, B)
		{
			int bt = (x >> i) & 1;
			if (bt == 0) continue;
			b[i]++;
			FOR (j, i + 1, B)
			{
				if (((x >> j) & 1) == 0) continue;
				m[i][j]++;
			}
		}
	}
	
	VI get1(int i)
	{
		return {b[i], cnt - b[i]};
	}
	
	VI get2(int i, int j)
	{
		int a0 = m[i][j];
		int a1 = b[i] - m[i][j];
		int a2 = b[j] - m[i][j];
		int a3 = cnt - a0 - a1 - a2;
		return {a0, a1, a2, a3};
	}
};

int a[N];
VI g[N];
VI del[N];
node val[N];
int n, k;
LL ans[N];

void dfs2(int v, VI& st)
{
	if (SZ(st) > k)
		del[st[SZ(st) - k - 1]].PB(v);
	st.PB(v);
	for (auto& to : g[v])
		dfs2(to, st);
	st.pop_back();
}

void merge(int v, int u)
{
	FOR (i, 0, B)
	{
		FOR (j, i + 1, B)
		{
			VI t1 = val[v].get2(i, j);
			VI t2 = val[u].get2(i, j);
			FOR (t, 0, 4)
				ans[v] += LL((1ll << (i + j + 1))) * t1[t] * t2[3 - t];
			val[v].m[i][j] += val[u].m[i][j];
		}
	}
	FOR (i, 0, B)
	{
		auto t1 = val[v].get1(i);
		auto t2 = val[u].get1(i);
		FOR (t, 0, 2)
			ans[v] += LL((1ll << (i * 2))) * t1[t] * t2[1 - t];
		val[v].b[i] += val[u].b[i];
	}
	val[v].cnt += val[u].cnt;
}

void erase(int v, int x)
{
	node tmp(x);
	
	val[v].cnt -= tmp.cnt;
	FOR (i, 0, B)
	{
		val[v].b[i] -= tmp.b[i];
		auto t1 = val[v].get1(i);
		auto t2 = tmp.get1(i);
		FOR (t, 0, 2)
			ans[v] -= LL((1ll << (i * 2))) * t1[t] * t2[1 - t];
	}
	FOR (i, 0, B)
	{
		FOR (j, i + 1, B)
		{
			val[v].m[i][j] -= tmp.m[i][j];
			VI t1 = val[v].get2(i, j);
			VI t2 = tmp.get2(i, j);
			FOR (t, 0, 4)
				ans[v] -= LL((1ll << (i + j + 1))) * t1[t] * t2[3 - t];
		}
	}
}


void dfs(int v)
{
	for (auto& to : g[v])
	{
		dfs(to);
		ans[v] += ans[to];
	}
	for (auto& to : g[v])
		merge(v, to);
	for (auto d : del[v])
		erase(v, a[d]);
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
		
	cin >> n >> k;
	
	FOR (i, 0, n) 
	{
		cin >> a[i];
		val[i] = node(a[i]);
	}
	
	FOR (i, 1, n)
	{
		int p;
		cin >> p;
		p--;
		g[p].PB(i);
	}
	VI st;
	dfs2(0, st);
	
	dfs(0);
	
	FOR (i, 0, n)
	{
		cout << ans[i] << '\n';
	}
	
	return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

86
98
0
0
4
0

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 10ms
memory: 11212kb

input:

500 50
393327425 751304819 230942599 559458196 463730189 323966066 445269312 651047281 840411866 359599246 305050356 77084171 817750542 938505384 467961771 187450694 832472869 388005758 610981616 644958519 816712665 988497027 348227527 106694696 636293112 217875090 408722909 63211622 736241151 76610...

output:

2484337175052561930
882956815531948810
1990290354187703220
2951108338243112542
7146296279649696070
8595506076130955810
9898692581349254122
12620105082907774521
6276027773445176816
1632820238819937125
7371320677776017260
9315757892603614610
14575883921859453174
5250428235567143810
6310709515080098492...

result:

ok 500 lines

Test #3:

score: 0
Accepted
time: 697ms
memory: 122664kb

input:

30000 3000
416189612 854085620 995232508 477116625 193936517 566101971 399043044 455751058 386513988 110470384 315774823 454201323 529613755 413253898 652443502 512275061 919556828 172076089 215832587 510075089 447664104 919474653 883448596 513147625 209784326 797440175 324985774 773933455 415212063...

output:

18057725148092092276
428618770514934899
2770956013599764048
14974779386150325552
1667188071420813090
10441555585508231290
10751760586752681724
18021604519623468929
4988347366418241634
3146161409882639404
13660046833129175896
6853828753100854932
6030564660542789579
4853803524065935816
154084277535820...

result:

ok 30000 lines

Test #4:

score: 0
Accepted
time: 1117ms
memory: 196672kb

input:

50000 5000
870764930 297784082 7626254 774155359 354987034 676628134 322041789 922236503 686928311 426612229 824853841 589347739 395052021 792470686 138744612 52802660 73169312 263040597 187770583 742986567 523704740 627366535 838756882 22998267 629163877 131244056 305020729 718037131 277741327 4550...

output:

7272857077981900250
15099659068771747586
14522811799441314662
17353244824464266168
5540788197669751308
2524815842644662544
9554129543165610669
4172261795794618661
11672422559869373832
17962636975027944100
10775942809849063400
6865209196956923593
15057925902539703336
14912048126404018264
348709397415...

result:

ok 50000 lines

Test #5:

score: 0
Accepted
time: 2442ms
memory: 385252kb

input:

100000 10
217535440 668041289 916326409 877679552 79139917 968120527 218183796 366237103 936590891 679253254 767087496 35261544 575365938 20069419 602633889 76393323 274817269 892275177 736442209 796284359 452525110 545356269 929568098 399248445 727298294 527293096 663541286 946091407 987197352 2720...

output:

8628164072562729054
9339286594783226830
9722416538737835002
16242117431354719584
14496544905222790521
15080670853656017377
13975564280201834297
10579353213447368460
9807801091922615344
8335927720284008664
10451631268230266904
9629639812140279536
11442347387760128044
8644024026907888068
9045689223807...

result:

ok 100000 lines

Test #6:

score: 0
Accepted
time: 2386ms
memory: 384440kb

input:

100000 50
460486087 340916464 65423953 735425636 287196174 35389184 610444272 718449910 570837167 105661028 120489129 959617371 411108967 243911428 857129432 571931319 82184180 487897920 723731336 927583545 408396108 900414143 239951850 211956693 54446699 946641378 872492528 485183542 745221299 3290...

output:

17968009078719101884
15357724600859613644
15885878391749066081
6632112191949845750
1383902874457886198
926663333166635414
2492800651177099538
17619839142489669686
3043317379937375710
10433326635853929662
14310071667386178380
15713577313796793260
12676505269147543776
17171151624926660596
528696653896...

result:

ok 100000 lines

Test #7:

score: 0
Accepted
time: 2422ms
memory: 383976kb

input:

100000 233
136493569 459760729 551422896 584644782 656600 848845767 665624793 417678416 918080676 831553956 877175397 605710551 514672905 855597792 514186029 548129196 642934060 418318406 341291741 476305088 284191455 840794677 57618451 11228823 986166753 916969458 632080803 880130791 423046835 7142...

output:

9961667212831636322
12775690836695400823
11301407955374626064
15884171922304610065
12400191213547458008
2373643094625015000
16025754547867978990
10325454678915570574
90473168284413646
4475863937732652493
5413065815933576012
11264215929441612035
1017047449087902195
2852161309857904300
152367007170325...

result:

ok 100000 lines

Test #8:

score: 0
Accepted
time: 2295ms
memory: 383592kb

input:

100000 7653
77779934 227963726 638214101 399233975 411912261 603216207 117485973 65728037 378714539 357942582 339885543 435933671 693026865 516110193 603639852 918925180 296283251 943078558 527144895 654403512 173710193 95589758 980554073 767407727 40025234 104447667 190531715 110864674 389766231 28...

output:

3307312356264045706
14874592074333075909
5996635450288450485
14194634542252335584
6148217449123074016
14431607782681977536
1919819350656677025
4400558466216659313
3936940638753856634
13147472119668827741
15447398510409411781
3502759633117113382
1937397500830042321
1816038206662161206
181904940382818...

result:

ok 100000 lines

Test #9:

score: 0
Accepted
time: 1913ms
memory: 382876kb

input:

100000 32135
672840933 118377636 371375523 641832662 850420243 112205191 791299778 234757252 838089753 562356867 712777625 794309233 676212792 580334739 432086225 657488514 697523881 303571391 543372920 572801306 926697093 341214278 214711154 33617848 960764124 432012669 411488444 877336355 81552994...

output:

12793232208625928111
14327555704638209142
6269733831463827398
6242512632017086727
17929270268635168986
17123696590254195384
10757352362720144624
11340040824798936120
201379829960496113
6665001071674189844
10422098044969734684
5056075788642753228
13809478561578038484
8754781422226413228
1027071960946...

result:

ok 100000 lines

Test #10:

score: 0
Accepted
time: 1372ms
memory: 384928kb

input:

100000 85555
361417381 636731807 228647435 827956271 809136192 385081646 434050376 329290680 528337363 547832744 337481047 185625389 953265245 176153488 322138641 86126763 188485380 964309137 933910603 74343826 802497833 977105692 373624999 152034604 733296793 221041443 950066373 784299949 712760054...

output:

4619882817926842478
13959335715362393545
13409901843907449352
8266342009617255197
8027320574283346234
7793723821400591786
453324073377786218
502532483741763514
2901404726897247280
13937362726960663424
13921333292070783660
1989003908080744495
7705096603319269628
17993606047309186511
16942503457083217...

result:

ok 100000 lines