QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#220017#7578. Salty FishPhantomThreshold#AC ✓1067ms128924kbC++203.6kb2023-10-19 20:47:212023-10-19 20:47:22

Judging History

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

  • [2023-10-19 20:47:22]
  • 评测
  • 测评结果:AC
  • 用时:1067ms
  • 内存:128924kb
  • [2023-10-19 20:47:21]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;

const int maxn = 310000;
const int maxp = maxn*4;
const int inf  = 1e15;

vector<int>E[maxn];
int n,m;

struct segment
{
	int tot;
	int seg[maxp],flag[maxp];
	int lc[maxp],rc[maxp];
	int newnode()
	{
		++tot;
		lc[tot]=rc[tot]=flag[tot]=0;
		seg[tot]=-inf;
		return tot;
	}
	void init()
	{
		tot=0;
	}
	void pushdown(const int x)
	{
		if(flag[x])
		{
			int fl=flag[x]; flag[x]=0;
			
			seg[lc[x]]+=fl; seg[rc[x]]+=fl;
			flag[lc[x]]+=fl; flag[rc[x]]+=fl;
		}
	}
	void build(int &x,const int l,const int r)
	{
		if(!x) x=newnode();
		if(l==r) return;
		int mid=(l+r)>>1;
		build(lc[x],l,mid); build(rc[x],mid+1,r);
	}
	int lx,rx,c;
	void add(const int x,const int l,const int r)
	{
		if(rx<l||r<lx) return;
		if(lx<=l&&r<=rx)
		{
			seg[x]+=c;
			flag[x]+=c;
			return;
		}
		pushdown(x);
		int mid=(l+r)>>1;
		add(lc[x],l,mid); add(rc[x],mid+1,r);
		seg[x]=max( seg[lc[x]],seg[rc[x]] );
	}
	int query(const int x,const int l,const int r)
	{
		if(rx<l||r<lx) return -inf;
		if(lx<=l&&r<=rx) return seg[x];
		pushdown(x);
		int mid=(l+r)>>1;
		return max( query(lc[x],l,mid), query(rc[x],mid+1,r) );
	}
	int loc;
	void upd(const int x,const int l,const int r)
	{
		if(l==r)
		{
			seg[x]=max(seg[x],c);
			return;
		}
		int mid=(l+r)>>1;
		pushdown(x);
		if(loc<=mid) upd(lc[x],l,mid);
		else upd(rc[x],mid+1,r);
		seg[x]=max( seg[lc[x]], seg[rc[x]] );
	}
}seg;

int ai[maxn];
int son[maxn],d[maxn],top[maxn],rt[maxn];
void dfs(const int x)
{
	d[x]=0;
	son[x]=0;
	for(auto y:E[x])
	{
		dfs(y);
		if(d[x]<d[y]+1) 
		{
			d[x]=d[y]+1;
			son[x]=y;
		}
	}
}
void dfs2(const int x,const int tp)
{
	top[x]=tp;
	rt[x]=0;
	if(x==tp)
	{
		seg.build(rt[x],1,1+d[x]);
	}
	
	if(son[x]) dfs2(son[x],tp);
	for(auto y:E[x]) if(y!=son[x])
	{
		dfs2(y,y);
	}
}
vector< pair<int,int> >V[maxn];
void dp(const int x)
{
	for(auto y:E[x])
	{
		dp(y);
	}
	
	int ff=top[x];
	
	//if(son[x])
	{
		seg.lx= 1+d[ff]-d[x]+1, seg.rx=1+d[ff];
		seg.c = max( 0ll, seg.query(rt[ff],1,1+d[ff]) )+ai[x];
		
		seg.loc= 1+d[ff]-d[x];
		seg.upd(rt[ff],1,1+d[ff]);
	}
/*	else
	{
		seg.loc= 1+d[ff]-d[x];
		seg.c=ai[x];
		seg.upd(rt[ff],1,1+d[ff]);
	}*/
	
	for(auto y:E[x]) if(y!=son[x])
	{
		for(int l=0;l<=d[y];l++)
		{
			seg.lx=1+d[ff]-d[x]+l+1, seg.rx=1+d[ff];
			int tc= max( 0ll, seg.query(rt[ff],1,1+d[ff]) );
			
			seg.lx=1+l, seg.rx=1+d[y];
			int cc= max( 0ll, seg.query(rt[y],1,1+d[y]) );
			
			seg.lx=seg.rx= 1+d[ff]-d[x]+l+1;
			seg.c=cc;
			seg.add(rt[ff],1,1+d[ff]);
			
			if(l==0)
			{
				seg.lx=seg.rx= 1+d[ff]-d[x];
				seg.c=cc;
				seg.add(rt[ff],1,1+d[ff]);
			}
			
			seg.c= cc+tc;
			seg.loc= 1+d[ff]-d[x]+l+1;
			seg.upd(rt[ff],1,1+d[ff]);
		}
	}
	for(auto [ki,ci]:V[x])
	{
		seg.lx=1+d[ff]-d[x],seg.rx= min(1+d[ff],seg.lx+ki);
		seg.c=-ci;
		seg.add(rt[ff],1,1+d[ff]);
	}
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int Tcase; cin>>Tcase;
	while(Tcase--)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		{
			vector<int>_;
			E[i].swap(_);
			vector< pair<int,int> >__;
			V[i].swap(__);
		}
		for(int i=2;i<=n;i++)
		{
			int fi; cin>>fi;
			E[fi].emplace_back(i);
		}
		for(int i=1;i<=n;i++) cin>>ai[i];
		for(int i=1;i<=m;i++)
		{
			int xi,ki,ci; cin>>xi>>ki>>ci;
			V[xi].emplace_back(ki,ci);
		}
		
		dfs(1);
		dfs2(1,1);
		seg.init();
		
		dp(1);
		int ans;
		seg.lx=1,seg.rx=1+d[1];
		ans=max( 0ll, seg.query(rt[1],1,1+d[1]) );
		cout<<ans<<'\n';
	}
	
	return 0;	
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 35996kb

input:

1
6 3
1 1 2 2 3
2 5 4 3 3 2
2 1 3
3 1 7
1 2 4

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: 0
Accepted
time: 1067ms
memory: 128924kb

input:

1003
100 100
1 2 3 4 5 6 7 8 9 10 6 1 2 4 1 11 17 14 17 2 13 8 8 5 11 7 18 6 2 10 23 11 13 3 9 1 33 20 3 9 32 35 11 41 42 29 33 45 21 35 9 36 12 54 19 24 57 31 32 5 3 10 46 15 46 48 20 44 5 41 67 7 18 30 27 6 29 69 57 75 62 74 18 64 17 21 38 60 79 69 54 90 83 83 31 96 31 93 53
152 498 653 559 287 38...

output:

20180
17083
14650
19924
15814
20189
20894
18175
18478
13758
20217
23680
15562
12882
18046
18132
20548
17000
23734
18740
24814
16728
20979
19727
16450
21717
15739
22081
17803
23024
14820
21503
23497
15804
18097
17197
21236
21286
14250
11632
18335
12317
16313
22840
18583
15245
19331
25978
22388
17091
...

result:

ok 1003 numbers