QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#107955 | #6322. Forestry | zhaohaikun | TL | 0ms | 0kb | C++14 | 3.4kb | 2023-05-23 11:11:21 | 2023-05-23 11:11:26 |
Judging History
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], 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]) * 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); read(m);
F(i, 1, n) read(a[i]);
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
4 1 2 3 4 1 2 2 4 3 2