QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#205419#7563. Fun on Treeucup-team1447#WA 1556ms110960kbC++146.6kb2023-10-07 15:53:502023-10-07 15:53:51

Judging History

你现在查看的是最新测评结果

  • [2023-10-07 15:53:51]
  • 评测
  • 测评结果:WA
  • 用时:1556ms
  • 内存:110960kb
  • [2023-10-07 15:53:50]
  • 提交

answer

// what is matter? never mind. 
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2") 
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define ull unsigned long long
#define int long long
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	if(f)x=-x;return x;
}

int mod;
struct modint{
	int x;
	modint(int o=0){x=o;}
	modint &operator = (int o){return x=o,*this;}
	modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
	modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
	modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
	modint &operator ^=(int b){
		modint a=*this,c=1;
		for(;b;b>>=1,a*=a)if(b&1)c*=a;
		return x=c.x,*this;
	}
	modint &operator /=(modint o){return *this *=o^=mod-2;}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
	friend modint operator /(modint a,modint b){return a/=b;}
	friend modint operator ^(modint a,int b){return a^=b;}
	friend bool operator ==(modint a,int b){return a.x==b;}
	friend bool operator !=(modint a,int b){return a.x!=b;}
	bool operator ! () {return !x;}
	modint operator - () {return x?mod-x:0;}
	bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}

