QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#182874#7045. XOR TreePetroTarnavskyiML 1201ms315212kbC++142.7kb2023-09-18 17:45:402023-09-18 17:45:42

Judging History

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

  • [2023-09-18 17:45:42]
  • 评测
  • 测评结果:ML
  • 用时:1201ms
  • 内存:315212kb
  • [2023-09-18 17:45:40]
  • 提交

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)
{
	{
        node x(a[v]);
		swap(val[v], x);
	}
	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];
	
	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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 11736kb

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: 15ms
memory: 14028kb

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: 742ms
memory: 187940kb

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: 1201ms
memory: 315212kb

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: -100
Memory Limit Exceeded

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: