QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#820702 | #9905. 哈夫曼树 | wsyear | 0 | 0ms | 0kb | C++20 | 3.3kb | 2024-12-18 23:38:47 | 2024-12-18 23:38:47 |
answer
// Problem: E. 哈夫曼树
// Memory Limit: 1024 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// Author: Donomonayuas
#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
template<class T>inline void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T>inline void chkmx(T &x, T y) { if (y > x) x = y; }
using namespace std;
mt19937_64 rnd(random_device{}());
const int maxn = 200010;
const int maxm = maxn * 100;
const ll maxv = 1e15;
map<pll, ull> MP;
map<ull, int> used;
ull get(ull x, ull y) {
if (x > y) swap(x, y);
if (MP.count(pll(x, y))) return MP[pll(x, y)];
ull ha = rnd();
while (used[ha]) ha = rnd();
used[ha] = 1;
return MP[pll(x, y)] = ha;
}
struct node {
int emp, len;
ull hal, har, ham;
ll vall, valr;
node() { emp = 1; }
node(ll x, int cnt) {
if (!cnt) {
emp = 1;
} else {
emp = 0;
len = cnt & 1;
hal = har = get(x, x) * (cnt >> 1);
vall = valr = x;
if (!(cnt & 1)) ham = get(x, x) * ((cnt >> 1) - 1);
}
}
friend node operator+(node x, node y) {
node res;
res.emp = x.emp & y.emp;
if (res.emp) return res;
if (x.emp) return y;
if (y.emp) return x;
res.len = x.len ^ y.len;
res.vall = x.vall, res.valr = y.valr;
if (x.len && y.len) {
res.hal = res.har = x.har + y.hal + get(x.valr, y.vall);
res.ham = x.hal + y.har;
} else if (!x.len && !y.len) {
res.hal = res.har = x.hal + y.har;
res.ham = x.ham + y.ham + get(x.valr, y.vall);
} else if (x.len && !y.len) {
res.hal = x.hal + y.hal;
res.har = x.har + get(x.valr, y.vall) + y.ham;
} else if (!x.len && y.len) {
res.har = x.har + y.har;
res.hal = x.ham + get(x.valr, y.vall) + y.hal;
}
return res;
}
} t[maxm];
int n, q, L[maxn], R[maxn], fa[maxn], ok[maxn], tot, rt, ls[maxm], rs[maxm], cc[maxm];
ll a[maxn];
ull sum;
#define mid ((l + r) >> 1)
void upd(int &p, ll l, ll r, ll x, int v) {
if (!p) p = ++tot;
if (l == r) return cc[p] += v, t[p] = node(x, cc[p]), void();
if (x <= mid) upd(ls[p], l, mid, x, v);
else upd(rs[p], mid + 1, r, x, v);
t[p] = t[ls[p]] + t[rs[p]];
}
#undef mid
ull get() { return t[rt].har; }
int main() {
cin.tie(nullptr) -> ios::sync_with_stdio(false);
cin >> n >> q, q--;
rep (i, 1, n) cin >> a[i];
rep (i, n + 1, 2 * n - 1) cin >> L[i] >> R[i], fa[L[i]] = fa[R[i]] = i;
rep (i, 1, n) {
int x = fa[i];
while (x) a[x] += a[i], x = fa[x];
}
rep (i, 1, 2 * n - 1) upd(rt, 1, maxv, a[i], 1);
rep (i, n + 1, 2 * n - 1) sum += get(a[L[i]], a[R[i]]);
cout << (sum == get() ? "YES" : "NO") << '\n';
while (q--) {
ll x, v;
cin >> x >> v, v -= a[x];
while (x) {
if (fa[x]) sum -= get(a[L[fa[x]]], a[R[fa[x]]]);
upd(rt, 1, maxv, a[x], -1), a[x] += v, upd(rt, 1, maxv, a[x], 1);
if (fa[x]) sum += get(a[L[fa[x]]], a[R[fa[x]]]);
x = fa[x];
}
cout << (sum == get() ? "YES" : "NO") << '\n';
}
}
详细
Subtask #1:
score: 0
Memory Limit Exceeded
Test #1:
score: 0
Memory Limit Exceeded
input:
3 4 1 1 1 2 3 1 4 2 2 1 2 3 3
output:
YES NO YES NO
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #16:
score: 0
Time Limit Exceeded
input:
10000 10000 85117964119 41472951000 61693640396 66409648221 91978532661 62922448518 92497200794 43837348258 45577855926 38256001396 79446271832 95289903258 62510175551 97599809584 56162244722 87617641117 64010325734 56604859803 58544571483 40687963085 38627694508 64665875035 62273927372 73014847094 ...
output:
YES YES YES YES NO NO NO NO NO NO NO NO NO NO YES YES YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO N...
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #20:
score: 0
Time Limit Exceeded
input:
50000 50000 16394396247 17456058492 11358090355 13208121167 8612535629 2853817504 18100755237 18603321637 1618810635 7615832530 13631222424 7817630977 10963098997 19654927084 645638016 9352759139 17939720223 15106346489 14475376391 2122412099 15482023637 11224675857 15931143509 4240408932 1270948838...
output:
YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO NO NO NO NO NO NO NO NO NO YES NO YES YES NO NO NO NO NO NO NO NO NO NO NO NO YES YES YES NO NO NO NO NO NO NO NO NO NO NO NO NO YES YES YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO...
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #24:
score: 0
Time Limit Exceeded
input:
70 100000 66748 126 1 91045172 3605661959574 274077743637349 147314183 8209537 740253 6920630855 25494 1377240316614 15756 6 108000 18118446805 169389361127761 29316262755 48 2643445763 5834083602536 3 9439745562111 29 3719 10 47434709561 11197815949 6018 325122336074 851181326345 1633739329 1527382...
output:
YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES ...