ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#199289 | #465. game | tkswls | 100 | 671ms | 28536kb | C++11 | 2.6kb | 2023-12-09 11:17:41 | 2023-12-09 12:09:39 |
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, m, a[200005], l[200005], r[200005], t[200005], ccnt, head[200005], nxt[400005], to[400005], fa[200005], num[200005], vis[200005];
int ans, in[200005];
inline void add(int p, int q) {
nxt[++ccnt] = head[p];
to[ccnt] = q;
head[p] = ccnt;
}
struct node {
int l, r, maxa, maxb, tag;
inline node operator+ (const node& p)const {
node op;
op.l = l, op.r = p.r;
if (maxa > p.maxa) {
op.maxa = maxa;
op.maxb = maxb;
} else {
op.maxa = p.maxa;
op.maxb = p.maxb;
}
op.tag = 0;
return op;
}
} b[800005];
inline void push_down(int p) {
if (b[p].tag != 0 && b[p].l != b[p].r) {
b[2 * p].tag += b[p].tag;
b[2 * p + 1].tag += b[p].tag;
b[2 * p].maxa += b[p].tag;
b[2 * p + 1].maxa += b[p].tag;
b[p].tag = 0;
return;
}
}
inline void dfs(int p, int q) {
fa[p] = q;
num[p] = num[q] + a[p];
int flg = 1;
l[p] = 0x3f3f3f3f, r[p] = -1;
for (int i = head[p]; i; i = nxt[i]) {
if (to[i] != q) {
flg = 0;
dfs(to[i], p);
l[p] = min(l[to[i]], l[p]), r[p] = max(r[p], r[to[i]]);
}
}
if (flg) {
t[++ccnt] = p;
l[p] = ccnt, r[p] = ccnt;
return;
}
}
inline void build(int p, int l, int r) {
// cout << p << " " << l << " " << r << '!';
b[p].l = l, b[p].r = r;
b[p].tag = 0;
if (l == r) {
// cout << t[l] << "[]" << num[t[l]] << "[]\n";
b[p].maxb = l, b[p].maxa = num[t[l]];
return;
}
build(2 * p, l, (l + r) >> 1);
build(2 * p + 1, ((l + r) >> 1) + 1, r);
b[p] = b[2 * p] + b[2 * p + 1];
}
inline void change(int p, int l, int r, int w) {
if (b[p].l >= l && b[p].r <= r) {
b[p].maxa += w;
b[p].tag += w;
return;
}
push_down(p);
int mid = (b[p].l + b[p].r) >> 1;
if (r <= mid) change(2 * p, l, r, w);
else if (l > mid) change(2 * p + 1, l, r, w);
else change(2 * p, l, mid, w), change(2 * p + 1, mid + 1, r, w);
b[p] = b[2 * p] + b[2 * p + 1];
}
inline void solve(int p) {
while (!vis[p] && p != 0) {
vis[p] = 1;
// cout << l[p] << " " << r[p] << " " << -a[p] << "!";
change(1, l[p], r[p], -a[p]);
p = fa[p];
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int p, q;
for (int i = 1; i < n; i++) {
cin >> p >> q;
in[q]++;
add(p, q);
}
int rt = 0;
for (int i = 1; i <= n; i++) {
if (in[i] == 0) {
rt = i;
break;
}
}
ccnt = 0;
dfs(rt, 0);
build(1, 1, ccnt);
for (int i = 1; i <= m; i++) {
int op = t[b[1].maxb];
ans += b[1].maxa;
solve(op);
}
cout << ans;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 0ms
memory: 1464kb
input:
1000 2 709700801 152640991 988131423 805797743 939123875 782742211 880215050 187178846 574805065 325...
output:
103155408830
result:
ok single line: '103155408830'
Test #2:
score: 10
Accepted
time: 0ms
memory: 1468kb
input:
1000 245 709700801 152640991 988131423 805797743 939123875 782742211 880215050 187178846 574805065 3...
output:
421103565360
result:
ok single line: '421103565360'
Test #3:
score: 10
Accepted
time: 0ms
memory: 1464kb
input:
1000 245 709700801 152640991 988131423 805797743 939123875 782742211 880215050 187178846 574805065 3...
output:
421103565360
result:
ok single line: '421103565360'
Test #4:
score: 10
Accepted
time: 56ms
memory: 14832kb
input:
100000 22645 709700801 152640991 988131423 805797743 939123875 782742211 880215050 187178846 5748050...
output:
39704318118429
result:
ok single line: '39704318118429'
Test #5:
score: 10
Accepted
time: 62ms
memory: 14936kb
input:
100000 22642 1047706648 254612080 455415899 800948123 590783813 436205181 685263875 247167811 948867...
output:
41698882455202
result:
ok single line: '41698882455202'
Test #6:
score: 10
Accepted
time: 55ms
memory: 14936kb
input:
100000 22642 1047706648 254612080 455415899 800948123 590783813 436205181 685263875 247167811 948867...
output:
41698882455202
result:
ok single line: '41698882455202'
Test #7:
score: 10
Accepted
time: 139ms
memory: 28536kb
input:
200000 45142 1047706648 254612080 455415899 800948123 590783813 436205181 685263875 247167811 948867...
output:
79779284177942
result:
ok single line: '79779284177942'
Test #8:
score: 10
Accepted
time: 145ms
memory: 28508kb
input:
200000 54862 311937903 356615937 996474967 796098503 242509288 89635383 490279933 307156776 24918764...
output:
85952803390219
result:
ok single line: '85952803390219'
Test #9:
score: 10
Accepted
time: 212ms
memory: 28396kb
input:
200000 54865 649943750 458619793 463792211 791248883 967943818 816872945 295328758 367145740 6232498...
output:
83448756475871
result:
ok single line: '83448756475871'
Test #10:
score: 10
Accepted
time: 2ms
memory: 1468kb
input:
1000 2 371727723 50669902 447072355 810647363 213656577 55570185 1457169 127189881 200742865 9068421...
output:
108410685518
result:
ok single line: '108410685518'