QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#107956#6322. ForestryzhaohaikunAC ✓1012ms175856kbC++143.5kb2023-05-23 11:13:232023-05-23 11:13:24

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-23 11:13:24]
  • 评测
  • 测评结果:AC
  • 用时:1012ms
  • 内存:175856kb
  • [2023-05-23 11:13:23]
  • 提交

answer

#include <bits/stdc++.h>
#define SZ(x) (int) x.size() - 1
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); }
template <typename T> void read(T &x) {
	x = 0; int f = 1; char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	T l = 0;
	ull y = 0;
	if (!x) { putchar(48); return; }
	if (x < 0) { x = -x; putchar('-'); }
	while (x) { y = y * 10 + x % 10; x /= 10; ++l; }
	while (l) { putchar(y % 10 + 48); y /= 10; --l; }
}
template <typename T> void writeln(T x) { write(x); puts(""); }
template <typename T> void writes(T x) { write(x); putchar(32); }
constexpr int N = 3e5 + 10, MOD = 998244353, inv2 = (MOD + 1) >> 1;
int pwn = 1, n, m, rt[N], tot, tmp[N], seg[N * 40], seg2[N * 40], ls[N * 40], rs[N * 40], ans, tag[N * 40], a[N], b[N], tag2[N * 40];//, tag3[N * 40];
vector <int> v[N];
void down2(int num, int x) {
	tag2[num] = (tag2[num] + (ll) x * tag[num]) % MOD;
	seg2[num] = (seg2[num] + (ll) x * seg[num]) % MOD;
}
void down(int num, int x) {
	tag[num] = (ll) tag[num] * x % MOD;
	seg[num] = (ll) seg[num] * x % MOD;
}
void pushdown(int num) {
	if (tag2[num]) {
		down2(ls[num], tag2[num]); down2(rs[num], tag2[num]);
		tag2[num] = 0;
	}
	if (tag[num] != 1) {
		down(ls[num], tag[num]); down(rs[num], tag[num]);
		tag[num] = 1;
	}
}
void pushup(int num) {
	seg[num] = (seg[ls[num]] + seg[rs[num]]) % MOD;
	seg2[num] = (seg2[ls[num]] + seg2[rs[num]]) % MOD;
}
void insert(int &num, int l, int r, int x, int y) {
	num = ++tot, tag[num] = 1;
	if (l == r) return seg[num] = y, void();
	int mid = (l + r) >> 1;
	if (mid >= x) insert(ls[num], l, mid, x, y);
	else insert(rs[num], mid + 1, r, x, y);
	pushup(num);
}
void mer(int &x, int y, int a, int b, int l, int r) {
	if (!x && !y) return;
	if (!y) return down(x, (ll) (a + 1) * inv2 % MOD), void();
	if (!x) return x = y, down2(x, inv2), down(x, (ll) b * inv2 % MOD), void();
	if (l == r) {
		seg2[x] = ((ll) seg2[x] + seg2[y] + (ll) seg[y] * inv2) % MOD;
		seg[x] = ((ll) seg[x] * (a + seg[y] + 1) % MOD + (ll) seg[y] * b % MOD) * inv2 % MOD;
		return;
	} pushdown(x), pushdown(y);
	int mid = (l + r) >> 1;
	mer(ls[x], ls[y], (a + seg[rs[y]]) % MOD, (b + seg[rs[x]]) % MOD, l, mid);
	mer(rs[x], rs[y], a, b, mid + 1, r);
	pushup(x);
}
int sum;
void query(int num, int l, int r) {
	if (l == r) {
		sum = (sum + (ll) (seg[num] + seg2[num]) * b[l]) % MOD;
		return;
	} pushdown(num);
	int mid = (l + r) >> 1;
	query(ls[num], l, mid); query(rs[num], mid + 1, r);
}
void dfs(int x, int fa) {
	insert(rt[x], 1, m, a[x], 1);
	for (int i: v[x])
		if (i != fa) {
			dfs(i, x);
			mer(rt[x], rt[i], 0, 0, 1, m);
		}
}
signed main() {
	// freopen("birth.in", "r", stdin);
	// freopen("birth.out", "w", stdout);
	read(n);
	F(i, 1, n) read(a[i]), b[i] = a[i];
	sort(b + 1, b + n + 1);
	m = unique(b + 1, b + n + 1) - b - 1;
	F(i, 1, n) a[i] = lower_bound(b + 1, b + m + 1, a[i]) - b;
	F(i, 2, n) {
		pwn = (pwn + pwn) % MOD;
		int x, y; read(x); read(y);
		v[x].push_back(y);
		v[y].push_back(x);
	} dfs(1, 0);
	query(rt[1], 1, m);
	cout << (ll) sum * pwn % MOD;
	return 0;
}

详细

Test #1:

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

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: 1ms
memory: 10444kb

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: 902ms
memory: 147684kb

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: 968ms
memory: 152308kb

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: 947ms
memory: 155752kb

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: 997ms
memory: 172400kb

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: 490ms
memory: 172900kb

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: 526ms
memory: 175856kb

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: 547ms
memory: 170252kb

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: 573ms
memory: 169168kb

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: 953ms
memory: 159368kb

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: 934ms
memory: 156132kb

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: 1012ms
memory: 156144kb

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: 875ms
memory: 145444kb

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: 822ms
memory: 159584kb

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"