QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#291502 | #7563. Fun on Tree | liuhengxi | WA | 806ms | 45640kb | C++14 | 3.7kb | 2023-12-26 20:13:01 | 2023-12-26 20:13:02 |
Judging History
answer
// created: Dec/26/2023 19:30:26
#include<cstdio>
#include<vector>
#define F(i,l,r) for(int i=(l),i##_end=(r);i<i##_end;++i)
using namespace std;
template<typename T>void readmain(T &x)
{
bool neg=false;unsigned c=getchar();
for(;(c^48)>9;c=getchar())if(c=='-')neg=true;
for(x=0;(c^48)<10;c=getchar())x=(x<<3)+(x<<1)+(c^48);
if(neg)x=-x;
}
template<typename T>T& read(T &x){readmain(x);return x;}
template<typename T,typename ...Tr>void read(T &x,Tr&... r){readmain(x);read(r...);}
constexpr int N=2e5+5;
struct value
{
long long a;int b;
friend bool operator<(const value &u,const value &v){return u.a!=v.a?u.a<v.a:u.b>v.b;}
friend value operator+(const value &u,const long long &v){return {u.a+v,u.b};}
friend value operator-(const value &u,const long long &v){return {u.a-v,u.b};}
};
struct seg
{
static constexpr int N=(1<<19)+5;
struct node
{
value a;
long long t;
}s[N];
void update(int p,long long v){s[p].a.a+=v;s[p].t+=v;}
#define lc (p<<1)
#define rc (p<<1|1)
void pushdown(int p){update(lc,s[p].t);update(rc,s[p].t);s[p].t=0;}
void build(int p,int l,int r,const value *a)
{
if(r-l==1)return s[p].a=a[l],void();
int mid=(l+r)>>1;
build(lc,l,mid,a);build(rc,mid,r,a);
s[p].a=max(s[lc].a,s[rc].a);
}
void update(int p,int l,int r,int x,int y,int v)
{
if(x==l&&r==y)return update(p,v);
int mid=(l+r)>>1;
pushdown(p);
if(y<=mid)update(lc,l,mid,x,y,v);
else if(mid<=x)update(rc,mid,r,mid,y,v);
else update(lc,l,mid,x,mid,v),update(rc,mid,r,mid,y,v);
s[p].a=max(s[lc].a,s[rc].a);
}
void update(int p,int l,int r,int x,const value &v)
{
if(r-l==1)return s[p].a=v,void();
int mid=(l+r)>>1;
pushdown(p);
if(x<mid)update(lc,l,mid,x,v);
else update(rc,mid,r,x,v);
s[p].a=max(s[lc].a,s[rc].a);
}
value get(int p,int l,int r,int x)
{
if(r-l==1)return s[p].a;
int mid=(l+r)>>1;
pushdown(p);
if(x<mid)return get(lc,l,mid,x);
else return get(rc,mid,r,x);
}
value query(int p,int l,int r,int x,int y)
{
if(x==l&&r==y)return s[p].a;
int mid=(l+r)>>1;
pushdown(p);
if(y<=mid)return query(lc,l,mid,x,y);
if(mid<=x)return query(rc,mid,r,x,y);
return max(query(lc,l,mid,x,mid),query(rc,mid,r,mid,y));
}
#undef lc
#undef rc
}s1,s2;
int n,q,a[N],siz[N],dfn[N][3],ind,top[N],fa[N];
long long dep[N];
value s[N];
vector<pair<int,int>> adj[N];
void update(int u)
{
value v=s1.get(1,0,n,dfn[u][0]);
if(dfn[u][1]<dfn[u][2])v=max(v,s1.query(1,0,n,dfn[u][1],dfn[u][2]));
v=v-2*dep[u];
s2.update(1,0,n,dfn[u][0],v);
}
void dfs1(int u)
{
siz[u]=1;
for(pair<int,int> vw:adj[u])
{
dep[vw.first]=dep[u]+vw.second;
fa[vw.first]=u;
dfs1(vw.first);
siz[u]+=siz[vw.first];
}
}
void dfs2(int u)
{
dfn[u][0]=ind++;
int hc=-1;
for(pair<int,int> vw:adj[u])if(!~hc||siz[vw.first]>siz[hc])hc=vw.first;
if(~hc)top[hc]=top[u],dfs2(hc);
dfn[u][1]=ind;
for(pair<int,int> vw:adj[u])if(vw.first!=hc)dfs2(top[vw.first]=vw.first);
dfn[u][2]=ind;
}
int main()
{
read(n,q);
F(i,0,n)read(a[i]);
F(i,1,n)
{
int p,w;read(p,w);--p;
adj[p].emplace_back(i,w);
}
dfs1(0);
dfs2(0);
F(i,0,n)s[dfn[i][0]]={dep[i]-a[i],i};
s1.build(1,0,n,s);
F(i,0,n)update(i);
while(q--)
{
int x,y,v;read(x,y,v);--x,--y;
s1.update(1,0,n,dfn[y][0],dfn[y][2],-v);
s2.update(1,0,n,dfn[y][0],dfn[y][2],-v);
while((y=top[y]))update(y=fa[y]);
long long d=dep[x];
value ans=s1.query(1,0,n,dfn[x][0],dfn[x][2])-2*dep[x];
while(top[x])
{
ans=max(ans,s2.query(1,0,n,dfn[top[x]][0],dfn[x][0]+1));
x=fa[top[x]];
}
ans=max(ans,s2.query(1,0,n,0,dfn[x][0]+1));
ans=ans+d;
printf("%d %lld\n",ans.b+1,ans.a);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 10204kb
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: 0ms
memory: 7796kb
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: 7740kb
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: 7528kb
input:
2 0 1 1 1 3
output:
result:
ok 0 lines
Test #5:
score: -100
Wrong Answer
time: 806ms
memory: 45640kb
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 ...
output:
119017 15000000000 123161 16000000000 123161 15000000000 123161 15000000000 123161 16000000000 123161 14000000000 123161 15000000000 123161 17000000000 123161 16000000000 123161 12000000000 123161 17000000000 123161 15000000000 123161 13000000000 123161 16000000000 123161 17000000000 123161 15000000...
result:
wrong answer 2nd lines differ - expected: '120167 17000000000', found: '123161 16000000000'