QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#171257 | #6322. Forestry | ucup-team1209# | WA | 275ms | 34232kb | C++20 | 2.9kb | 2023-09-09 16:42:11 | 2023-09-09 16:42:11 |
Judging History
answer
#include<bits/stdc++.h>
using std::cin;
using std::cout;
const int N = 300005;
using u64 = unsigned long long;
const int mod = 998244353;
const int inv2 = (mod + 1) / 2;
struct func { int k, a; };
func operator + (func x, func y) {
return {(u64) x.k * y.k % mod, ((u64) x.a * y.k + y.a) % mod};
}
func v[N][2];
int pow(int a, int b, int ans = 1) {
for(;b;b >>= 1, a = (u64) a * a % mod) if(b & 1)
ans = (u64) ans * a % mod;
return ans;
}
struct ds {
int mul, cnt;
ds() { mul = 1, cnt = 0; }
void ins(int x) {
++ x;
if(x) mul = (u64) mul * x % mod;
else ++cnt;
}
void del(int x) {
++ x;
if(x) mul = pow(x, mod-2, mul);
else --cnt;
}
func v() const {
int w = cnt ? 0 : mul;
return {w, w};
}
} pb[N];
int son[N][2], fa[N], rev[N];
int get(int x, int p = 1) { return son[fa[x]][p] == x; }
int isroot(int x) { return !(get(x) || get(x, 0)); }
void update(int x) {
func z = pb[x].v();
v[x][0] = z;
v[x][1] = z;
if(son[x][0]) {
v[x][0] = v[son[x][0]][0] + v[x][0];
v[x][1] = v[x][1] + v[son[x][0]][1];
}
if(son[x][1]) {
v[x][0] = v[x][0] + v[son[x][1]][0];
v[x][1] = v[son[x][1]][1] + v[x][1];
}
}
void rotate(int x) {
int y = fa[x], z = fa[y], b = get(x);
if(!isroot(y)) son[z][get(y)] = x;
son[y][b] = son[x][!b], son[x][!b] = y;
fa[son[y][b]] = y, fa[y] = x, fa[x] = z;
update(y);
}
void put(int x) {
std::swap(son[x][0], son[x][1]);
std::swap(v[x][0], v[x][1]);
rev[x] ^= 1;
}
void pushdown(int x) {
if(rev[x]) {
put(son[x][0]);
put(son[x][1]);
rev[x] = 0;
}
}
void down(int x) {
if(!isroot(x)) down(fa[x]);
pushdown(x);
}
void splay(int x) {
for(;!isroot(x);rotate(x)) if(!isroot(fa[x]))
rotate(get(x) ^ get(fa[x]) ? x : fa[x]);
update(x);
}
int out(int x) {
return v[x][1].a;
}
void access(int x) {
for(int t = 0;x;) {
splay(x);
if(son[x][1]) pb[x].ins(out(son[x][1]));
if(t) pb[x].del(out(t));
son[x][1] = t;
update(x);
t = x;
x = fa[x];
}
}
void makeroot(int x) {
access(x), splay(x), put(x);
}
int n;
int a[N];
std::vector<int> go[N];
int vis[N], rk[N];
int ans = 0, sum = 0;
int main() {
std::ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
for(int i = 1;i <= n;++i) cin >> a[i], update(i), rk[i] = i;
for(int i = 1, u, v;i < n;++i) {
cin >> u >> v;
go[u].push_back(v);
go[v].push_back(u);
}
for(int i = 1;i <= n;++i) pb[i].ins(pow(inv2, go[i].size() - 1) - 1);
std::sort(rk + 1, rk + n + 1, [](int x, int y) {
return a[x] < a[y];
});
const int T = pow(2, n - 2);
for(int i = n;i >= 1;--i) {
int x = rk[i];
for(int t : go[x]) if(vis[t]) {
makeroot(t);
pb[x].ins(out(t));
fa[t] = x;
}
update(x);
sum = (sum + out(x)) % mod;
// cout << (u64) out(x) * T %mod << '\n';
ans = (ans + (u64) (a[rk[i]] - a[rk[i - 1]]) * sum) % mod;
vis[x] = 1;
}
ans = (u64) ans * T % mod;
cout << ans << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17912kb
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: 17852kb
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: -100
Wrong Answer
time: 275ms
memory: 34232kb
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:
788046247
result:
wrong answer 1st numbers differ - expected: '434293031', found: '788046247'