QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#204056 | #7563. Fun on Tree | Forever_Young# | RE | 0ms | 0kb | C++14 | 5.7kb | 2023-10-07 00:36:59 | 2023-10-07 00:37:00 |
answer
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) ((x).begin()), ((x).end())
#define fi first
#define se second
typedef long long LL;
const LL infll = 1e18;
const int N = 200022;
int vst[N], fa[N], siz[N], p[N];
int tim = 0;
LL w[N], a[N];
bool banned[N];
vector<pair<int, LL> > e[N];
struct SGT {
int n;
vector<pair<LL, int>> a;
void build(int k, int le, int ri) {
a[k].se = -1;
a[k].fi = -infll;
if(le != ri) {
int mid = (le + ri) / 2;
build(k * 2, le, mid);
build(k * 2 + 1, mid + 1, ri);
}
}
void modify(int k, int le, int ri, int pos, pair<LL, int> val) {
if(le == ri) {
a[k] = val;
}else {
int mid = (le + ri) / 2;
if(pos <= mid) {
modify(k * 2, le, mid, pos, val);
}else {
modify(k * 2 + 1, mid + 1, ri, pos, val);
}
a[k] = max(a[k * 2], a[k * 2 + 1]);
}
}
void add(int k, int le, int ri, int pos, LL val) {
if(le == ri) {
a[k].fi += val;
}else {
int mid = (le + ri) / 2;
if(pos <= mid) {
add(k * 2, le, mid, pos, val);
}else {
add(k * 2 + 1, mid + 1, ri, pos, val);
}
a[k] = max(a[k * 2], a[k * 2 + 1]);
}
}
pair<LL, int> getmax(int k, int le, int ri, int ql, int qr) {
if(ql > qr) {
return make_pair(-infll, -1);
}
if(ql <= le && ri <= qr) {
return a[k];
}else {
pair<LL, int> res{-infll, -1};
int mid = (le + ri) / 2;
if(ql <= mid) res = max(res, getmax(k * 2, le, mid, ql, qr));
if(qr >= mid + 1) res = max(res, getmax(k * 2 + 1, mid + 1, ri, ql, qr));
return res;
}
}
};
struct Dnode {
int rt;
map<int, int> mp;
vector<int> fa, blg;
vector<LL> dep;
LL tag;
SGT sgt;
LL len;
vector<Dnode*> s;
void resize(int n) {
fa.resize(n);
blg.resize(n);
dep.resize(n);
}
} dn_pool[N * 2];
int dn_num = 0;
Dnode * dvcq(int st) {
Dnode &dn = dn_pool[dn_num++];
map<int, int> &mp(dn.mp);
vector<int> q;
q.pb(st);
mp[st] = q.size();
tim++;
vst[st] = tim;
fa[st] = -1;
for(int op = 0; op < (int)q.size(); op++) {
int v = q[op];
siz[v] = 0;
if(banned[v]) continue;
for(auto t : e[v]) {
int y = t.fi;
if(vst[y] != tim) {
vst[y] = tim;
q.pb(y);
mp[y] = q.size();
fa[y] = v;
}
}
}
for(int i = (int)q.size() - 1; i >= 0; i--) {
siz[q[i]]++;
if(fa[q[i]] != -1) siz[fa[q[i]]] += siz[q[i]];
}
cout << "q" << q.size() << endl;
dn.resize(q.size());
int rt = st;
for(;;) {
bool flag = true;
for(auto t : e[rt]) {
int y = t.fi;
if(fa[y] == rt && siz[y] > q.size() / 2) {
rt = y;
flag = false;
break;
}
}
if(flag) break;
}
if(q.size() == 2) rt = banned[st] ? q.back() : q.front();
q.clear();
q.pb(rt);
dn.rt = rt;
tim++;
vst[rt] = tim;
vector<int> & blg(dn.blg);
vector<int> & fa(dn.fa);
vector<LL> & dep(dn.dep);
dep[mp[rt]] = 0;
fa[mp[rt]] = -1;
blg[mp[rt]] = -1;
for(int op = 0; op < (int)q.size(); op++) {
int v = q[op];
int _ = 0;
int mpv = mp[v];
if(banned[v]) continue;
for(auto t : e[v]) {
int y = t.fi;
if(vst[y] != tim) {
vst[y] = tim;
q.pb(y);
int mpy = mp[y];
fa[mpy] = mpv;
dep[mpy] = dep[mpv] + t.se;
if(v == rt) {
blg[mpy] = _;
}else {
blg[mpy] = blg[mpv];
}
}
_++;
}
}
assert(banned[rt] == false);
if(q.size() == 2) {
dn.len = dep[mp[q.back()]];
dn.sgt.n = 2;
dn.sgt.a.resize(9);
dn.sgt.build(1, 1, 2);
dn.sgt.modify(1, 1, 2, 1, make_pair(a[rt], -rt));
dn.sgt.modify(1, 1, 2, 2, make_pair(a[q.back()], -q.back()));
return &dn;
}
banned[rt] = true;
dn.sgt.n = e[rt].size();
dn.sgt.a.resize(4 * dn.sgt.n + 1);
dn.sgt.build(1, 1, dn.sgt.n);
dn.s.resize(dn.sgt.n);
for(int i = 0; i < (int)e[rt].size(); i++) {
dn.s[i] = dvcq(e[rt][i].fi);
dn.sgt.modify(1, 1, dn.sgt.n, i + 1, dn.s[i]->sgt.a[1]);
}
dn.tag = 0;
return &dn;
}
pair<LL, int> operator + (const pair<LL, int> & a, LL b) {
return make_pair(a.fi + b, a.se);
}
void add(Dnode * p, int y, int fa, LL w) {
int mpy = p->mp[y];
int mpp = p->mp.count(fa) ? p->mp[fa] : -1;
if(p->sgt.a.empty()) {
if(mpp != -1) {
int pos = mpy == p->rt ? 1 : 2;
p->sgt.add(1, 1, 2, pos, w);
}else {
p->sgt.add(1, 1, 2, 1, w);
p->sgt.add(1, 1, 2, 2, w);
}
return;
}
if(mpp != -1 && p->fa[mpp] == mpy) {
p->tag += w;
add(p->s[p->blg[mpp]], fa, y, -w);
p->sgt.modify(1, 1, p->sgt.n, p->blg[mpp] + 1, p->s[p->blg[mpp]]->sgt.a[1] + p->s[p->blg[mpp]]->tag);
}else {
if(y == p->rt) {
p->tag += w;
}else {
add(p->s[p->blg[mpy]], y, fa, w);
p->sgt.modify(1, 1, p->sgt.n, p->blg[mpy] + 1, p->s[p->blg[mpy]]->sgt.a[1] + p->s[p->blg[mpy]]->tag);
}
}
}
pair<LL, int> query(Dnode * p, int x) {
if(p->sgt.a.empty()) {
if(x == p->rt) {
return max(p->sgt.a[2], p->sgt.a[3] + p->len);
}else {
return max(p->sgt.a[2] + p->len, p->sgt.a[3]);
}
}
LL mpx = p->mp[x];
LL depx = p->dep[mpx];
if(x == p->rt) {
return p->sgt.a[1] + p->tag;
}
pair<LL, int> res{-infll, -1};
LL blg = p->blg[mpx];
res = max(p->sgt.getmax(1, 1, p->sgt.n, 1, blg), p->sgt.getmax(1, 1, p->sgt.n, blg + 2, p->sgt.n));
res = max(res, query(p->s[blg], x));
return res + p->tag;
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for(int i = 1; i < n; i++) {
scanf("%d%lld", &p[i + 1], &w[i + 1]);
e[p[i + 1]].pb({i + 1, w[i + 1]});
e[i + 1].pb({p[i + 1], w[i + 1]});
}
Dnode * rt = dvcq(1);
for(int i = 1; i <= q; i++) {
LL x, y, w;
scanf("%lld%lld%lld", &x, &y, &w);
add(rt, y, p[y], w);
pair<LL, int> res = query(rt, x);
printf("%d %lld\n", res.se * -1, res.fi);
}
}
详细
Test #1:
score: 0
Runtime Error
input:
6 6 1 1 4 5 1 4 1 5 2 0 3 2 4 1 5 6 3 2 -100000 1 2 100000 1 1 0 2 2 66 3 1 5 4 4 -3
output:
q6 q3