vector<modint> fac,ifac,iv;
inline void initC(int n)
{
	if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
	int m=iv.size(); ++n;
	if(m>=n)return;
	iv.resize(n),fac.resize(n),ifac.resize(n);
	For(i,m,n-1){
		iv[i]=iv[mod%i]*(mod-mod/i);
		fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
	}
}
inline modint C(int n,int m){
	if(m<0||n<m)return 0;
	return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 200005
#define inf 0x3f3f3f3f3f3f3f3f

// buru toptree/fn

int n,q,a[maxn];
vector<pii>e[maxn];

int fa[maxn],son[maxn],sz[maxn],dep[maxn];
int dfn[maxn],out[maxn],top[maxn],que[maxn],idx,dis[maxn];
void dfs1(int u){
	sz[u]=1;
	for(auto [v,w]:e[u]){
		if(v==fa[u])continue;
		fa[v]=u,dep[v]=dep[u]+1,dis[v]=dis[u]+w,dfs1(v),sz[u]+=sz[v];
		if(sz[v]>sz[son[u]])son[u]=v; 
	}
}

int lca(int u,int v){
	for(;top[u]!=top[v];u=fa[top[u]])if(dep[top[u]]<dep[top[v]])swap(u,v);
	return dep[u]<dep[v]?u:v;
}
int kth(int u,int k){
	while(k>=dfn[u]-dfn[top[u]]+1&&dfn[u]!=1)
		k-=dfn[u]-dfn[top[u]]+1,u=fa[top[u]];
	return que[dfn[u]-k];
}
int jump(int u,int v){
	for(;top[u]!=top[v];u=fa[top[u]])
		if(fa[top[u]]==v)return top[u];
	return son[v];
}

struct bit{
	vector<int>tr;
	int n;
	void init(int N){n=N,tr=vector<int>(N+1,0);}
	void add(int x,int y){for(;x<=n;x+=x&-x)tr[x]+=y;}
	int ask(int x){
		int s=0;
		for(;x;x^=x&-x)s+=tr[x];
		return s;
	}
	void down(){For(i,1,n)tr[i]+=tr[i^(i&-i)];}
}T;

// 

struct segt{
	pii a[maxn];
	pii mx[maxn<<2];
	int tag[maxn<<2];
	void up(int p){
		mx[p]=max(mx[p<<1],mx[p<<1|1]);
		mx[p].fi+=tag[p];
	}
	void build(int p,int l,int r){
		if(l==r)return mx[p]=a[l],void();
		int mid=l+r>>1;
		build(p<<1,l,mid);
		build(p<<1|1,mid+1,r); up(p);
	}
	void upd(int p,int l,int r,int x,pii y){
		if(l==r)return mx[p]=y,void();
		int mid=l+r>>1;
		y.fi-=tag[p];
		if(x<=mid)upd(p<<1,l,mid,x,y);
		else upd(p<<1|1,mid+1,r,x,y);
		up(p);
	}
	void add(int p,int l,int r,int nl,int nr,int v){
		if(l>=nl&&r<=nr)return tag[p]+=v,mx[p].fi+=v,void();
		int mid=l+r>>1;
		if(nl<=mid)add(p<<1,l,mid,nl,nr,v);
		if(nr>mid)add(p<<1|1,mid+1,r,nl,nr,v);
		up(p);
	}
	pii ask(int p,int l,int r,int ql,int qr){
		if(ql>qr)return mkp(-inf,-inf);
		if(l>=ql&&r<=qr)return mx[p];
		int mid=l+r>>1; pii res=mkp(-inf,-inf);
		if(ql<=mid)res=max(res,ask(p<<1,l,mid,ql,qr));
		if(qr>mid)res=max(res,ask(p<<1|1,mid+1,r,ql,qr));
		res.fi+=tag[p];
		return res;
	}
}T1,T2;

set<pii>s[maxn];

int ed[maxn];
pii sec(int u){
	if(s[u].size()<=1)return mkp(-inf,-inf);
	return *(--(--s[u].end()));
}
void dfs2(int u,int tp){
	top[u]=tp,dfn[u]=++idx,que[idx]=u,ed[tp]=u;
	if(son[u])dfs2(son[u],tp);
	for(auto [v,w]:e[u])if(v!=fa[u]&&v!=son[u])dfs2(v,v);
	out[u]=idx;
	s[u].insert(mkp(dis[u]+a[u],-u));
	for(auto [v,w]:e[u]){
		if(v!=fa[u] && v!=son[u]){
			s[u].insert(T1.ask(1,1,n,dfn[v],out[v]));
		}
	}
	T1.upd(1,1,n,dfn[u],*s[u].rbegin());
	auto it=*s[u].rbegin();
	it.fi-=2*dis[u];
	T2.upd(1,1,n,dfn[u],it);
}

bool in(int u,int v){
	return dfn[v]>=dfn[u] && dfn[v]<=out[u];
}

void wk(int u,int w){
	int uu=u;
	while(u){
		int t=top[u];
		if(fa[t]){
			int f=fa[t];
			pii tmp=T1.ask(1,1,n,dfn[t],out[t]);
			tmp.fi-=T.ask(dfn[f]);
			assert(s[f].count(tmp));
			s[f].erase(tmp);
		}
		u=fa[top[u]];
	}
	u=uu;
	T.add(dfn[u],w);
	T.add(out[u]+1,-w);
	
	T1.add(1,1,n,dfn[u],out[u],w);
	T2.add(1,1,n,dfn[u],out[u],w);
	
	while(u){
		int t=top[u];
		if(fa[t]){
			int f=fa[t];
			pii tmp=T1.ask(1,1,n,dfn[t],out[t]);
			tmp.fi-=T.ask(dfn[f]);
			s[f].insert(tmp);
			tmp=*s[f].rbegin();
			T1.upd(1,1,n,dfn[f],tmp);
			tmp.fi-=2*dis[f];
			T2.upd(1,1,n,dfn[f],tmp);
		}
		u=fa[top[u]];
	}
}

pii ask(int u){
	int uu=u;
	pii res=mkp(-inf,-inf);
	while(u){
		res=max(res,T2.ask(1,1,n,dfn[top[u]],dfn[u]-1));
//		cout<<"res "<<res.fi<<" "<<res.se<<"\n";
		pii tmp=T1.ask(1,1,n,dfn[u]+1,dfn[ed[top[u]]]); tmp.fi-=dis[u]*2;
//		cout<<"tmp "<<tmp.fi<<" "<<tmp.se<<"\n";
		res=max(res,tmp);
		auto it=--s[u].end();
		int adds=-dis[u]*2+T.ask(dfn[u]);
		if(uu==u || !in(-(it->se),uu)){
			pii tt=*it; tt.fi+=adds;
			res=max(res,tt);
		}
		else if(s[u].size()>1){
			--it;
			pii tt=*it; tt.fi+=adds;
			res=max(res,tt);
		}
		u=fa[top[u]];
	}
	res.fi+=dis[uu];
	return res;
}

signed main()
{
	n=read(),q=read();
	For(i,1,n)a[i]=-read();
	For(u,2,n){
		int v=read(),w=read();
		e[u].pb(mkp(v,w));
		e[v].pb(mkp(u,w));
	}
	dfs1(1),dfs2(1,1);
	T.init(n);
	For(_,1,q){
		int x=read(),y=read(),w=-read();
		wk(y,w);
		pii tmp=ask(x);
		cout<<-tmp.se<<" "<<tmp.fi<<"\n";
	}
	return 0;
}

/*
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


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
*/

詳細信息

Test #1:

score: 100
Accepted
time: 9ms
memory: 61076kb

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: 61204kb

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: 4ms
memory: 61044kb

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: 62856kb

input:

2 0
1 1
1 3

output:


result:

ok 0 lines

Test #5:

score: -100
Wrong Answer
time: 1556ms
memory: 110960kb

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
120167 17000000000
119017 15000000000
119017 15000000000
120167 17000000000
120167 15000000000
120167 16000000000
119017 17000000000
119017 16000000000
119017 12000000000
119017 17000000000
120167 16000000000
120167 14000000000
120167 17000000000
120167 18000000000
120167 16000000...

result:

wrong answer 6083rd lines differ - expected: '119017 16000000000', found: '106237 16000000000'