QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#667004#9492. 树上简单求和Galex17 4971ms85432kbC++235.4kb2024-10-22 20:44:142024-10-22 20:44:22

Judging History

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

  • [2024-10-22 20:44:22]
  • 评测
  • 测评结果:17
  • 用时:4971ms
  • 内存:85432kb
  • [2024-10-22 20:44:14]
  • 提交

answer

#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define pii pair<int, int>
#define pll pair<LL, LL>
#define fi first
#define se second
#define mkp make_pair
#define sz(x) (int)x.size()
using namespace std;
bool Mst;

LL read() {
	LL s = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
		f = (ch == '-' ? -1 : 1), ch = getchar();
	while (ch >= '0' && ch <= '9')
		s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
	return s * f;
}

ULL readull() {
	ULL s = 0; char ch = getchar();
	while (ch < '0' || ch > '9') ch = getchar();
	while (ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
	return s;
}

mt19937 rnd(114514);

const int MAXN = 200005;

int n, m;
ULL a[MAXN];
vector<int> e[MAXN];
int fa[MAXN][20], dep[MAXN];
int dfn[MAXN], tim = 0, rnk[MAXN];

void pre(int x, int fat) {
	fa[x][0] = fat, dep[x] = dep[fat] + 1;
	dfn[x] = ++tim, rnk[tim] = x;
	for (int y : e[x])
		if (y != fat)
			pre(y, x);
}

int mov(int u, int d) {
	for (int i = 18; i >= 0; i--)
		if ((1 << i) <= d)
			d -= (1 << i), u = fa[u][i];
	return u;
}

int lca(int u, int v) {
	dep[u] > dep[v] ? u = mov(u, dep[u] - dep[v]) : v = mov(v, dep[v] - dep[u]);
	if (u == v) return u;
	for (int i = 18; i >= 0; i--)
		if (fa[u][i] != fa[v][i])
			u = fa[u][i], v = fa[v][i];
	return fa[u][0];
}

namespace Blocker {
	const int MAXC = 505;
	int B, C;
	int L[MAXC], R[MAXC], bl[MAXN];
	ULL sum[MAXC], val[MAXN];
	void init() {
		B = sqrt(n), C = (n - 1) / B + 1;
		for (int i = 1; i <= n; i++)
			bl[i] = (i - 1) / B + 1, R[bl[i]] = i;
		for (int i = n; i; i--)
			L[bl[i]] = i;
	}
	void mdf(int x, ULL v) {
		for (int i = x; i <= R[bl[x]]; i++) val[i] += v;
		for (int i = bl[x]; i <= C; i++) sum[i] += v;
	}
	ULL qry(int x) { return val[x] + sum[bl[x] - 1];}
	ULL qry(int l, int r) {
		return qry(r) - qry(l - 1);
	}
}

namespace Tree1 {
	vector<int> e[MAXN];
	void add(int u, int v) {
		e[u].push_back(v);
		e[v].push_back(u);
	}
	int fa[MAXN][20], dep[MAXN], dfn[MAXN], rnk[MAXN], tim = 0, sz[MAXN];
	void pre(int x, int fat) {
		dfn[x] = ++tim, rnk[tim] = x, sz[x] = 1;
		fa[x][0] = fat, dep[x] = dep[fat] + 1;
		for (int y : e[x])
			if (y != fat)
				pre(y, x), sz[x] += sz[y];
	}
	int mov(int u, int d) {
		for (int i = 18; i >= 0; i--)
			if ((1 << i) <= d)
				d -= (1 << i), u = fa[u][i];
		return u;
	}
	int lca(int u, int v) {
		dep[u] > dep[v] ? u = mov(u, dep[u] - dep[v]) : v = mov(v, dep[v] - dep[u]);
		if (u == v) return u;
		for (int i = 18; i >= 0; i--)
			if (fa[u][i] != fa[v][i])
				u = fa[u][i], v = fa[v][i];
		return fa[u][0];
	}
	void pre() {
		pre(1, 0);
		for (int j = 1; j <= 18; j++)
			for (int i = 1; i <= n; i++)
				fa[i][j] = fa[fa[i][j - 1]][j - 1];
	}
	void mdf(int x, int y, int l, ULL k) {
		Blocker::mdf(dfn[x], k), Blocker::mdf(dfn[y], k);
		Blocker::mdf(dfn[l], -k), Blocker::mdf(dfn[fa[l][0]], -k);
	}
	ULL qry(int x) {
		return Blocker::qry(dfn[x], dfn[x] + sz[x] - 1) + a[x];
	}
	int sum[MAXN];
	void dfs2(int x, int fa) {
		sum[x] += sum[fa];
		for (int y : e[x])
			if (y != fa)
				dfs2(y, x);
	}
	int qry(int u, int v, int l) {
		return sum[u] + sum[v] - sum[l] - sum[fa[l][0]];
	}
}

int p[MAXN], cnt = 0;
bool in[MAXN];

void spread() {
	cnt = 200;
	for (int i = 1; i <= cnt; i++) p[i] = rnd() % n + 1;
	p[++cnt] = 1;
	sort(p + 1, p + cnt + 1, [](int x, int y) {return dfn[x] < dfn[y];});
	for (int i = 2, len = cnt; i <= len; i++) p[++cnt] = lca(p[i], p[i - 1]);
	sort(p + 1, p + cnt + 1, [](int x, int y) {return dfn[x] < dfn[y];});
	cnt = unique(p + 1, p + cnt + 1) - p - 1;
	for (int i = 1; i <= cnt; i++) in[p[i]] = 1;
}

int x[MAXN], y[MAXN], l1[MAXN];
int p1[MAXN], p2[MAXN], p3[MAXN];
ULL ans[MAXN], k[MAXN];

int qry(int u, int i, ULL v) {
	while (!in[u])
		ans[i] += v * Tree1::qry(u), u = fa[u][0];
	return u;
}

bool Med;
signed main() {
	// freopen("mist7-9.in", "r", stdin);
	// freopen("1.out", "w", stdout);
	fprintf(stderr, "%.2lfMB\n", abs(&Med - &Mst) / 1048576.0);
	n = read(), m = read();
	for (int i = 1; i <= n; i++) a[i] = readull();
	for (int i = 1; i < n; i++) {
		int u = read(), v = read();
		Tree1::add(u, v);
	}
	for (int i = 1; i < n; i++) {
		int u = read(), v = read();
		e[u].push_back(v), e[v].push_back(u);
	}
	pre(1, 0), Tree1::pre();
	for (int j = 1; j <= 18; j++)
		for (int i = 1; i <= n; i++)
			fa[i][j] = fa[fa[i][j - 1]][j - 1];
	Blocker::init(), spread();
	for (int i = 1; i <= m; i++) {
		x[i] = read(), y[i] = read(), k[i] = readull(), l1[i] = Tree1::lca(x[i], y[i]);
		Tree1::mdf(x[i], y[i], l1[i], k[i]), p1[i] = qry(x[i], i, 1), p2[i] = qry(y[i], i, 1);
		// cout << ans[i] << endl;
		int l = lca(x[i], y[i]);
		p3[i] = qry(l, i, -2), ans[i] += Tree1::qry(l);
	}
	cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
	for (int i = 1; i <= cnt; i++) {
		for (int j = 1; j <= n; j++) Tree1::sum[j] = 0;
		ULL res = 0;
		for (int j = p[i]; j; j = fa[j][0])
			Tree1::sum[j]++, res += a[j];
		Tree1::dfs2(1, 0);
		for (int j = 1; j <= m; j++) {
			int c = Tree1::qry(x[j], y[j], l1[j]);
			// cout << c << endl;
			res += k[j] * c;
			if (p1[j] == p[i]) ans[j] += res;
			if (p2[j] == p[i]) ans[j] += res;
			if (p3[j] == p[i]) ans[j] -= res * 2;
		}
	}
	for (int i = 1; i <= m; i++)
		printf("%llu\n", ans[i]);
	cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 16ms
memory: 20748kb

input:

3000 3000
7236742292501328495 17973811477309806363 16075782662531676171 17971236571771878676 11392080645527132110 3685563455925680459 9773593720088356683 8313828403245053795 7736401634567449043 1634817828009987181 6951124933529719486 12775126714635387213 15460977209223753216 397573676785925632 31372...

output:

12105153858659381124
18367442707572066757
11668241962484097878
11288238120352358472
1742468310074622166
9942835997686093671
3305677510569607477
17741602000425004088
14984128302052618266
1075081718074605786
6509217537832509095
16750513627843273113
8569443169249732820
14475184194298579044
156111071108...

result:

ok 3000 lines

Test #2:

score: 5
Accepted
time: 17ms
memory: 22760kb

input:

3000 3000
1612333876155866602 8538417838700679227 6080765231437578796 17905224638340228394 12270907925903144224 17944105326358594564 17302041033966840611 1006351124625222126 496336153231744288 9393087977687876980 9553975238547373621 9361882717200384390 15051881329169144319 9757999873162420435 882725...

output:

11133131376095771981
7909873024850695144
16250639243139481926
14562550655578101207
8274205996508264973
178549413271904466
2368406276743327913
7464009386554813982
9439464815411774627
1471756740732097060
15201641099137019227
6774030298556871576
18156105511913219667
1553508745644446823
4225137078364117...

result:

ok 3000 lines

Test #3:

score: 5
Accepted
time: 13ms
memory: 20916kb

input:

3000 3000
9709246061666095435 1861649101703072889 10620139893353930613 17635186539135419482 710209455559527146 6075101384669982511 1120305006358459674 9703156967435388252 1397046737759839382 5259056712870179169 8253156305433022999 710199693203327302 15130650033641744675 10720111924616886955 15543351...

output:

7834604406305153073
5037061270969117785
16481572776620825702
15177894197606565804
3120320619896892806
18008650876379132344
7417108723176816402
13515164814425439399
3299769942258542105
15897528270699011770
11642805469843844638
16764682282380318054
4824039114054405772
4859834102876213962
1234210473247...

result:

ok 3000 lines

Test #4:

score: 5
Accepted
time: 11ms
memory: 20836kb

input:

3000 3000
16538965545220923528 18062192327708400751 10422465150728338588 3471522151129113073 1236650672072793692 1942240200040301168 13090729759591037952 15335798523677372669 9912100622761466753 11177948788405690381 3710859061697501523 4984944638666762977 17278589713462878008 6371292801024547050 868...

output:

8182453933067329108
13535217473847106938
17067385337010269798
3806121648880466130
11322569288575153037
11079197311131660121
9670138330007803226
6554062218199796758
965954569567598779
18055887214749050688
6142620503089407421
8690117812667761187
9547139298346295115
8890987597519353054
1755036654049586...

result:

ok 3000 lines

Test #5:

score: 5
Accepted
time: 15ms
memory: 20964kb

input:

3000 3000
17759588706587888497 10550000524636484378 11601004513528075994 7150322911283804521 4459707248078569712 10692395730842402625 8940418793863522991 12967068928670540447 9954278250450015940 13702413838608801301 10598390500439869870 15110245227553613794 490862872212325709 15164980555660957366 94...

output:

9743736929788175512
16812303667256960040
14694223512340829897
550204232580650311
1175342872438242313
17622261358285047637
7413682703975031220
12643066512274062227
1868985217436232595
5471830334855681322
8070132260376389587
3970361922096052085
218281824643752746
991917103472727104
2960248244218479023...

result:

ok 3000 lines

Subtask #2:

score: 12
Accepted

Dependency #1:

100%
Accepted

Test #6:

score: 12
Accepted
time: 4ms
memory: 20416kb

input:

5 7
0 3 2 6 4
1 2
2 4
1 5
5 3
3 4
4 2
2 5
5 1
5 3 0
3 2 5
4 4 4
4 4 3
5 2 0
3 4 3
5 5 6

output:

15
21
10
13
17
26
18

result:

ok 7 lines

Test #7:

score: 12
Accepted
time: 1416ms
memory: 40784kb

input:

70000 70000
3805295436278888199 9842309351516174725 1566744796319231180 2206519284152256579 2715928675931950447 6346821976624501261 16020972671480798719 14702021753902144915 17127828773798978442 15779168055669690475 4964561323934614661 9395102787554964450 6377076753365184543 15167378195767668817 288...

output:

5971729064136092190
6457394048987305727
13604212649915736394
8639973959364892219
437861319070967556
16133076880026962355
5384937395694479961
4591478439775690843
16071919565966962790
15485626634068969082
10235993901046758372
3449528613427081475
8064280362779764074
12784984512326434905
424951714880051...

result:

ok 70000 lines

Test #8:

score: 12
Accepted
time: 1395ms
memory: 42936kb

input:

70000 70000
17769190865915081913 3772925482507158804 10559962993069063712 16307277356502651642 12014171661057147061 1923543107882042577 13408785599350410314 17786178374951015816 2038922879833426794 2540043772647346461 15419977514837351390 5175974305273838292 16815288359165841441 6295059675346852046 ...

output:

16215781699519408534
17067966839552063165
1639359200259068228
1157756671621253300
12850966537933214537
13917563606289473282
11146906493479190751
869141055866285398
529460535280965984
11437720548737856517
12321579881011015953
4005153170897692243
10217866116994297464
8892403813874757974
12520505236760...

result:

ok 70000 lines

Test #9:

score: 12
Accepted
time: 1358ms
memory: 44568kb

input:

70000 70000
1322605819855709761 1534349070722535975 3956030287626175223 12996546673549161162 7258680666490714729 15591023033141410544 11626890152249303179 7745771567168540351 5535931029756133379 11840793767439557739 6286106656048048381 9490665709724541446 4561258384162386434 2460318488748442222 1303...

output:

7565012138645637258
1080785033897684285
4000254219257999844
8727142139647715419
1784876728989450460
2474052717732723820
5108017366064709316
5232698473118606856
7893212823648229982
6449010654774296779
16571818815110297674
603759348329356530
7364528294111530037
4667545362378304836
3039728935129459889
...

result:

ok 70000 lines

Test #10:

score: 12
Accepted
time: 1447ms
memory: 41528kb

input:

70000 70000
2918414982140182939 1004760492603077644 7526656799259998488 6665485253854847449 7752199419154649757 12763267823077347079 11745132191692540338 6726116817426709990 15550876907005962464 9760509858122842638 684733892856965421 10077915441058780247 8380400329996723109 16920573433866702239 3069...

output:

8230389499860859172
16425656898047941538
107743004356580170
9778122844868660722
11068387722102791183
13252614309136720348
15937842372230698728
12777338070107774364
17974062134369145560
3740400391792770609
7367804332878038809
14236246024207211797
5659238205278608512
10550373456364765526
3478082332928...

result:

ok 70000 lines

Test #11:

score: 12
Accepted
time: 2687ms
memory: 44504kb

input:

70000 70000
14167059704556856337 16190708842842354431 16763990539754009056 7631426709261583690 16701377874952853623 13128000186728267818 13668914249103068169 11444044591715948726 461080622438520919 15327533341012334586 15905150558482528923 18113008235210277231 18273290154232335325 871461822812191943...

output:

5416890687002400795
15434184693210288436
14994504916760087024
2057026449542829151
14782289435774270062
5375237679514404106
6242405047854012647
13176621545709355733
14860610197328732602
2320525143444929350
4955538191022622551
16072981679771537209
16493487770453132249
7457010288198365370
1095949888193...

result:

ok 70000 lines

Test #12:

score: 12
Accepted
time: 1203ms
memory: 41724kb

input:

70000 70000
6512290618577097706 2307104154841663907 18099814251235047570 8297332016606109910 6979819983598849680 18022671181330012408 7003320957516774041 10765303713874539785 15263207007138552812 11713955610641877995 9084887894280210904 3653718255996209121 14197591595561260765 2937670413926210256 43...

output:

5372775214253596890
927985558228810546
3829815088328182672
17496384540548895622
2541458359607440535
9685902106698191409
13649653134779075960
2952563488513208867
3457470079648848247
11542323450217419837
6576344363223624061
12316990756988470568
17923006133291073450
13069551524451668138
648013229980407...

result:

ok 70000 lines

Test #13:

score: 12
Accepted
time: 1371ms
memory: 42972kb

input:

70000 70000
13665984219894847790 9458613748861462697 7467746948118990839 10855454155004540952 10025433108785732161 15816172836312183738 2834129139700401667 221649423184372325 8409217794427284711 16119623676185869010 12488380095384700010 3049877130176336551 5805665682633632307 13224802542929355280 18...

output:

7934210059911784858
14305091721658406168
5803801684631217062
8806866881905382618
14997911434771439753
7006465422324293550
15394754861139766679
8377831978907312075
12227086919743533414
8784212755151945751
17039860679476902214
6474495685436520748
11136139762939837997
16869294577244011226
1164744311638...

result:

ok 70000 lines

Subtask #3:

score: 0
Time Limit Exceeded

Dependency #2:

100%
Accepted

Test #14:

score: 0
Time Limit Exceeded

input:

120000 120000
4056283459929576306 2264755903151268173 1157390036441353969 5734735320959854923 6025999163810189446 3972481234804681969 4746636248696530169 6716674455256322787 6407347371842702902 7463142557880503801 208361219405474896 512530621977574257 6488145455921761864 6595856237657889728 95997703...

output:


result:


Subtask #4:

score: 0
Time Limit Exceeded

Test #21:

score: 0
Time Limit Exceeded

input:

200000 200000
622783158027686223 2242697872372232537 8481648430436878777 10092474834140799044 15403999682625301609 12614289513474949582 9180944589267018841 7823784919308285798 8257785171198951273 5134508521895120821 8041682272181381093 3835432206618893170 2653803171409877650 5589823419153460372 1007...

output:


result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #27:

score: 0
Time Limit Exceeded

input:

200000 200000
1958469220619413759 14991498002015735322 6054491201406941902 18206143187746582567 15082377615826460430 2936248617457291604 10073577150351675920 16534472678586906457 2207599132486246393 10301540360769075442 1492580560381080472 551692353431379140 13238280352539145808 8462626987240986565 ...

output:


result:


Subtask #6:

score: 0
Time Limit Exceeded

Test #34:

score: 19
Accepted
time: 4803ms
memory: 85356kb

input:

200000 200000
6794776813641982926 1561596256197101737 10910039723053043515 7892247858295192798 12233819960547881004 17695389034783066733 9173201689566865598 17626618141377486739 7358781671024283919 6787559733384974662 3884392438269280436 14872846228351316833 9037842441501571648 14299818404271084016 ...

output:

5519324519442957729
13462861144392030499
8898301730697138469
4148979398311169421
15090246851327208067
8628707816538336707
13867297907038176506
10296428352441857103
15654304415409320656
7404566644919251615
9870876264015800597
6356224262148620783
249874952637342736
9023132497407647441
1416175985367538...

result:

ok 200000 lines

Test #35:

score: 19
Accepted
time: 4622ms
memory: 85416kb

input:

200000 200000
11863650499568632095 6646669915142734572 4053375998669045948 14662364203830894482 7319511971537928619 4131498054494361157 7103043576860354080 2730587527012777841 8626012955062792888 7098277606750148625 12990209623538415680 100871355087529915 12267084290544303817 7008468684400973426 856...

output:

11796827338356402281
853302424664654652
564419876379363056
11173501553976506440
2595891684253320024
5590778053561483334
16613470245879883493
5698255402711241072
8125718572356459513
11371252602486101178
7605800585391618993
17939653588292877927
15906636118593883045
14840423959426947118
122409373008752...

result:

ok 200000 lines

Test #36:

score: 19
Accepted
time: 4971ms
memory: 84976kb

input:

200000 200000
7819034666999754227 1261434745131385029 4729051582132640442 9603230030483067185 9188148629137054368 4647584887897271213 14878584596185020248 5036501082632549589 13434605396022727436 10747373329537943689 2859138487129973374 17399126170759425906 5170686633178482234 1548518177806164267 68...

output:

13979872615900358449
13089809959604496472
1561607543806976909
704865086165131395
4750114500765789463
8620101000132483891
1061990993707638791
8219417779659049453
16783172337878697026
731502258552473136
15516870835064396649
13412140913912394465
5029558179142432386
9655856473800271062
14498308915283050...

result:

ok 200000 lines

Test #37:

score: 19
Accepted
time: 4720ms
memory: 85432kb

input:

200000 200000
13667364256171297683 5643454264481609425 13916992424255487273 16139471110620927446 10964822257543206396 18104319795931117637 6754294313610492941 627150985476406559 9787453073837140391 8330282299925244302 16457178623381098023 2644823686047461659 3971198924127091796 9747389824949735337 1...

output:

11367465309833674818
18362877133708215281
7423548724337603968
8957456044849213851
17833913066069915980
7858235162317128668
12543057704351927321
10505054748074388644
6816176482433035300
2467436539272277421
1679282916199502250
4514431222303891247
8020348968469583082
4250522620562980350
344143146282723...

result:

ok 200000 lines

Test #38:

score: 19
Accepted
time: 4287ms
memory: 85328kb

input:

200000 200000
8264785020939323331 5620958879723030311 933008402364228745 5711653520387260744 16167130674961631916 10635243671618608635 7034482071437480120 10254956504177159052 10510387087831623788 8381634740474427698 9506597691312026965 14784451691298216046 15821757099494287606 1919888068508281591 1...

output:

5787014875832445646
9634577796009446262
12314294073540302873
2915216425908688602
7120463906657998875
4286046781319255714
6776880553034928756
7781782119312943753
10261843991161497641
6413565360321098258
15025688638596291097
9526784383328827422
4012177064000612489
1287077077700121461
98702777920648956...

result:

ok 200000 lines

Test #39:

score: 0
Time Limit Exceeded

input:

200000 200000
11664667196988092805 1811572170904050995 15694419630875459125 12737549840477675073 16755302998318006416 4014818780481352253 5609118000649893828 6256332194728466258 15733576495075669968 17960532384856428505 15897732465031414620 17425753576410476171 4620624862371502705 112264419736513372...

output:


result:


Subtask #7:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

0%