QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#207102 | #7563. Fun on Tree | ucup-team1448 | TL | 2ms | 22008kb | C++14 | 6.8kb | 2023-10-08 09:21:40 | 2023-10-08 09:21:41 |
Judging History
answer
//#define dxx
#ifdef dxx
#define dbg(...) fprintf(stderr,__VA_ARGS__)
#define dex(a) dbg(#a"=%lld onL%d infun %s\n",a,__LINE__,__FUNCTION__)
#include<cstdlib>
#define pause sys##tem("pause")
#define _GLIBCXX_DEBUG
#endif
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using ll=long long;
using std::max;
using std::min;
using std::abs;
template<class T> void cmax(T&a,T b){a=max(a,b);}
template<class T> void cmin(T&a,T b){a=min(a,b);}
template<class T> T sqr(T a){return a*a;}
namespace io {
const int __SIZE = (1 << 21) + 1;
char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof;
#define Gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
inline void flush () { fwrite (obuf, 1, oS - obuf, stdout), oS = obuf; }
inline void gc (char &x) { x = Gc(); }
inline void pc (char x) { *oS ++ = x; if (oS == oT) flush (); }
inline void pstr (const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc (s[__f]); }
inline void gstr (char *s) { for(__c = Gc(); __c < 32 || __c > 126 || __c == ' ';) __c = Gc();
for(; __c > 31 && __c < 127 && __c != ' ' && __c != '\n' && __c != '\r'; ++s, __c = Gc()) *s = __c; *s = 0; }
template <class I> inline bool gi (I &x) { _eof = 0;
for (__f = 1, __c = Gc(); (__c < '0' || __c > '9') && !_eof; __c = Gc()) { if (__c == '-') __f = -1; _eof |= __c == EOF; }
for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
template <class I> inline void print (I x) { if (!x) pc ('0'); if (x < 0) pc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) pc (qu[qr --]); }
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
int N;
struct SEGT{
struct INFO{
ll v; int i;
bool operator<(INFO const&b)const{
return v!=b.v?v<b.v:i>b.i;
}
} ti[800005];
int tadd[800005];
void up(int x){
ti[x]=max(ti[x*2],ti[x*2+1]);
ti[x].v+=tadd[x];
}void build(int x,int l,int r,INFO*a){
int mid=l+r>>1;
tadd[x]=0;
if(l==r){
ti[x]=a[l];
return;
}
build(x*2,l,mid,a);
build(x*2+1,mid+1,r,a);
up(x);
}void upd(int x,int l,int r,int ql,int qr,ll z){
int mid=l+r>>1;
if(r<ql||qr<l||ql>qr) return;
if(ql<=l&&r<=qr){
tadd[x]+=z;
ti[x].v+=z;
return;
}
if(ql<=mid) upd(x*2,l,mid,ql,qr,z);
if(mid<qr) upd(x*2+1,mid+1,r,ql,qr,z);
up(x);
}void updpt(int x,int l,int r,int p,INFO z){
int mid=l+r>>1;
if(l==r){
ti[x]=z;
return;
}
z.v-=tadd[x];
if(p<=mid) updpt(x*2,l,mid,p,z);
else updpt(x*2+1,mid+1,r,p,z);
up(x);
}INFO que(int x,int l,int r,int ql,int qr){
int mid=l+r>>1;
if(qr<l||r<ql||ql>qr) return INFO{-1ll<<61,1<<30};
if(ql<=l&&r<=qr) return ti[x];
INFO res;
if(qr<=mid) res=que(x*2,l,mid,ql,qr);
if(mid<ql) res=que(x*2+1,mid+1,r,ql,qr);
res=max(que(x*2,l,mid,ql,qr),que(x*2+1,mid+1,r,ql,qr));
res.v+=tadd[x];
return res;
}
};
namespace bit{
ll tr[200005];
void _add(int i,ll x){
for(;i<=N;i+=i&-i) tr[i]+=x;
}ll que(int i){
ll ans=0;
for(;i;i-=i&-i) ans+=tr[i];
return ans;
}void add(int l,int r,ll x){
_add(l,x);
_add(r+1,-x);
}
}
namespace gph{
struct edge{
int t,w,n;
} G[400005];
int ecnt=2,hd[200005];
void ade(int s,int t,int w){
G[ecnt]={t,w,hd[s]};
hd[s]=ecnt++;
}
ll vdep[200005];
int dfc,dfn[200005],siz[200005],dep[200005],pr[200005],\
son[200005],top[200005],btm[200005];
void dfs1(int x,int p){
dep[x]=dep[p]+1;
siz[x]=1;
for(int e=hd[x];e;e=G[e].n) if(G[e].t!=p){
vdep[G[e].t]=vdep[x]+G[e].w;
dfs1(G[e].t,x);
siz[x]+=siz[G[e].t];
if(siz[G[e].t]>siz[son[x]]) son[x]=G[e].t;
}
}void dfs2(int x,int tp){
dfn[x]=++dfc;
top[x]=tp;
btm[x]=x;
if(son[x]){
dfs2(son[x],tp);
btm[x]=btm[son[x]];
for(int e=hd[x];e;e=G[e].n) if(!top[G[e].t])
dfs2(G[e].t,G[e].t);
}
}
SEGT t1,t2;
SEGT::INFO quesut(int x,int y){
if(y)
return max(t1.que(1,1,N,dfn[x],dfn[y]-1),
t1.que(1,1,N,dfn[y]+siz[y],dfn[x]+siz[x]-1));
return t1.que(1,1,N,dfn[x],dfn[x]+siz[x]-1);
}
void gupd(int x,int z){
t1.upd(1,1,N,dfn[x],dfn[x]+siz[x]-1,-z);
if(top[x]!=x) t2.upd(1,1,N,dfn[x],dfn[btm[x]],-z);
bit::add(dfn[x],dfn[x]+siz[x]-1,-z);
x=pr[top[x]];
while(x){
auto retr=quesut(x,son[x]);
retr.v=retr.v-vdep[x]*2-bit::que(dfn[top[x]]);
t2.updpt(1,1,N,dfn[x],retr);
x=pr[top[x]];
}
}auto gque(int x){
SEGT::INFO ans=SEGT::INFO{-(1ll<<61),1<<30};
int y=0,z=x;
while(x){
auto retr=quesut(x,y);
retr.v+=vdep[z]-vdep[x]*2;
cmax(ans,retr);
if(top[x]!=x){
retr=t2.que(1,1,N,dfn[top[x]],dfn[pr[x]]);
retr.v+=vdep[z]+bit::que(dfn[top[x]]);
cmax(ans,retr);
}
y=top[x];
x=pr[top[x]];
}
return ans;
}
}
namespace xm{
SEGT::INFO initer[200005];
int a[200005];
void _(){
int Q;
io::gi(N);
io::gi(Q);
for(int i=1;i<=N;++i) io::gi(a[i]);
for(int i=2;i<=N;++i){
int w;
io::gi(gph::pr[i]);
io::gi(w);
gph::ade(gph::pr[i],i,w);
}
gph::dfs1(1,0);
gph::dfs2(1,1);
for(int i=1;i<=N;++i)
initer[gph::dfn[i]]=SEGT::INFO{gph::vdep[i]-a[i],i};
gph::t1.build(1,1,N,initer);
for(int i=1;i<=N;++i){
initer[gph::dfn[i]]=gph::quesut(i,gph::son[i]);
initer[gph::dfn[i]].v-=gph::vdep[i]*2;
}
gph::t2.build(1,1,N,initer);
while(Q--){
ll z;
int x,y;
io::gi(x);
io::gi(y);
io::gi(z);
gph::gupd(y,z);
auto ans=gph::gque(x);
io::print(ans.i);
io::pc(32);
io::print(ans.v);
io::pc(10);
}
}
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
xm::_();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 20120kb
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:
6 100005 6 10 6 10 1 4 1 -1 1 1
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 20104kb
input:
5 6 -10 0 2 -4 8 1 7 1 1 2 2 2 -2 1 1 100 2 1 -100 1 1 0 4 3 10 2 5 3 5 2 2
output:
4 -87 1 17 4 13 1 19 1 17 1 15
result:
ok 6 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 20032kb
input:
6 3 0 0 0 0 0 0 1 10 1 10 1 -100 4 10 4 11 1 1 0 4 1 0 1 4 1000
output:
2 10 6 11 2 10
result:
ok 3 lines
Test #4:
score: 0
Accepted
time: 0ms
memory: 22008kb
input:
2 0 1 1 1 3
output:
result:
ok 0 lines
Test #5:
score: -100
Time Limit Exceeded
input:
200000 200000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...